In java Programming both are used for comparing. The == is used to compare primitive and objects while the later is used to compare objects. Use of both can vary based on the business needs.
What is "==" operator in java.
comparing primitives like boolean, int, float "==" works fine, but for objects comparison it uses the memory location. Return True only if both the memory reference are same otherwise return false.
What is "equals" method in java.
It also returns true if both the memory reference are same. It checks the values inside the object.
Difference between Java equals and == :
Main difference between == and equals in Java is that "==" is used to compare primitives while equals() method is recommended to check equality of objects. Another difference between them is that, If both "==" and equals() is used to compare objects than == returns true only if both references points to same object while equals() can return true or false based on its overridden implementation.
String a = new String("hai i am here");
String b = new String("hai i am here");
boolean c = a == b;
System.out.println(" == operator: " + c);
result = a.equals(b);
System.out.println("equals method: " + c);
homeLoan = personalLoan;
result = (a == b);
System.out.println("reference pointing same String with == operator: " + c);
Output:
Comparing two strings with == operator: false
Comparing two Strings with same content using equals method: true
Comparing two reference pointing to same String with == operator: true
Conclusion:
What is "==" operator in java.
comparing primitives like boolean, int, float "==" works fine, but for objects comparison it uses the memory location. Return True only if both the memory reference are same otherwise return false.
What is "equals" method in java.
It also returns true if both the memory reference are same. It checks the values inside the object.
Difference between Java equals and == :
Main difference between == and equals in Java is that "==" is used to compare primitives while equals() method is recommended to check equality of objects. Another difference between them is that, If both "==" and equals() is used to compare objects than == returns true only if both references points to same object while equals() can return true or false based on its overridden implementation.
String a = new String("hai i am here");
String b = new String("hai i am here");
boolean c = a == b;
System.out.println(" == operator: " + c);
result = a.equals(b);
System.out.println("equals method: " + c);
homeLoan = personalLoan;
result = (a == b);
System.out.println("reference pointing same String with == operator: " + c);
Output:
Comparing two strings with == operator: false
Comparing two Strings with same content using equals method: true
Comparing two reference pointing to same String with == operator: true
Conclusion:
- For comparing primitive types use "==".
- For comparing objects of the same refernce use "equals".
- For String comparison use "equals" rather than "==".
No comments:
Post a Comment