| Author |
How to System.out.println the string reference
|
saxena vicky
Greenhorn
Joined: Sep 05, 2005
Posts: 18
|
|
For Example: String s = new String("xyz"); how can I print the object reference of String object s
|
 |
Michael Lloyd Lee
Greenhorn
Joined: Sep 07, 2005
Posts: 22
|
|
You can not (excluding JNI). The best you could do is get the hashcode via System.identityHashCode(java.lang.Object), as Object.hascode() is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language. Why do you want it?
|
Please please please use code tags!<br /> <br /><a href="http://java.sun.com/j2se/1.5.0/docs/api/" target="_blank" rel="nofollow">Java API</a> - <a href="http://java.sun.com/docs/books/tutorial/index.html" target="_blank" rel="nofollow">Java Tutorials</a>
|
 |
saxena vicky
Greenhorn
Joined: Sep 05, 2005
Posts: 18
|
|
Thanks Michael ! But the value return from System.identityHashCode(java.lang.Object), as Object.hascode() are different, why ?
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24040
|
|
Read the Javadoc for String.hashCode().
Returns a hash code for this string. The hash code for a String object is computed as s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1] using int arithmetic, where s[i] is the ith character of the string, n is the length of the string, and ^ indicates exponentiation. (The hash value of the empty string is zero.)
|
[Jess in Action][AskingGoodQuestions]
|
 |
Steve Morrow
Ranch Hand
Joined: May 22, 2003
Posts: 657
|
|
String s = new String("xyz");
It's worth mentioning that this kind of construct is considered a poor programming practice due to the way String objects and literals work in Java (Strings are immutable and one should avoid creating unnecessary objects). The following is preferred:
|
 |
Michael Lloyd Lee
Greenhorn
Joined: Sep 07, 2005
Posts: 22
|
|
Originally posted by saxena vicky: Thanks Michael ! But the value return from System.identityHashCode( java.lang.Object), as Object.hascode() are different, why ?
Maybe I should of clarified that as java.lang.Object.hashCode(). They should return different values, as hashCode() is overridden by String (to the method shown above). Again, why do you want this?
|
 |
Norm Radder
Ranch Hand
Joined: Aug 10, 2005
Posts: 681
|
|
Here's the generated byte code for the two ways to init a String reference:
|
 |
Steve Morrow
Ranch Hand
Joined: May 22, 2003
Posts: 657
|
|
If I may be so bold as to add a comment on the bytecode... String s1 = "AAA"; ------------------------------------ 0 ldc #2 <AAA> 2 astore_1 ------------------------------------ String s2 = new String("BBB"); ------------------------------------ 3 new #3 <java/lang/String> 6 dup 7 ldc #4 <BBB> 9 invokespecial #5 <java/lang/String.<init>> 12 astore_2 ------------------------------------ 13 return
|
 |
 |
|
|
subject: How to System.out.println the string reference
|
|
|