Wednesday 9 March 2016

Difference between == and equals() method in java

Believe me or not...
This is one of the most frequently asked java interview question. Apart from that you will use either of them while coding number of times.
So lets take a walk through to the difference between the two.

First thing that you should be aware of is that both are used to compare two objects. But this question is generally asked in relation to String because each of them when used to compare String Objects behaves differently.

= = operator


Java tutorial by java radar






















== operator checks if two object refers to same memory location or not. That is if two object refers to same memory location then == operator will evaluate to true. (Here you need to have clear idea about how String object are created, stored and assigned to variables...)
Let's try to analyze above program.
1) First, we have three int variables (primitive type) a, b and c. Here you can easily figure out that since a and c has same value so if(a==c) loop is getting executed. Simple!
2)Second, we have four String variable s1, s2 (commented), s3 and s4. Looking at the way String object God is assigned to different variables we can say that only s1 and s3 share same memory location (How? Click here to know).



equals() method

Java tutorial by java radar























equals() method checks if the content of two object are same or not. Like in above program both s1 and s3 object has same content, God. 
s2 also has GOD (but in upper case) thus is not absolutely same as God. If you compare s2 with either s1 or s3 using equals then equals will return false. If you wish to ignore the case difference and evaluate to true then use s1.equalsIgnoreCase(s2) instead of just equals(). 
equalsIgnoreCase just checks the content without being concerned about case of the letters forming word.


You may also like to read about:



4 comments:

  1. Replies
    1. Welcome Nicolas. Glad that you liked the article

      Delete
  2. 1]what is difference between Object class public boolean equals(java.lang.Object);method and String class public boolean equals(java.lang.Object);

    2] what is difference between == operator and Object class public boolean equals(java.lang.Object);method.......
    please give at least 2nd question answer

    ReplyDelete
  3. The java.lang.Boolean.equals(Object obj) returns true if and only if the argument is not null and is a Boolean object that represents the same boolean value as this object.


    java.lang.Object

    public class Object
    Class Object is the root of the class hierarchy. Every class has Object as a superclass. All objects, including arrays, implement the methods of this class.

    ReplyDelete

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
If you are looking for a reference book on java then we recommend you to go for → Java The Complete Reference
Click on the image link below to get it now.