• 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

Strings Again.......

 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why does this code prints : False
class StringTest{
public static void main(String args[]){
Byte b1 = new Byte("121");

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

}
Method toString is overridden in class Byte and the API says
"It returns a String object representing this Byte's value"

Shouldn't then (b1.toString()) and (b1.toString()) reffer to same string in the String pool ?
Thanks
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To findout the answer, you will have to do some "code-autopsy"
The Byte.toString() method actually delegates the call to Integer.toString() method. The Integer.toString() method looks like this

Ignore all the code and look at only the last line marked in Red color.
Clearly each time toString() method is called, a new string object is returned. Hence your program is equivalent to

Since the two string objects are different objects on the heap( with same contents ), == operator which compares their reference return false.
Ajith
 
Rajiv Ranjan
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ajit,
That really helps........
In that case don't you think the API should say :
"Returns a new String object representing this Byte's value"

[This message has been edited by Rajiv Ranjan (edited November 14, 2000).]
 
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rajiv.
Let me add little more to Ajith's answer, whenever String object is created with 'new'keyword at runtime, compiler creates new String object rather than getting it from the pool.hence in this case
String s1 = b1.toString() ;
String s2 = b1.toString() ;
there are two String objects.You can put String objects in the pool by calling intern() method.as
String s1 = b1.toString().intern();
String s2 = b1.toString().intern();
Now the s1 & s1 refer to One String Object. You can run the below code to get the idea.
class StringTestOne
{
public static void main(String args[]){
Byte b1 = new Byte("121");

String s1 = b1.toString().intern() ;
String s2 = b1.toString().intern() ;

if( s1 == s2 )
System.out.println("True");
else
System.out.println("False");
}
}
(Special thanks to Ajith for getting my concepts clear on intern())
Regards.
------------------
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ajith,
Thank you very much
Shubhangi
 
reply
    Bookmark Topic Watch Topic
  • New Topic