Java Reference
In-Depth Information
You may get an error when you try to use * (asterisk) to multiply two numbers. The operating system may
interpret it as all files names in the current directory. To avoid such errors, you can enclose the operator in double
quotes or the escape character provided by the operating system.
java com.jdojo.array.Calc 7 "*" 8
If your program uses command-line arguments, it is not a 100% Java program. this is because the program
does not fit in the category of “write once, run everywhere.” some operating systems do not have a command prompt,
and therefore, you may not be able to use the command-line argument feature. additionally, an operating system may
interpret the metacharacters in the command-line arguments differently.
Tip
Multi-Dimensional Arrays
If a data element in a list is identified using more than one dimension, you can use a multi-dimensional array to
represent the list in your program. For example, a data element in a table is identified by two dimensions, row and
column. You can store a tabular data in your Java program in a two dimensional array. You can declare a multi-
dimensional array by using a pair of brackets ([]) for each dimension in the array declaration. For example, you can
declare a two dimensional array of int as shown:
int[][] table;
Here, table is a reference variable that can hold a reference to a two-dimensional array of int . At the time of
declaration, memory is allocated only for the reference variable table , not for any array elements. The memory state
after the above code is executed is depicted in Figure 15-6 .
table
X
Figure 15-6. Memory state after the declaration of a two-dimensional array
A two-dimensional array of int with three rows and two columns can be created as shown:
table = new int[3][2];
The memory state after execution of this code is depicted in Figure 15-7 . All elements have been shown to
have a value of zero, because all elements of a numeric array are initialized to zero by default. The rules for default
initialization of array elements of a multi-dimensional array are the same as that of a single dimensional array as
discussed previously in this chapter.
table
0
0
0
0
X
0
0
Figure 15-7. Memory state after the creation of a two-dimensional array
 
 
Search WWH ::




Custom Search