The difference is the Objects.equals() considers two nulls to be "equal". The pseudo code is:
- if both parameters are
nullor the same object, returntrue - if the first parameter is
nullreturnfalse - return the result of passing the second parameter to the
equals()method of the first parameter
This means it is "null safe" (non null safe implementation of the first parameter’s equals() method notwithstanding).
Videos
The difference is the Objects.equals() considers two nulls to be "equal". The pseudo code is:
- if both parameters are
nullor the same object, returntrue - if the first parameter is
nullreturnfalse - return the result of passing the second parameter to the
equals()method of the first parameter
This means it is "null safe" (non null safe implementation of the first parameter’s equals() method notwithstanding).
this is literal code from java source: as you can see, @Agent_L is right
//Suppose I have another class with a default constructor method
public Circle(double r)
{
radius =r;
}
//and in my main class I have the codes
Circle cir1= new Circle(5.1);
Circle cir2= new Circle(5.1);
System.out.println(cir1.equals(cir2));
// Why would the output be false? If i use the equals() method to compare the content of two strings, then why wouldnt it work the same way when comparing the contents with two different objects? How is the output false if the arguments are the same?
I'm wondering how "==" is implemented to check equality between two objects. The internet say's the Object superclass, .equals() is defined as:
public boolean equals(Object obj) {
return (this == obj);
}How does a simple "==" check equality between two objects? Hashcode, a bitwise AND of the sum of all data? I can't find anywhere that explains in depth how two objects are compared.