[Ljava.lang.Object; this is the name Object[].class , java.lang.Class representing the class of the array Object .
The naming scheme is documented in Class.getName() :
If this class object represents a reference type that is not an array type, then the binary name of the class is returned, as specified in the Java Language Specification ( Β§13.1 ).
If this class object is a primitive type or void , then the return name is a Java language keyword corresponding to the primitive type or void .
If this class object represents a class of arrays, then the internal form of the name consists of an element type name preceded by one or more characters '[' representing the depth of the array. The encoding of element type names is as follows:
Element Type Encoding boolean Z byte B char C double D float F int I long J short S class or interface Lclassname;
Your last on this list. Here are some examples:
// xxxxx varies System.out.println(new int[0][0][7]); // [[[I@xxxxx System.out.println(new String[4][2]); // [[Ljava.lang.String;@xxxxx System.out.println(new boolean[256]); // [Z@xxxxx
The reason that the toString() method in arrays returns a String in this format is because arrays are not the @Override method inherited from Object , which is specified as follows:
The toString method for the Object class returns a string consisting of the name of the class whose object the instance is, the at-sign '@' character and the six-digit hexadecimal representation of the object's hash code. In other words, this method returns a string equal to the value:
getClass().getName() + '@' + Integer.toHexString(hashCode())
Note : you cannot rely on toString() any arbitrary object to follow the above specification, as they can (and usually do) @Override to return something else. A more reliable way to check the type of an arbitrary object is to call getClass() on it (the final method inherited from Object ), and then reflection on the returned Class object. Ideally, however, the API should be designed so that reflection is not necessary (see Effective Java 2nd Edition, paragraph 53: Preferring interfaces to reflection).
About the more "useful" toString for arrays
java.util.Arrays provides toString overloads for primitive arrays and Object[] . There is also deepToString which you can use for nested arrays.
Here are some examples:
int[] nums = { 1, 2, 3 }; System.out.println(nums); // [I@xxxxx System.out.println(Arrays.toString(nums)); // [1, 2, 3] int[][] table = { { 1, }, { 2, 3, }, { 4, 5, 6, }, }; System.out.println(Arrays.toString(table)); // [[I@xxxxx, [I@yyyyy, [I@zzzzz] System.out.println(Arrays.deepToString(table)); // [[1], [2, 3], [4, 5, 6]]
There are also Arrays.equals and Arrays.deepEquals that compare element equality over their elements, among many other methods related to arrays.
Related Questions
- Java Arrays.equals () returns false for two-dimensional arrays. - in-depth lighting