• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to print reference of String Object ?

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Mark Vedder
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Gaurav Chauhan
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic