equals( ) method is one of the Object Class methods. As all other Classes in Java are the sub classes of the Object Class, any Class can access this method using its object.
equals( ) method is used to compare the object with the other object. equals( ) method returns true if the objects are equal else it will result false.
Syntax of equals( ) method -
object1.equals(object2); //This will result true if object1 and object2 are equal else returns false
Lets implement this on Eclipse IDE as shown below -
1. Launch Eclipse IDE, create a Java Project 'Project 33' as shown below -
2. Create a Java Class 'ClassXYZ' with main( ) method as shown below -
3. Create two other Classes 'ClassOne' and 'ClassTwo' in the same Java Class 'ClassXYZ' file as shown below -
4. In the ClassOne Class, create an instance variable 'var1' and in the 'ClassTwo' Class, create an instance variable 'var2' as shown below -
5. Now create objects for ClassOne and ClassTwo Classes in ClassXYZ Class as shown below -
6. Lets use the equals( ) method to compare this objects and print whether object1 of ClassOne is equal to the object2 of ClassTwo as shown below -
7. Save and Run the Java Class file 'ClassXYZ' and observe that the output is displayed in the console as shown below -
Please comment below to feedback or ask questions.