wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes using getclass on references Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "using getclass on references" Watch "using getclass on references" New topic
Author

using getclass on references

ronnir paterl
Greenhorn

Joined: Oct 18, 2007
Posts: 11
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
Sheriff

Joined: Sep 28, 2004
Posts: 18507
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18507
    
  40

.. 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

Joined: Jul 11, 2001
Posts: 14112
a) this is an array of *primitives* - there really isn't a true class name in this case.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: using getclass on references
 
Similar Threads
Objects 'Call by Reference' Instead of 'Call of Value'?
how to get the current Object reference name?
ClassCastException
An array question
Circular reference issue?