Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

using getclass on references

 
ronnir paterl
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Im facing this wierd problem while using getClass method on different references

e.g
intarray.getClass() gives me "Class [I"
where intarray is a reference to int[]
Objarray.getClass() gives me "Class [Ljava.lang.Object;"
where Objarray is a reference to Object[] .. also why does it have ";" in the end
myclass.getClass() gives me "Class [Lmyclass;"
where myclass is a class created by me.

My Question are
a. Why doesnt it show the entire name of the class in eg. 1
b. What determines the prefix appended to the class names as in eg. 2 &3

Thanks
 
Henry Wong
author
Marshal
Pie
Posts: 20889
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My Question are
a. Why doesnt it show the entire name of the class in eg. 1
b. What determines the prefix appended to the class names as in eg. 2 &3


When you call the getClass() method, you will get a Class object that gives you a ton of information about the class -- its fields, its methods, its interfaces, etc.

To answer (a), one of the methods of the Class object that is returned, gives you the "entire name of the class".

To answer (b), I am assuming that you simply did a system out of the class object and got a string (that is pretty useless). When you print the Class object, all you see is the result of the toString() method. The result of the toString() method simple returns "Class" followed by the class descriptor (as a string) for the class.

Don't just println() the class object, use the methods of the class object to obtain the information about the class.

Henry
 
Henry Wong
author
Marshal
Pie
Posts: 20889
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
.. also why does it have ";" in the end


BTW, for the full details on why it ends with ";", or start with "[L", etc., google for "Java class descriptor" -- for the string format of the descriptor.

Henry
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a) this is an array of *primitives* - there really isn't a true class name in this case.
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Henry Wong:
To answer (b), I am assuming that you simply did a system out of the class object and got a string (that is pretty useless). When you print the Class object, all you see is the result of the toString() method. The result of the toString() method simple returns "Class" followed by the class descriptor (as a string) for the class.

Except he's not printing an instance of his own class, he's print an instance of an array of his own class.

Sadly, arrays have a quite useless toString() implementation. Instead, use the static Arrays.toString methods. That will print arrays just like the default collection implementations are printed.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic