File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

valueOf( ) and toString( )

 
Shivani Chandna
Ranch Hand
Posts: 380
  • 0
  • Mark post as helpful
  • send pies
  • 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!!
 
Mark Spritzler
ranger
Sheriff
Posts: 17276
6
IntelliJ IDE Mac Spring
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Shrikant Pandhare
Greenhorn
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First example.. returns true.. when i ran it
 
Mark Spritzler
ranger
Sheriff
Posts: 17276
6
IntelliJ IDE Mac Spring
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Steve Morrow
Ranch Hand
Posts: 657
Clojure Spring VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Rushikesh Wagh
Ranch Hand
Posts: 58
  • 0
  • Mark post as helpful
  • send pies
  • 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
Clojure Spring VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Ryan Kade
Ranch Hand
Posts: 69
  • 0
  • Mark post as helpful
  • send pies
  • 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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic