Two Laptop Bag*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes strange results 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 "strange results" Watch "strange results" New topic
Author

strange results

kashif sohail
Greenhorn

Joined: Dec 04, 2000
Posts: 29
public class t
{
public static void main (String []args)
{
Byte b1 = new Byte("127");

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

strange behaviour
result true if 1 is commented and 2 is used result is false
any expalination
plz
Val Dra
Ranch Hand

Joined: Jan 26, 2001
Posts: 439
if (b1==b1) // l
//if(b1.toString() == b1.toString()) //2
System.out.println("True");
else
System.out.println("False");
}
}
There is nothing wrong with this, first you compare to bytes , their values which are equal. But when you make it a string using toString() method you comparing references not values and they are not the same. I hope thishelps you.


Val SCJP <BR>going for SCJD
Anshuman Acharya
Ranch Hand

Joined: Jan 19, 2001
Posts: 144
no val, that's not the way it works.
== as we know compares object references. b1 == b1 returns true coz they are the same reference.
however, b1.toString() creates a new String reference pointing to a String object holding the string representation of b1. so when we are saying
b1.toString() == b1.toString() , we are creating 2 new String references and then asking if they are the same reference. well, obviously not!
if we had used the equals method which just compares the content, the return would have been true.
Clear?
Val Dra
Ranch Hand

Joined: Jan 26, 2001
Posts: 439
That's exactly what i meant , i mean that b1 == b2 because they are the same type but everyone knows that Strings are final so whenver you make an assignment to a string it crates a new one , and the method toString() in that case creates a new string for both b1 and b2 so they are now references and when you == on them they will return false because it compares hashCodes not the values.
vikas singh
Greenhorn

Joined: Jan 22, 2001
Posts: 23
anybody could suggest me :
System.out.println("hello" == "hello"); // o/p - true
than why b1.toString(), which is a string, is not
equals to another string - b1.toString().
taking the consideration of the above comparision of
two "hello" strings . . .
thanks
vikas Singh.
Val Dra
Ranch Hand

Joined: Jan 26, 2001
Posts: 439
System.out.println("hello" == "hello"); // o/p - true
because both of them reference same object String.
look here
String s = "Good";
String f = "Good";
System.out.println(s == f);
see 2 different string objects right ? But they both reference same object value.
If you would have said
String s = new String("Good");
string f = "Good";
System.out.println( s == f) // false
They point to different objects now.
Tualha Khan
Ranch Hand

Joined: Nov 22, 2000
Posts: 287
String s1="hello";
String s2="hello";
s1==s2 // true
See, here we are comparing two String LITERALS, and checking whether they have the same value namely "hello". the result will be true.
Actually, as far as I know, here the concept of String pool is used. The concept is that a String literal is created only once in the pool even if multiple variables are made with the literal value.
A String literal like "hello" is kept in a pool and any other variable which also have a String literal like "hello" is made, then they both share the same String literal. So here, s1 and s2 are both using the same String literal from the String pool : "hello". Hence the result is true.
*******************
String s1=new String("hello");
String s2=new String("hello");
s1==s2 //false
String s3=s2;
s2==s3 // true (think hard, why this is true??)
Now, here in the second code, we are trying to compare two String OBJECTS. Objects are stored in memory locations, and each object has a unique memory address. The == operator tries to check whether the memory address of two String OBJECTS is same or not, which in our case is NOT SAME. So the result is false. However, if we use s1.equals(s2), then the result will be true since this way, we are checking whether the value "hello" is same in both the objects.
Hope this explains.
Bye,
Tualha Khan

SCJP2, BEA WLS 6.0, DB2 UDB 7.1
Anshuman Acharya
Ranch Hand

Joined: Jan 19, 2001
Posts: 144
just check this earlier discussion on a very related topic:
http://www.javaranch.com/maha/Discussions/java_lang_Package/String_literals_-_JavaRanch_Big_Moose_Saloon.htm
 
wood burning stoves
 
subject: strange results
 
Similar Threads
toString Question
comparison
Byte - toString() function
Interesting QUestion
Understanding Byte and == Op