This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes toString( ) method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "toString( ) method" Watch "toString( ) method" New topic
Author

toString( ) method

Lalit mishra
Ranch Hand

Joined: Sep 01, 2005
Posts: 99
Byte b1 = new Byte("127");

if(b1.toString()==b1.toString())
System.out.println("True");
else
System.out.println("false");


How do this returns false???
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

This has something to do with the String in the pool and String not in the pool thing. Check this out...

System.out.println( new String("blah") == new String ("blah") );
System.out.println( "blah" == "blah" );

The second statement is in the pool. So it prints true. You will not find these types of questions on the exam for sure ! You dont have to know the intricacies of such stuff. All you have to know is that...

* The out reference's println method in the System class will use the toString() method of an object when it is called.

* Strings have pools and will exhibit the behaviour shown in those two code fragments.
[ April 24, 2006: Message edited by: John Meyers ]

SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
samir Sadiki
Ranch Hand

Joined: Apr 22, 2006
Posts: 31
If you replace:
if(b1.toString()==(b1.toString()))
by
if(b1.toString().equals(b1.toString()))
it will return true. So, I think you actually have two different Strings but with the same value.
I hope I made some sens lol.
Jeroen T Wenting
Ranch Hand

Joined: Apr 21, 2006
Posts: 1847
== when executed on references compares those references, which effectively are memory addresses.
It doesn't compare the actual content of those memory addresses.

When you executed it on the results of 2 calls to toString() on the same reference, you were actually executing it on 2 different references.

You might as well have asked "if (1 == 2) { " for all the good that would do.

Indeed the ONLY way you could get a true result out of that when comparing object references is when comparing 2 identical string literals, as those could actually refer to the same reference in the string constant pool.
But even then the result isn't guaranteed to yield true (though it in practice usually does).
But even when executing 'if ("Bill Door" == ("Bill" + " Door")) { ' you're not guaranteed to get a true result.


42
Mohammed Yousuff
Ranch Hand

Joined: Apr 18, 2006
Posts: 45
Hi, Guys . i saw the Question. i Worked and i surprise to see the ans is false. then i started to learn whats really happing behind the Screen.



Steps :

* We are calling Byte.toString() method

* then it calls Integer,toString() method


* in that it check for the radix and other stupid stuffs

* Finally it creates New String Object ()

* So every time the toString method will create a brand new Object


* So that only u r getting as False ..



BASE LINE : the base Line is when u create a String Object using new String() then u will have new Object with UNIQUE Memory Address. that it Boys ...


Regards<br /> <br />Mohammed Yousuff M N <br /> <br />Try NOT to Become a man of SUCCESS, BUT Try to Become a man of VALUE..
Mohammed Yousuff
Ranch Hand

Joined: Apr 18, 2006
Posts: 45
Originally posted by lalit mishra:
Byte b1 = new Byte("127");

if(b1.toString()==b1.toString())
System.out.println("True");
else
System.out.println("false");


How do this returns false???


Hi, Guys . i saw the Question. i Worked and i surprise to see the ans is false. then i started to learn whats really happing behind the Screen.



Steps :

* We are calling Byte.toString() method

* then it calls Integer,toString() method


* in that it check for the radix and other stupid stuffs

* Finally it creates New String Object ()

* So every time the toString method will create a brand new Object


* So that only u r getting as False ..



BASE LINE : the base Line is when u create a String Object using new String() then u will have new Object with UNIQUE Memory Address. that it Boys ...
 
 
subject: toString( ) method