What is the formulation for Java Array class names?

Greg :

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?

rzwitserloot :

One letter for each primitive, one symbol for arrays, and one letter for reference:

  • I = int
  • J = long
  • D = double
  • F = float
  • Z = boolean
  • C = char
  • B = byte
  • S = short
  • Lcom/foo/Thing; = reference 'com.foo.Thing'
  • [ = array of whatever is next (so, [[I is an int[][]).

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.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=393667&siteId=1