getClass method Java with array types

孤者浪人 提交于 2019-12-29 04:36:08

问题


So I've run into something odd and I don't know what it's called so I'm having trouble finding out information about it, hence my question here.

I've run into an issue where if you create an array of any type and call getClass on this array in Java you will get an odd return. I am wondering why you get this specific return and what it means.

Code example is as follows:

byte[] me = new byte[1];
int[] me2 = new int[1];
double[] me3 = new double[1];
float[] me4 = new float[1];
String[] me5 = new String[1];
Integer[] me6 = new Integer[1];

System.out.println(me.getClass());                  
System.out.println(me2.getClass());                 
System.out.println(me3.getClass());                 
System.out.println(me4.getClass());                 
System.out.println(me5.getClass());
System.out.println(me6.getClass());

and the output is:

 class [B
 class [I
 class [D
 class [F
 class [Ljava.lang.String;
 class [Ljava.lang.Integer;

回答1:


The toString method of Class invokes the getName method of Class which

Returns the name of the entity (class, interface, array class, primitive type, or void) represented by this Class object, as a String. 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 by The Java™ Language Specification.

If this class object represents a primitive type or void, then the name returned is a String equal to the 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 the name of the element type preceded by one or more '[' characters representing the depth of the array nesting. The encoding of element type names is as follows:

 Element Type           Encoding
 boolean                Z
 byte                   B
 char                   C
 class or interface     Lclassname;
 double                 D
 float                  F
 int                    I
 long                   J
 short                  S 

The class or interface name classname is the binary name of the class specified above.

Examples:

 String.class.getName()
     returns "java.lang.String"
 byte.class.getName()
     returns "byte"
 (new Object[3]).getClass().getName()
     returns "[Ljava.lang.Object;"
 (new int[3][4][5][6][7][8][9]).getClass().getName()
     returns "[[[[[[[I"



回答2:


It's just some stupid naming convention. Would been much better if they are more humanly readable: class byte[] , class java.lang.Integert[][]




回答3:


The [ at the start of the class name should be read as "array of ...", with "..." being what follows the [; the conventions for what follows are spelled out in the documentation for Class.getName() that @emory cited and linked to.

Section 4.3.1 of the Java Language Specification starts: "An object is a class instance or an array." This suggests that arrays are not class instances. (Perhaps this is what @TigerTrussell was getting at in his answer.) However, Section 10.8 starts: "Every array has an associated Class object, shared with all other arrays with the same component type. The direct superclass of an array type is Object." This suggests that arrays are, if not true class instances, pretty darn close.

Unfortunately, the syntax rules for Java prohibit inheriting from an array type:

public class MyFancyArray extends int[] { // ILLEGAL
   // ...
}

Note that this is a syntax constraint; there are no conceptual or semantic barriers (and it's allowed in many other OO languages).




回答4:


Those are the names of the underlying type object. The [ indicates it's an array, and the following letter indicates the array type. B=byte, I=integer, D=double, etc. "L" is for class type members as you can see.




回答5:


Arrays don't have classes, they are simply data structures. The only thing having a class would be something extended from Object which is included in the array.



来源:https://stackoverflow.com/questions/6867131/getclass-method-java-with-array-types

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!