This question already has an answer here:
I switched lecturers today and he stated using a weird code to me. (He said it's better to use .equals
and when I asked why, he answered "because it is!")
So here's an example:
if (o1.equals(o2))
{
System.out.println("Both integer objects are the same");
}
Instead of what I'm used to:
if (o1 == o2)
{
System.out.println("Both integer objects are the same");
}
What's the difference between the two. And why is his way (using .equals
) better?
Found this on a quick search but I can't really make sense of that answer:
== is an operator. equals is a method defined in the Object class
== checks if two objects have the same address in the memory and for primitive it checks if they have the same value.equals method on the other hand checks if the two objects which are being compared have an equal value(depending on how ofcourse the equals method has been implemented for the objects. equals method cannot be applied on primitives(which means that if a is a primitive a.equals(someobject) is not allowed, however someobject.equals(a) is allowed).
Here is a simple interpretation about your problem:
where as
Therefore, it its better to use == for numeric operations & equals() method for String related operations. So, for comparison of objects the equals() method would be right choice.
If you and I each walk into the bank, each open a brand new account, and each deposit $100, then...
myAccount.equals(yourAccount)
istrue
because they have the same value, butmyAccount == yourAccount
isfalse
because they are not the same account.(Assuming appropriate definitions of the
Account
class, of course. ;-)Lets say that "==" operator returns true if both both operands belong to same object but when it will return true as we can't assign a single object multiple values
Now when this happens practically speaking, If its not happen then why this is == compares functionality....
In Java, when the “==” operator is used to compare 2 objects, it checks to see if the objects refer to the same place in memory. EX:
Even though the strings have the same exact characters (“xyz”), The code above will actually output: obj1==obj2 is FALSE
Java String class actually overrides the default equals() implementation in the Object class – and it overrides the method so that it checks only the values of the strings, not their locations in memory. This means that if you call the equals() method to compare 2 String objects, then as long as the actual sequence of characters is equal, both objects are considered equal.
This code will output the following:
obj1==obj2 is TRUE
In Java,
==
always just compares two references (for non-primitives, that is) - i.e. it tests whether the two operands refer to the same object.However, the
equals
method can be overridden - so two distinct objects can still be equal.For example:
Additionally, it's worth being aware that any two equal string constants (primarily string literals, but also combinations of string constants via concatenation) will end up referring to the same string. For example:
Here
x
andy
are references to the same string, becausey
is a compile-time constant equal to"hello"
.