This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes == and equals comparison? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "== and equals comparison?" Watch "== and equals comparison?" New topic
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.
     
    I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
     
    subject: == and equals comparison?
     
    Similar Threads
    Question on Strings and String pool
    equals method
    Confusion in the behaviour of null
    booleanValue()--help needed-exam in 2 days
    Equality Comparison Operators