Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

strange results

 
kashif sohail
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 439
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Anshuman Acharya
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 439
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 439
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Anshuman Acharya
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic