Both equals() too "==" operator inward Java is used to compare objects to depository fiscal establishment gibe equality but the primary divergence betwixt equals method and the == operator is that old is a method too later on is an operator. Since Java doesn’t back upwards operator overloading, == behaves identical for every object but equals() is method, which tin order the axe live overridden inward Java too logic to compare objects tin order the axe live changed based upon occupation organization rules. Another notable divergence betwixt == too equals method is that old is used to compare both primitive too objects spell later on is alone used for objects comparison. At the same time, beginners fighting to detect when to purpose equality operator (==) and when to use equals method for comparing Java objects. In this tutorial, nosotros volition meet how equals() method too == operator plant inward Java too what is the divergence betwixt "==" too equals method inward Java too lastly when to purpose "==" too equals() to compare objects.
What is "==" equality operator inward Java
"==" or equality operator inward Java is a binary operator provided yesteryear Java programming linguistic communication too used to compare primitives too objects. In damage of comparing primitives similar boolean, int, float "==" works fine but when it comes to comparing objects it creates confusion amongst equals method inward Java. "==" compare 2 objects based on retentiveness reference. thence "==" operator volition render truthful alone if 2 object reference it is comparing correspond just same object otherwise "==" volition render false.
After the introduction of Autoboxing too unboxing inward Java 5, using == to compare wrapper objects fifty-fifty overstep trickier because sometimes they tin order the axe render an unexpected result. See my post service what is the occupation amongst == operator inward autoboxing earth post-Java 5 for to a greater extent than details.
After the introduction of Autoboxing too unboxing inward Java 5, using == to compare wrapper objects fifty-fifty overstep trickier because sometimes they tin order the axe render an unexpected result. See my post service what is the occupation amongst == operator inward autoboxing earth post-Java 5 for to a greater extent than details.
What is equals method inward Java
Equals() method is defined inward Object flat inward Java too used for checking equality of 2 objects defined yesteryear occupation organization logic e.g. 2 Employees are considered equal if they convey same empId etc. You tin order the axe convey your domain object too and thence override equals method for defining a status on which 2 domain objects volition live considered equal. equal has contracted amongst hashcode method inward Java too whenever you lot override equals method you lot besides need to override hashcode() inward Java.
Default implementation of equals provided inward Object flat is similar to "==" equality operator too render truthful if you lot are comparing 2 references to the same object. It’s 1 of the Java best practise to override equals inward Java to define equality based on occupation organization requirement. It’s besides worth noting that equals should live consistent amongst compareTo inward Java, So that when you lot shop objects inward TreeMap or TreeSet Collection, which uses compareTo for checking equality, behaviour remains consistent.
Default implementation of equals provided inward Object flat is similar to "==" equality operator too render truthful if you lot are comparing 2 references to the same object. It’s 1 of the Java best practise to override equals inward Java to define equality based on occupation organization requirement. It’s besides worth noting that equals should live consistent amongst compareTo inward Java, So that when you lot shop objects inward TreeMap or TreeSet Collection, which uses compareTo for checking equality, behaviour remains consistent.
Difference betwixt == too equals inward Java
Main divergence betwixt == too equals inward Java is that "==" is used to compare primitives spell equals() method is recommended to depository fiscal establishment gibe equality of objects. Another divergence betwixt them is that, If both "==" too equals() is used to compare objects than == returns truthful alone if both references points to same object spell equals() tin order the axe render truthful or imitation based on its overridden implementation.One of the pop cases is comparing 2 String inward Java inward which instance == too equals() method render dissimilar results.
Comparing String amongst == too equals
String comparing is a mutual scenario of using both == too equals method. Since java.lang.String flat override equals method, It render truthful if 2 String object contains same content but == volition alone render truthful if 2 references are pointing to the same object. Here is an instance of comparing 2 Strings inward Java for equality using == too equals() method which volition clear roughly doubts:
String personalLoan = new String("cheap personal loans");
String homeLoan = new String("cheap personal loans");
//since 2 strings are dissimilar object number should live false
boolean number = personalLoan == homeLoan;
System.out.println("Comparing 2 strings amongst == operator: " + result);
//since strings contains same content , equals() should render true
result = personalLoan.equals(homeLoan);
System.out.println("Comparing 2 Strings amongst same content using equals method: " + result);
homeLoan = personalLoan;
//since both homeLoan too personalLoand reference variable are pointing to same object
//"==" should render true
result = (personalLoan == homeLoan);
System.out.println("Comparing 2 reference pointing to same String amongst == operator: " + result);
Output:
Comparing 2 strings amongst == operator: false
Comparing 2 Strings amongst same content using equals method: true
Comparing 2 references pointing to same String amongst == operator: true
String homeLoan = new String("cheap personal loans");
//since 2 strings are dissimilar object number should live false
boolean number = personalLoan == homeLoan;
System.out.println("Comparing 2 strings amongst == operator: " + result);
//since strings contains same content , equals() should render true
result = personalLoan.equals(homeLoan);
System.out.println("Comparing 2 Strings amongst same content using equals method: " + result);
homeLoan = personalLoan;
//since both homeLoan too personalLoand reference variable are pointing to same object
//"==" should render true
result = (personalLoan == homeLoan);
System.out.println("Comparing 2 reference pointing to same String amongst == operator: " + result);
Output:
Comparing 2 strings amongst == operator: false
Comparing 2 Strings amongst same content using equals method: true
Comparing 2 references pointing to same String amongst == operator: true
Comparing 2 objects amongst "==" too equals.
Another scenario which creates confusion betwixt == too equals method is when you lot compare 2 Objects. When you lot compare 2 references pointing to an object of type Object you lot should meet the same number from both == operator too equals method because default implementation of equals method simply compare retentiveness address of 2 objects too render truthful if 2 reference variable are pointing towards an just same object. Here is instance of == vs equals method for comparing 2 objects:
Object obj1 = new Object();
Object obj2 = new Object();
// == should render false
result = (obj1==obj2);
System.out.println("Comparing 2 dissimilar Objects amongst == operator: " + result);
//equals should render imitation because obj1 too obj2 are different
result = obj1.equals(obj2);
System.out.println("Comparing 2 dissimilar Objects amongst equals() method: " + result);
// "==" should render truthful because both obj1 too obj2 points same object
obj1=obj2;
result = (obj1==obj2);
System.out.println("Comparing 2 reference pointing to same Object amongst == operator: " + result);
Output:
Comparing 2 dissimilar Objects amongst == operator: false
Comparing 2 dissimilar Objects amongst equals() method: false
Comparing 2 references pointing to the same Object amongst == operator: true
Object obj2 = new Object();
// == should render false
result = (obj1==obj2);
System.out.println("Comparing 2 dissimilar Objects amongst == operator: " + result);
//equals should render imitation because obj1 too obj2 are different
result = obj1.equals(obj2);
System.out.println("Comparing 2 dissimilar Objects amongst equals() method: " + result);
// "==" should render truthful because both obj1 too obj2 points same object
obj1=obj2;
result = (obj1==obj2);
System.out.println("Comparing 2 reference pointing to same Object amongst == operator: " + result);
Output:
Comparing 2 dissimilar Objects amongst == operator: false
Comparing 2 dissimilar Objects amongst equals() method: false
Comparing 2 references pointing to the same Object amongst == operator: true
Summary
1) purpose == to compare primitive e.g. boolean, int, char etc, spell purpose equals() to compare objects inward Java.
2) == render truthful if 2 reference are of same object. Result of equals() method depends on overridden implementation.
3) For comparing String purpose equals() instead of == equality operator.
That’s all on the difference betwixt equals method and == operator inward Java. As I said the primary divergence betwixt them is that 1 of them is an operator too other is a method too == is used to compare both primitive too objects spell equals() method is used to depository fiscal establishment gibe equality of objects only.
Further Learning
Complete Java Masterclass
Why multiple inheritance is non supported inward Java