equals, is supposed to compare that the content of two objects is equivalent, regardless of their identity (that is, the instance of the object, which is compared with ==).
A variable is not an object, it is the container of a data/object type. If the variable is true nullyou cannot compare it as equality because there is no object to compare.
The operator ==compares the object reference. and .equals()it checks if two objects are equal, but, even though the objects are equal null when compared will return a NullPointerException.
When you compare two objects with (==) what you are comparing is whether the two references point to the same memory address where the object is located.
Example:
Person person1 = new Person(); Person person2 = new Person();
if you compare
person1 == person2 -> the result will be false.
but if you do this:
person2 = person1 -> you are indicating that person2 references the object referenced by person1.
then when comparing
person1 == person2 -> will return true.
Case 2 equals.
The equals method is declared in the main class Object, it indicates if two objects are equal (if they have the same content), all classes in java inherit from object explicitly or implicitly.
In the case of the java.lang.String class, if we check the documentation, it overrides the equals method.
Example:
String name1 = "joel"; String name2 = "joel";
name1.equals(name2) -> The result will be true since they have the same content.
If you wanted to compare your own created objects, using equals, what you have to do is override the equals method and implement it.
In the Java documentation it is said how the equals method has to be implemented following its contract.
equals
, is supposed to compare that the content of two objects is equivalent, regardless of their identity (that is, the instance of the object, which is compared with==
).A variable is not an object, it is the container of a data/object type. If the variable is true
null
you cannot compare it as equality because there is no object to compare.The operator
==
compares the object reference. and.equals()
it checks if two objects are equal, but, even though the objects are equal null when compared will return aNullPointerException
.The reason is because you want to compare the reference and for this purpose it is better to use the operator
==
The equals method will compare the equality
valor
of two objects. the parameter sent must be aObjeto
and not a reference otherwise NullPointerException will be thrownStarting from Java
7.0
we added a Method equals(Object a ,Object b) of the Objects class to check thisMy opinion:
When you compare two objects with (==) what you are comparing is whether the two references point to the same memory address where the object is located.
Example:
Person person1 = new Person(); Person person2 = new Person();
if you compare
person1 == person2 -> the result will be false.
but if you do this:
person2 = person1 -> you are indicating that person2 references the object referenced by person1.
then when comparing
person1 == person2 -> will return true.
Case 2 equals.
The equals method is declared in the main class Object, it indicates if two objects are equal (if they have the same content), all classes in java inherit from object explicitly or implicitly.
In the case of the java.lang.String class, if we check the documentation, it overrides the equals method.
Example:
String name1 = "joel"; String name2 = "joel";
name1.equals(name2) -> The result will be true since they have the same content.
If you wanted to compare your own created objects, using equals, what you have to do is override the equals method and implement it.
In the Java documentation it is said how the equals method has to be implemented following its contract.
https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#equals(java.lang.Object)
I hope I've helped.
The == operator performs the comparison at the object level. That is, it determines if both objects are equal.
The equals() method compares the characters within the String object.