aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes valueOf( ) and toString( ) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "valueOf( ) and toString( )" Watch "valueOf( ) and toString( )" New topic
Author

valueOf( ) and toString( )

Shivani Chandna
Ranch Hand

Joined: Sep 18, 2004
Posts: 380
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!!


/** Code speaks louder than words */
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Shrikant Pandhare
Greenhorn

Joined: Jul 29, 2005
Posts: 15
First example.. returns true.. when i ran it
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

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

Joined: May 22, 2003
Posts: 657

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

Joined: Jun 18, 2005
Posts: 58
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.


Best Regards,
Rushikesh Wagh
SCJP 1.4 (2005)
SCJP 1.6 (2010)
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

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

Joined: Aug 16, 2005
Posts: 69


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
 
subject: valueOf( ) and toString( )
 
Similar Threads
Explain the strange behavior of Boolean Class??
Equals and ==
boolean question
== AND EQUALS
equals() and ==