• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to System.out.println the string reference

 
saxena vicky
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For Example:
String s = new String("xyz");
how can I print the object reference of String object s
 
Michael Lloyd Lee
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
saxena vicky
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
Steve Morrow
Ranch Hand
Posts: 657
Clojure Spring VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 1342
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's the generated byte code for the two ways to init a String reference:

 
Steve Morrow
Ranch Hand
Posts: 657
Clojure Spring VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic