I have a need to create the class name of an array in Java. I notice when using reflection to look at class names an array has a certain pattern:
For example for an array of array of com.foo.Thing
[[Lcom.foo.Thing;
What is the formula used to create this class name? I presume other letters besides 'L' mean different things. Are there libraries that help with this?
One letter for each primitive, one symbol for arrays, and one letter for reference:
I
= intJ
= longD
= doubleF
= floatZ
= booleanC
= charB
= byteS
= shortLcom/foo/Thing;
= reference 'com.foo.Thing'[
= array of whatever is next (so,[[I
is anint[][]
).
This is VM-speak for type signatures. For example, the signature of:
public boolean[] foo(String[] args, int count) { ... }
is: ([Ljava/lang/String;I)[Z
.
It is for machines and not humans; it is easy to parse (you just move forward, no need to look-ahead).
This is not the 'class name' of a thing; the usual name for this construct is 'vm name'. Note that generics just disappear from these; the VM name of List<String>
is Ljava/util/List;
. It's why you can't override methods if the two methods end up with the same name, param types, and return type if you remove all generics.