• 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

valueOf( ) and toString( )

 
Ranch Hand
Posts: 380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am confused about when do api calls such as valueOf() and toString() return same or different instances of String type.

Lets see this code snippet:








Can some one summarize and tell me what are the rules - of returning same of different instances of the type when such method calls are invoked?

Thanks!!
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First, the second example isn't about String instances but Boolean values.

And the third example is an example of what happens when you have a String literal in code and the String pool that is created with these values, so that any other String that gets "Created" in code will just use the same instance that is in the pool. That is why that returns true.

The first is definitely two different String instances being created. That is also why when comparing String values in your code always always use .equals()

Mark
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First example.. returns true.. when i ran it
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Shrikant Pandhare:
First example.. returns true.. when i ran it



Which JDK did you use. I would think that it is possible to be true if you are usin JDK 5.0 because of boxing and unboxing. Or maybe not.

Mark
 
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

I am confused about when do api calls such as valueOf() and toString() return same or different instances of String type.

It may vary based on the implementation of the toString() or valueOf() method. I would be surprised if such a topic appeared on the exam.
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
toString() Returns String Object Not The Value.
valueOf() Returns The Value Not The String Object.

Integer i = new Integer(4);
System.out.println(i.toString()==i.toString());// prints false

Explanation : Here To String Returns Two Diffrent Strings Though We Apply It On Same Object (As Function Is Called Two Diffrent Times). And We Know That Two Diffrent Strings == Comparison Is Always False.
----------------------------------------------------------------------------Boolean b1 = Boolean.valueOf("trUE");
Boolean b11 = Boolean.valueOf(true);// prints true.

Explanation : Here valueOf Function Returns "true" Hence true Is Printed
----------------------------------------------------------------------------
String s1 = "S1";
String s2 = s1.toString();
System.out.print(s1==s2); //Prints true.

Here Is Confusion For Me.
We Are Applying toString() Method On String Object. Which Obviously Returns A New String That Is Like s1 But Not s1. Also As It Is Not The Same String Objects Why It Returns true. I Have To Check On JVM.

Please Give Me Any Explanation. Also Correct Me When ever I M Wrong
Thanks.
 
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

String s1 = "S1";
String s2 = s1.toString();
System.out.print(s1==s2); //Prints true.

Which Obviously Returns A New String That Is Like s1 But Not s1.


No; s1.toString() returns a reference to the same String object. Both s1 and s2 point to the same String on the heap.
 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


This is going to sound bizarre (it confused me at first), but the two references returned by the call to toString() really ARE the same, but only for numbers between -3 and 10 inclusive. This is due the weird way Sun wrote the toString() method in the Integer class. You can look in the source to java.lang.Integer to see what I'm talking about if you're curious.

We had a discussion about this last week:
Wrappers
[ August 29, 2005: Message edited by: Ryan Kade ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic