| Author |
== and equals comparison?
|
Joe Harry
Ranch Hand
Joined: Sep 26, 2006
Posts: 8795
|
|
Hi Guys, I often get confused and give wrong answers in mock exams when it comes to == and equals questions. The sample code below is from whizlabs, Boolean b = new Boolean("True"); boolean b2 = true; Now the questions, if(b.booleanValue() == b1) if(b.booleanValue() == "true"); if(b.equals("True")); if(b == "true"); if(b1 == "true"); I'm terribly confused as to what principles govern the usage of == and equals operators. All I know is that equals method in Object class compares the references and it is overidden in the String class and Wrapper classes where the comparison for the equals method is made for the actual object. Now, I want to know under what circumstances will it show a compiler error and under what other circumstances will it print false. Please explain me about both the == and equals options. Thanks in advance.
|
SCJP 1.4, SCWCD 1.4 - Hints for you, SCBCD Hints - Demnachst, SCDJWS - Auch Demnachst
Did a rm -R / to find out that I lost my entire Linux installation!
|
 |
Sanjeev Singh
Ranch Hand
Joined: Nov 01, 2006
Posts: 381
|
|
Given
Boolean b = new Boolean("True"); boolean b1 = true;
b.booleanValue() == b1 will return true.The equality of two primitves having same value. b.booleanValue() == "true".Compilation fails as you can not compare a primitive with an Object.Being more specific == can be applied to compare primitives and for objects references in same inheritance hierarchy. b.equals("True").Compilation succeds as the overridded method equal() takes the Object.Comparision gives true only when the object passed is a Boolean. if(b == "true").Compilation fails.Reason being the avobe. if(b1 == "true").Compilation fails.Reason being the avobe. So based on the avobe disscussion here is what I concludes... == operator. Comarision of primitive with primitive (numbers)is OK except primitive with boolean.Comparsion of primitive with object refernce is a compialtion error.Comparion of object references is acceptible in same inheritance hierarchy else compilation error. equals(). The behavior depends on the class which overrides this method.
|
~Sanjeev Singh<br />SCJP 1.5
|
 |
Joe Harry
Ranch Hand
Joined: Sep 26, 2006
Posts: 8795
|
|
Sanjeev, As per your quote, if(b.booleanValue() == "true"); fails to compile reason as you say "true" is of type String?? Am I right?
|
 |
Prabhu Venkatachalam
Ranch Hand
Joined: Nov 16, 2005
Posts: 502
|
|
You are right. b.booleanValue() will return you the boolean primitive and not the String object. So, you will get compiler error, because you are comparing Object with the primitive.
|
Prabhu Venkatachalam<br />SCJP 1.4,SCWCD 1.4<br />prabhu.venkatachalam@gmail.com
|
 |
Sanjeev Singh
Ranch Hand
Joined: Nov 01, 2006
Posts: 381
|
|
Jothi writes... As per your quote, if(b.booleanValue() == "true"); fails to compile reason as you say "true" is of type String?? Am I right?
Yes exactly.
|
 |
Joe Harry
Ranch Hand
Joined: Sep 26, 2006
Posts: 8795
|
|
Sanjeev, Thanks for the help.
|
 |
 |
|
|
subject: == and equals comparison?
|
|
|