Reputation: 56199
I have strange bug in my code. I have variable type and when I load that from class Settings ( implements persistable ) it has value "CPU 21" (type="CPU 21"), but when I try if(type=="CPU 21") the condition is false. How is that possible ?
Upvotes: 1
Views: 1782
Reputation: 240900
you need to use equals();
to compare String values
replace
if(type=="CPU 21")
with
if("CPU 21".equals(type))
== will compare two reference variable. while equals will check the object's equality Also See
Upvotes: 3
Reputation: 14568
What is difference between equals() and == ?
Explanation : 1
They both differ very much in their significance. equals() method is present in the java.lang.Object class and it is expected to check for the equivalence of the state of objects! That means, the contents of the objects. Whereas the '==' operator is expected to check the actual object instances are same or not.
For example, lets say, you have two String objects and they are being pointed by two different reference variables s1 and s2.
s1 = new String("abc");
s2 = new String("abc");
Now, if you use the "equals()" method to check for their equivalence as
if(s1.equals(s2))
System.out.println("s1.equals(s2) is TRUE");
else
System.out.println("s1.equals(s2) is FALSE");
You will get the output as TRUE as the 'equals()' method check for the content equivality.
Lets check the '==' operator..
if(s1==s2)
System.out.printlln("s1==s2 is TRUE");
else
System.out.println("s1==s2 is FALSE");
Now you will get the FALSE as output because both s1 and s2 are pointing to two different objects even though both of them share the same string content. It is because of 'new String()' everytime a new object is created.
Try running the program without 'new String' and just with
String s1 = "abc";
String s2 = "abc";
You will get TRUE for both the tests.
Explanation : 2
By definintion, the objects are all created on the heap. When you create an object, say,
Object ob1 = new SomeObject();
Object ob2 = new SomeObject();
We have 2 objects with exactly the same contents, lets assume. We also have 2 references, lets say ob1 is at address 0x1234 and ob2 is at address 0x2345. Though the contents of the objects are the same, the references differ.
Using == compares the references. Though the objects, ob1 and ob2 are same internally, they differ on using this operation as we comare references. ob1 at address 0x1234 is compared with ob2 at address 0x2345. Hence, this comparison would fail.
object.equals() on the other hand compares the values. Hence, the comparison between ob1 and ob2 would pass. Note that the equals method should be explicitly overridden for this comparison to succeed.
Upvotes: 3
Reputation: 1500525
It's not a bug at all. It's the way that == works.
For reference types (such as string), "==" will always compare the references directly. If you have two references which refer to separate objects with equal content, then "==" will evaluate to false. Use equals
for value equality.
Note that it's easy to fool yourself by using String literals, which are interned:
String x = "CPU 21";
String y = "CPU 21";
boolean b = (x == y);
Now b
is true because the values of x
and y
- the references are actually the same. They're referreing to the same String object. Compare that with this:
String x = new String("CPU 21");
String y = new String("CPU 21");
boolean b1 = (x == y);
boolean b2 = x.equals(y);
Now b1
is false because x
and y
refer to distinct String
objects, but b2
is true because String.equals(String)
compares the contents of the strings in question (i.e. the character sequences represented by the strings).
Upvotes: 5
Reputation: 993095
You have to use .equals()
to compare string content:
if (type.equals("CPU 21")) ...
This is because the ==
operator only compares two references to see whether they are pointing to the same object or not. Since your string type
is not the same object as the literal "CPU 21"
, the comparison is false. The .equals()
method actually checks the strings themselves.
You may also find that people often write this the other way around:
if ("CPU 21".equals(type)) ...
This means the same thing, but will not throw an exception if type
happens to be null
.
Upvotes: 4