Difference Between == and equals() in Java

In Java == operator and equals() are used to perform comparison. In this article, we are going to see the difference between the “==” operator and the equals() method in Java.

== Operatorequals() method
1. It is an operator in Java. == operator applies to both primitive and object types.1. It is a method in Java. equals() method is only applicable to object types we can not use for primitive.
2. In this case of Object reference == operator is for reference comparison (address comparison)2. By default equals() also meant for refrence comparision.
3. We can not override == operator 3. We can overide equals() method
4. To use the == operator it is compulsory that there should be some relation between argument types otherwise we will get a compile time error.4. If there is no relation between argument types then the equals method would not raise any compile time or run time error, It simply returns false.

In general we can use == operator for refrence comparision and equals method for content comparision

Note- For any object reference if any argument is null then it will always return false.

Similar Java Tutorials

Leave a Comment