Java question - Printing objects

Status
Not open for further replies.
Check out the API documentation for the class Object (you know, the root of the class tree :)) The toString documentation and hashCode documentation are of particular interest.

It appears that for object that do not have a toString method, the toString method returns the object name followed by the @ and then the unsigned hex representation of the result of hashCode.

Looking at the documentation for hashcode it appears that in most implementations the hashCode returns the address of the object. But it is not required. Something to be aware of.

So you should be able to do something along the lines of:

Code:
System.out.println(objName.hashCode());

hth

gen
 

R2P2

Ars Praefectus
3,783
Subscriptor
Casting to Object like bishnu showed won't work; Java still resolves the object to its real class to choose the toString to call. But, casting super to Object will work. (Object)super.toString() will call toString() in Object, but super.toString() wouldn't give you what you wanted if a class on the hierarchy between your class and Object overrode toString(). I actually tested this, so I know of what I speak. :)
 
Simply using super to get the correct stuff from toString is definitely not always going to result in the correct result. As was said before, if it was any class between the current and Object that overrode toString, then you aren't going to get around it.

One method could be something like the following !![BOLD]Warning, untested code follows[/BOLD]

Code:
SomeClass sc = new SomeClass();

Object o = (Object) sc;

while (!(o instanceof Object)) {
    o = o.super();
}

System.out.println(o);

Of course I'm not sure if you can use super from outside of the class like that, nor am I certain the casting will work right for using instanceof.
 

Midlands

Ars Praetorian
535
Subscriptor
Originally posted by generator:
One method could be something like the following !![BOLD]Warning, untested code follows[/BOLD]

Code:
SomeClass sc = new SomeClass();

Object o = (Object) sc;

while (!(o instanceof Object)) {
    o = o.super();
}

System.out.println(o);

Of course I'm not sure if you can use super from outside of the class like that, nor am I certain the casting will work right for using instanceof.

Nope, it won't work. "o instanceof Object" is always true in Java for any object reference you have (not sure about null, but that's beside the point anyway). What you mean is "o.getClass().equals(Object.class)". And "super" is not a fake method, but a fake member that is only accessible from inside the class anyway.

But, in any case, none of this will work anyway. Java maintains protection and security guarantees that ensure that if you call a method with a given signature on an object of a particular class, you will call the most-specific method (overridden in the 'lowest' class on the inheritance tree). If this were otherwise, you could, as a normal client of a class, muck up its internal representation in general and blow it to pieces.

Even reflection, AFAIK, won't get you what you want. You'll still end up calling the most-derived toString() method.

System.identityHashCode() is, however, for the problem in question, exactly what you want.
 
Status
Not open for further replies.