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).
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
How does Java "==" operator check equality between objects? Trying to understand how Object.equals() works
== vs. .equals()??
Help me understand the difference between "==" and ".equals()" in Java
Java comparison == is not null-safe?
Videos
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.
I'm on MOOC.fi and I'm a little confused on why you can't use .equals() when comparing two objects. And on part 5_12.Song programming exercise, I'm confused on why the parameter takes an Object parameter instead of a "Song" type, and then why do we have to typecast the object to compare it?
I'm currently working on a project that involves comparing strings, but I keep getting stuck on whether to use the "==" operator or the ".equals()" method. From what I've gathered so far, they seem to do the same thing - is it true? Or are there cases where one should be used over the other?