(a) State the difference between == operator and equals() method.
Answers
Answered by
3
Ans. == compares if the two objects being compared refer to the instance while equals() method which can be overridden by classes generally compares if the contents of the objects are equals.
Answered by
10
== operator - it is the equality operator
- checks if both the object references refer to the same memory location.e.g. if(a==b)it means it verifies whether both a and b refers to the same location in memory.
equals()-it is for content comparison
-compare the value.e.g String a="home";String b="home";String c="sweet";
if(a.equals(b))returns true because both contain the same value.
if(a.equals(c))returns false because a and c contents are different.
- checks if both the object references refer to the same memory location.e.g. if(a==b)it means it verifies whether both a and b refers to the same location in memory.
equals()-it is for content comparison
-compare the value.e.g String a="home";String b="home";String c="sweet";
if(a.equals(b))returns true because both contain the same value.
if(a.equals(c))returns false because a and c contents are different.
Similar questions