In general both equals() and “==” operator in Java are used to compare objects to check equality but here are some of the differences between the two:
- Main difference between .equals() method and == operator is that one is method and other is operator.
- We can use == operators for reference comparison (address comparison) and .equals() method for content comparison. In simple words, == checks if both objects point to the same memory location whereas .equals() evaluates to the comparison of values in the objects.
- If a class does not override the equals method, then by default it uses equals(Object o) method of the closest parent class that has overridden this method. See this for detail
- Coding Example:
// Java program to understand
// the concept of == operator
String s1 =
String s2 =
System.out.println(s1 == s2);
Explanation: Here we are creating two objects namely s1 and s2.
- Both s1 and s2 refers to different objects.
- When we use == operator for s1 and s2 comparison then the result is false as both have different addresses in memory.
- Using equals, the result is true because its only comparing the values given in s1 and s2.
Let us understand both the operators in detail:
We can apply equality operators for every primitive types including boolean type. we can also apply equality operators for object types.
false false true true
If we apply == for object types then, there should be compatibility between arguments types (either child to parent or parent to child or same type). Otherwise we will get compile time error.
false false // error: incomparable types: Thread and String
In Java, string equals() method compares the two given strings based on the data/content of the string. If all the contents of both the strings are same then it returns true. If all characters are not matched then it returns false.
true false false false true
Explanation: Here we are using .equals method to check whether two objects contains the same data or not.
- In the above example, we are creating 3 Thread objects and 2 String objects.
- In the first comparison, we are checking that t1 == t3 or not. As we know that both t1 and t3 pointing to same object that’s why it returns true.
- In second comparison, we are using operator “==” for comparing the String Objects and not the contents of the objects. Here, both the objects are different and hence the outcome of this comparison is “False”
- When we are comparing 2 String objects by .equals() operator then we are checking that is both objects contains the same data or not.
- Both the objects contains the same String i.e. GEEKS that’s why it returns true.
This article is contributed by Bishal Kumar Dubey. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- AbstractSequentialList equals() method in Java with Example
- HashSet equals() method in Java with Example
- YearMonth equals() method in Java
- Map equals() method in Java with Examples
- ZoneOffsetTransition equals() method in Java with Example
- FieldPosition equals() method in Java with Example
- Vector equals() Method in Java
- Stack equals() method in Java with Example
- ZoneOffsetTransitionRule equals() method in Java with Example
- JapaneseDate equals() method in Java with Example
- MessageFormat equals() method in Java with Example
- StringWriter equals() method in Java with Example
- ParsePosition equals() method in Java with Example
- MinguoDate equals() method in Java with Example
- ConcurrentSkipListSet equals() method in Java
- ThaiBuddhistDate equals() method in Java with Example
- GregorianCalendar equals() Method in Java
- BigInteger equals() Method in Java
- RuleBasedCollator equals() method in Java with Example
- DecimalStyle equals() method in Java with Example
Improved By : UjasMehta