getClass method Java with array types

2019-01-23 14:38发布

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;

5条回答
ら.Afraid
2楼-- · 2019-01-23 15:21

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.

查看更多
时光不老,我们不散
3楼-- · 2019-01-23 15:22

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

查看更多
兄弟一词,经得起流年.
4楼-- · 2019-01-23 15:26

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.

查看更多
太酷不给撩
5楼-- · 2019-01-23 15:35

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, Second Edition.

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"
查看更多
贪生不怕死
6楼-- · 2019-01-23 15:43

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).

查看更多
登录 后发表回答