Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

toString( ) method

 
Lalit mishra
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
samir Sadiki
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1847
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
== 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.
 
Mohammed Yousuff
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ...
 
Mohammed Yousuff
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic