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
== 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
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.
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
== 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
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:
- Star Pattern Program in java
- Can we call run() directly on thread without calling start?
- Class variable vs instance variable
- iterator vs list iterator
- Why String is immutable in java?
- What is marker interface?
thanks
ReplyDeleteWelcome Nicolas. Glad that you liked the article
Delete1]what is difference between Object class public boolean equals(java.lang.Object);method and String class public boolean equals(java.lang.Object);
ReplyDelete2] what is difference between == operator and Object class public boolean equals(java.lang.Object);method.......
please give at least 2nd question answer
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.
ReplyDeletejava.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.