Java Reference
In-Depth Information
String[] sArr = {"A", "B"} ;
String[][] ssArr = {{"AA"}, {"BB"}} ;
String[][][] sssArr = {} ; // A 3D empty array of string
// Print the class name for all arrays
System.out.println("int[]:" + getClassName(iArr));
System.out.println("int[][]:" + getClassName(iiArr));
System.out.println("int[][][]:" + getClassName(iiiArr));
System.out.println("String[]:" + getClassName(sArr));
System.out.println("String[][]:" + getClassName(ssArr));
System.out.println("String[][][]:" + getClassName(sssArr));
}
// Any java object can be passed to getClassName() method.
// Since every array is an object, we can also pass an array to this method.
public static String getClassName(Object obj) {
// Get the reference of its class
Class c = obj.getClass();
// Get the name of the class
String className = c.getName();
return className;
}
}
int[]:[I
int[][]:[[I
int[][][]:[[[I
String[]:[Ljava.lang.String;
String[][]:[[Ljava.lang.String;
String[][][]:[[[Ljava.lang.String;
The class name of an array starts with left bracket(s) ([) . The number of left brackets is equal to the dimension of
the array. For an int array, the left bracket(s) is followed by a character I . For a reference type array, the left bracket(s)
is followed by a character L , followed by the name of the class name, which is followed by a semicolon. The class
names for one-dimensional primitive arrays and a reference type are shown in Table 15-3 .
 
Search WWH ::




Custom Search