All classes in Java are child classes of the Object class that you can find in java.lang package in rt.jar. If you open the Object class, you will see that it has many methods and one of the very important ones that we are going to discuss in this tutorial is the equals(Object obj) method.
Let's open the java.lang.Object class and look at the default implementation of the equals method.
Default implementation of equals method in Object class:public boolean equals(Object obj) {
return (this == obj);
}
As you can see, the default implementation uses double equals-to == comparison of the current object and the object passed on to the equals method. == does the address comparison
Example without overriding equals method:
public class EqualsExampleJava {
public static void main(String[] args) {
Employee employee1 = new Employee(1, "Sam");
Employee employee2 = employee1; //same reference
Employee employeeDup = new Employee(1, "Sam");
System.out.println("employee1 == employee2 => " + (employee1 == employee2));
System.out.println("employee1.equals(employee2) => " + employee1.equals(employee2));
System.out.println("employee1 == employeeDup => " + (employee1 == employeeDup));
System.out.println("employee1.equals(employeeDup) => " + employee1.equals(employeeDup));
}
}
class Employee {
int employeeId;
String employeeName;
Employee(int employeeId, String employeeName) {
this.employeeId = employeeId;
this.employeeName = employeeName;
}
}
Output:
employee1 == employee2 => true
employee1.equals(employee2) => true
employee1 == employeeDup => false
employee1.equals(employeeDup) => false
You will get the same results when comparing two objects using == and equals methods if you do not override the equals method.
✏️ Always compare two objects (created using the new keyword) using the equals(Object obj) method and by overriding it in your class.
In our example, we say that two employees are the same if their name and id are the same,
Example: override equals method@Override
public boolean equals(Object obj) {
//If object is null then they are not same
if(obj == null) return false;
//If object is have the same reference they are same
if(obj == this) return true;
Employee employee = (Employee) obj;
return this.employeeId == employee.employeeId &&
this.employeeName.equals(employee.employeeName);
}
Output:
employee1 == employee2 => true
employee1.equals(employee2) => true
employee1 == employeeDup => false
employee1.equals(employeeDup) => true
- Java equals method - Tutorial
- Unbound classpath container: JRE System Library [JavaSE-1.7]
- Spring Boot: @RequestBody not applicable to method
- Java 8: Steam map with Code Examples
- Java Program: Random Number Generator
- Java java.time.Clock class code examples [Java Date Time API]
- Fix: type argument is not within bounds of type-variable T
- [Fix] java.net.MalformedURLException: unknown protocol
- Java 7 addSuppression() and getSuppression() Exception Handling
- Convert Java Array to ArrayList Code Example
- How to Word-Warp Console logs in IntelliJ
- Ways Compare Dates in Java Programming with Examples
- Remove Trailing zeros BigDecimal Java
- CRUD operations in Spring Boot + JDBC
- [Java Threads] Should we extend Thread Class or implement Runnable interface
- Json Serialization and Deserialization using Java Jackson
- Create simple struts2 project using maven commands
- How to install Java OpenJDK 11 on Alpine Linux
- Unsupported major.minor version 52.0 in java
- Error: Can not find the tag library descriptor for
- Java: Convert String to Binary
- How to run Java Unit Test cases with Apache Maven?
- Java: Testing Private Methods in JUnit using reflection API Example
- Java JDBC Connection with MySQL Driver in VS Code + Troubleshooting
- Java Join Strings with Comma Separator
- Fix: python3: Library/Developer/CommandLineTools/usr/bin/python3: No module named notebook (jupyter) - Python
- How to Get File Size using Python in Bytes/KB/MB or GB - Python
- Eclipse : This version of the rendering library is more recent than your version of ADT plug-in. Please update ADT plug-in - Eclipse
- Possible outages message Google Webmaster tool - Google
- The default username and password for RabbitMQ - HowTos
- How to stop and start a docker container - Docker
- Update All Outdated Modules/Packages using pip3 - PIP
- Chessboard with pieces using pure HTML and CSS - Html