This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes toString Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "toString Question" Watch "toString Question" New topic
Author

toString Question

Fes D Gaur
Ranch Hand

Joined: Apr 29, 2005
Posts: 54
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

Joined: Jun 13, 2005
Posts: 55
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

Joined: Oct 18, 2004
Posts: 15
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

Joined: May 13, 2005
Posts: 20
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

Joined: Aug 03, 2002
Posts: 7729
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.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Prasanta Chinara
Ranch Hand

Joined: Jul 20, 2005
Posts: 30
As per the language specification:
Prasanta Chinara
Ranch Hand

Joined: Jul 20, 2005
Posts: 30
'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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: toString Question
 
Similar Threads
Interesting Question
* Byte == comparision
byteRef.toString() == byteRef.toString() ?
toString( ) method
Why is " == " on different strings with same content is returning "false"