Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

toString Question

 
Fes D Gaur
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1: Byte b1 = new Byte("127");
2:
3: if(b1.toString() == b1.toString())
4: System.out.println("True");
5: else
6: System.out.println("False");

The output is False. Why?

Thanks,
Fes
 
bhavesh bhanushali
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the following code will reflect some light on the issue

public class ByteToString
{
public static void main ( String args[] )
{
Byte b1 = new Byte ( "127" ) ;

if ( b1.toString ( ) == b1.toString ( ) ) // ( 1 )
System.out.println ( " Yes the == returns true " ) ;
else
System.out.println ( " Yes the == returns false " ) ;

if ( b1.toString ( ).equals ( b1.toString ( ) ) ) // ( 2 )
System.out.println ( " Yes the == returns true " ) ;
else
System.out.println ( " Yes the == returns false " ) ;

}
}

---------------------------------------------------------------------------
at ( 1 ) , the addresses are compared which are different for b1.toString ( ) and b1.toString .

whereas at ( 2 ) the values that the strings have are compared which are equal

so , in a sense the values are the same but the addressses are different
others please shed some light on the issue

regards ,
Bhavesh
 
Amin Mohammed-Coleman
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by bhavesh bhanushali:
the following code will reflect some light on the issue

public class ByteToString
{
public static void main ( String args[] )
{
Byte b1 = new Byte ( "127" ) ;

if ( b1.toString ( ) == b1.toString ( ) ) // ( 1 )
System.out.println ( " Yes the == returns true " ) ;
else
System.out.println ( " Yes the == returns false " ) ;

if ( b1.toString ( ).equals ( b1.toString ( ) ) ) // ( 2 )
System.out.println ( " Yes the == returns true " ) ;
else
System.out.println ( " Yes the == returns false " ) ;

}
}

---------------------------------------------------------------------------
at ( 1 ) , the addresses are compared which are different for b1.toString ( ) and b1.toString .

whereas at ( 2 ) the values that the strings have are compared which are equal

so , in a sense the values are the same but the addressses are different
others please shed some light on the issue

regards ,
Bhavesh



That is correct. if you want to compare the contents then use the equals() method, whereas the == operator checks if variables are referring to the same object. So in the example b1.toString() creates one String object and the second b1.toString() creates another String object. Therefore the variables are not refering to the same object. The below example demonstrate how to get two variables refering to the same object.

So if you wanted to check the contents of temp against temp2 then you would do the following:


Hope that helps.
 
ritwik roy
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just try this.
String s1="abc";
String s2="abc";
here both (s1==s2) and (s1.equals(s2)) returns true.
(s1.equals(s2)) is true as both s1 and s2 contains the same value.
(s1==s2)is true due to the internal mechanism of java. In java two String containing the same value are referenced to the same object to save space in the internal String table.

but in the following case
String s1="abc";
String s2= new String("abc");
(s1.equals(s2)) returns true as both contains the same value and
(s1==s2) returns false as here we are forcing the compiler to allocate new memory space to s2.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As a previous post mentioned the two String objects returned by Byte.toString are different.

As quick look at the Byte AP tells you this immediately:
Returns a new String object representing the specified byte.
 
Prasanta Chinara
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As per the language specification:
 
Prasanta Chinara
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
'apologize for the previous error posting.

As per the language specification:
Literal strings and strings computed by constant expressions refer to the same String object.

Following is the example from the Java lang spec:
String hello = "Hello", lo = "lo";

System.out.print((hello == "Hello") + " "); //----> PRINTS TRUE
System.out.print((hello == ("Hel"+"lo")) + " "); //----> PRINTS TRUE
System.out.print((hello == ("Hel"+lo)) + " "); //----> PRINTS FALSE

The expression "hel" + lo in the 3rd statement can't be determined by compier to be a constant. lo is a variable and could have been assigned any other value before line 3 -- compiler can never detemine this. Hence the 3rd statement prints false.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic