Forums Register Login

using getclass on references

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
 

.. 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
+Pie Number of slices to send: Send
a) this is an array of *primitives* - there really isn't a true class name in this case.
+Pie Number of slices to send: Send
 

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.
You don't know me, but I've been looking all over the world for. Thanks to the help from this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 739 times.
Similar Threads
An array question
Circular reference issue?
ClassCastException
how to get the current Object reference name?
Objects 'Call by Reference' Instead of 'Call of Value'?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 05:58:16.