• 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 System.out.println the string reference

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For Example:
String s = new String("xyz");
how can I print the object reference of String object s
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Michael !
But the value return from System.identityHashCode(java.lang.Object), as Object.hascode() are different, why ?
 
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
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.)

 
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic