File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes How to print reference of String Object ? 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 "How to print reference of String Object ?" Watch "How to print reference of String Object ?" New topic
Author

How to print reference of String Object ?

Gaurav Chauhan
Greenhorn

Joined: Apr 14, 2008
Posts: 27
Hi members,

We know that whenever we try to print the reference variable of any class , the toString() method from current class or it's parent class is executed.



It happens to the reference variable of all classes. But today when i created a reference of String class and tried printing the same i got the value as output not the Address. When i checked the String class source code i came to know that String class has overridden the toString() method which it inherited from Object class. The code is below:



So if i try the program given below i get Hello as output.


In this case when i use System.out.println(ob) then the toString() method from String Class is executed whose code is given above. The toString() method of String class just returns the reference of String Class so i must get the reference id as output. But i am not getting it, i get the value.


I hope you guys have got my question . I have been thinking on it from morning and my mind is stuck so i might not be able to define my problem in a proper way but try to get my perspective and reply me.


Don't tell me there is nothing beyond sky, There are footprints on moon.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37941
    
  22
Finding the location of a reference to an object is not something you do in Java. You can get the memory address from the hashCode method of java.lang.Object, but it is overridden in most classes so as to hide that information.

In fact it doesn't count as object-oriented programming if you try to find a memory location like that.

I am sure there are ways, but they are not straightforward, and anyway, the garbage collector reserves the right to move all objects after collecting garbage.
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

Originally posted by Gaurav Chauhan:
But today when i created a reference of String class and tried printing the same i got the value as output not the Address.


The (default) toString() implementation in the Object class does not print out the memory address. The use of the at ('@') annotation in the output -- "java.lang.Object@9304b1" -- might make one think this. The value after the '@' is the object's hashcode in hexadecimal. This is discussed in the javadoc for the Object's toString() method.

If we look at the toString() implementation in the Object Class, it is simply:



So if we wanted to mimic that for any object, even if it overrides the toString() method, we could write a simple utility to do so:


[ September 01, 2008: Message edited by: Mark Vedder ]
Gaurav Chauhan
Greenhorn

Joined: Apr 14, 2008
Posts: 27
My dear friend you said that hashCode() method is overridden in most classes. Will you please tell me the name of any class that does it ?

Even in java.lang.Object what i got is this ?

for toString() method


When i tried to look at the definition of hashCode() method, i didn't got any it is native ?




Now i can't see any class overriding hashCode() however toString() method is overridden and that is executed whenever we print any object.



@ Mark Vedder

I do know that hashCode() method is executed to get the memory value and toString() method just returns the same in a String Format.

Your code works fine but my friend it doesn't serves my purpose.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Many, many classes override hashCode(). Since you're already looking at the source for java.lang.String, I'm surprised you didn't notice that String does so.

Anyway, you're thinking much too hard here. toString() returns a String, right? And so String::toString() returns itself. Whenever you print an object, toString() is called to return a printable String. String::toString() just happens to have a handy one available already!


[Jess in Action][AskingGoodQuestions]
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

Originally posted by Gaurav Chauhan:
Will you please tell me the name of any class that does it ?


There are hundreds of classes in the Java API that do such; and thousands in other APIs. If you look at the H index page of the Java API, you will see entries for some of the hashCode() methods that are overridden in the Java API (the ones that provide additional JavaDoc information).

Originally posted by Gaurav Chauhan:

When i tried to look at the definition of hashCode() method, i didn't got any it is native ?





In the Object class, it uses a native call. But most other classes do not use native calls. For example, the hashCode method of the java.util.Date class is this:



For information on writing hashCode methods, you can look at the article Java theory and practice: Hashing it out and the wikipedia article Java hashCode().

Originally posted by Gaurav Chauhan:
...it doesn't serves my purpose.


I must have misunderstood your purpose. If you can explain in a bit more detail what it is you are trying to accomplish, we might be able to provide more relevant answers.
[ September 01, 2008: Message edited by: Mark Vedder ]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18508
    
  40

The toString() method of String class just returns the reference of String Class so i must get the reference id as output. But i am not getting it, i get the value.


First question, when has Java ever give you a "reference id" when you return an reference of String class? Second question, what the heck is a "reference id"?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Gaurav Chauhan
Greenhorn

Joined: Apr 14, 2008
Posts: 27
Originally posted by Ernest Friedman-Hill:
Many, many classes override hashCode(). Since you're already looking at the source for java.lang.String, I'm surprised you didn't notice that String does so.


Thanks for letting me aware of this i just checked out the String class and saw that hashcode() has been overridden.


Anyway, you're thinking much too hard here. toString() returns a String, right? And so String::toString() returns itself. Whenever you print an object, toString() is called to return a printable String. String::toString() just happens to have a handy one available already!


I agree that toString() return a String. I agree with "so String::toString() returns itself."

But it is still unclear "String::toString() just happens to have a handy one available already!"


@ Mark
Thanks for informing about the hashCode() Overridden implementation in different classes.


Now all discussion is going well but i again re-iterate my query.

Is there any way to print the reference-id of object of String class as we does with objects of other classes ?


First question, when has Java ever give you a "reference id" when you return an reference of String class? Second question, what the heck is a "reference id"?

Henry


Java has never given me the reference-id for String Class and this is my question.

Reference-id: It is a unique number assigned to objects. http://www.nomachetejuggling.com/2008/06/04/getting-a-java-objects-reference-id/
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18508
    
  40

If you mean the value of the Object classes' hashCode() method, you can use the identityHashCode() method. This method returns the same value as the original hashCode method -- and can still be accessed, as it doesn't get overridden like the hashCode() method.

BTW, I don't think hashcode (even the identity hashcode) is guarranteed to be unique.

Henry
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Well, two things. First of all, I would advise you never to read that blog again. There's no such thing as a "reference id", nor is it a useful term for the blogger to have invented. There are a number of false or confused statements in that article, and you'd do yourself a service by simply forgetting you ever looked at it.

But secondly, the blogger actually answers the question you're asking, down at the end of the article! To get the object's identity hashcode (which the blogger has renamed to "reference id"), you can use the identityHashcode method in the System class, and if you want to see it as a hexadecimal value, there's a handy method in Integer that can do that, too:

Integer.toHexString(System.identityHashCode(object))

The identity hash code is the value that Object.hashCode() would return for an object if hashCode() weren't overridden.
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

Originally posted by Gaurav Chauhan:


Java has never given me the reference-id for String Class and this is my question.

Reference-id: It is a unique number assigned to objects. http://www.nomachetejuggling.com/2008/06/04/getting-a-java-objects-reference-id/


I believe the author of that post is misapplying or "inventing" a term. I have never heard of a reference ID. And based on Henry's post, I suspect neither has he. If you search the Java Language Specification, no where in it does the term "reference ID" exist.

As clearly stated above, in the javadoc of Object.toString(), it returns the object's name, an '@' symbol, and the hashcode. It does not return a "reference ID". I think the author of the post you reference is using the term "reference ID" as an easy way to say "the object's name, an '@' symbol, and the hashcode".
Gaurav Chauhan
Greenhorn

Joined: Apr 14, 2008
Posts: 27
Well my teacher uses this term very frequently that's why i used it too ? But based on above justification by senior members i would better use "hashcode" in place of ref-id.

Thanks again from stopping me from misleading myself.

The above discussion prompted me to do write some code and this is what i did ?


The above code prints the hashcode for string object ...... (This was the main question i was asking). Thanks for telling me the identityHashCode() method.


One more thing that i proved here is that String objects are immutable. In above case first 2 values of "sob" are same so their hashcode is same too but third value is different so a different memory location has been assigned to it.

Thanks a lot Ranchers. I got my answer.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18508
    
  40

One more thing that i proved here is that String objects are immutable. In above case first 2 values of "sob" are same so their hashcode is same too but third value is different so a different memory location has been assigned to it.


Just thought I would clarify -- the identity hashcode is *not* related to the memory location. It may have been related when it is first generated... but keep in mind, the identity hashcode can never change, while the object may be moved many times in memory due to the garbage collector.

You can't use the identity hashcode to determine the memory location. You can't even use it to determine if two objects are the same object with it. You can determine that they are different though.

Henry
Gaurav Chauhan
Greenhorn

Joined: Apr 14, 2008
Posts: 27
So it means the identityHashCode return a number which is used to refer to object in memory ?

The object location can be changed by GC but the identity hashcode remains same if once assigned.

Another thing identity HashCode doesn't gives us the memory location.

Thanks for clearing the doubt.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to print reference of String Object ?
 
Similar Threads
How does return type "boolean" affect print statement in a method ?
List's contains(Object o) method doesn't work for object version.
Code output -Doubt.
Garbage collection doubt
what is the result and why?please help me!