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;
getClass() method returns the runtime class of an object. That Class object is the object that is locked by static synchronized methods of the represented class.
The getName() method of java Class class is used to get the name of the entity, and that entity can be class, interface, array, enum, method, etc. of the class object.
You cannot override getClass .
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 SThe 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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With