Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What is the class for array type reference?

 
Chandra Bairi
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class TestInt {
TestInt a[];
public static void main(String[] a) {
System.out.println("Class name :"+a.getClass()+ " got it");
}
}
O/p is class [Ljava.lang.String;
What does that mean?
 
Tom Blough
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It appears the the "[L" denotes an array. I wonder if it's a typo in the library and should be "[]".
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java class files use a specific "type code" notation to describe the type of fields and the signatures of methods. A synopsis:
There's a single letter that stands for each primitive type:
I is int, F is float, D is double, J is long, V is void, etc.
A class type is denoted by the letter "L", followed by the name of the class using "/" as a separator, followed by a ";". So "Ljava/lang/String;"
An array is denoted by a prefix "[", so that an array of String is "[java/lang/String;" . A 2D array of int would be "[[I".
A method signature is a set of parentheses containing the types of the arguments separated by commas, followed by the return type of the method; so the signature of main is "([Ljava/lang/String ;) V" .
Now, when you call getClass().getName() on an array class, Java converts the slashes to periods, but otherwise passes you the same type descriptor that would be used in the class file.
This page contains all the details.
[ October 23, 2003: Message edited by: Ernest Friedman-Hill ]
 
Donald R. Cossitt
buckaroo
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See Retrieving Component Types at java.sun.com.
Also see "ALLOWED_CLASSNAMES" under "Field Detail" and "getClassName" under "Method Detail" of Class Open Type
HTH
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic