Reputation: 9134
I know the correct way to do this is Days.MONDAY.name().equals(day)
. But I'm wonder why Days.MONDAY.equals(day)
fails when both prints MONDAY.
I know I'm missing something with equals() and toString(). I wanna clearly know what is it.
String day = "MONDAY";
System.out.println("main().Days.MONDAY : " + Days.MONDAY); // Prints MONDAY
System.out.println("main().day : " + day);// Prints MONDAY
System.out.println("main().Days.MONDAY.equals(day) : " + Days.MONDAY.equals(day)); // Why is this false when below is OK.
System.out.println("main().Days.MONDAY.toString().equals(day) : " + Days.MONDAY.toString().equals(day));// This is true
System.out.println("main().Days.MONDAY.name().equals(day) : " + Days.MONDAY.name().equals(day)); // This is true and I know this is the correct way
Edit: This is the enum.
enum Days{
MONDAY,TUESDAY,WEDENSDAY,THURSDAY,FRIDAY,SATURDAY,SUNDAY;
}
Upvotes: 1
Views: 3901
Reputation: 34618
The methods of the class Object
have a strictly defined contract.
One of those methods is the Object.equals()
method - here is its documentation.
To be able to maintain the symmetry requirement, it is practically impossible to return true
in any implementation of equals()
unless the two objects being compared are of the same class. equals()
is supposed to represent some sort of equivalent between their properties, but objects which are not of the same class do not have the same properties.
Do not confuse the Days
object Days.MONDAY
with the string returned from Days.MONDAY.toString()
. Its toString()
just returns a string that represents it, and two strings are objects that can be equal. But Days.MONDAY.toString()
is not the object itself (try Days.MONDAY.equals( Day.MONDAY.toString() )
and you'll get false here, too!
When you send an object to the print()
or println()
methods of System.out
or any other PrintWriter
, print()
will take that object's toString()
value and print it. This is the reason why they "print the same thing". It is not actually the MONDAY
object that's being printed (it's hard to define "printing an object"), it's the string "MONDAY"
that's returned from its toString()
method.
All this would hold true even if Days
was not an enum but some other object that is not a string, though in the particular case of an enum, its equals()
method is indeed a comparison of references rather than attributes.
Upvotes: 0
Reputation: 166
Look at is-it-ok-to-use-on-enums-in-java. Based on this, Java's implementation of equals on Enum simply performs ==. Since the Enum and the String day
in your example are not the same object, it returns false.
Upvotes: 0
Reputation: 7730
String day="MONDAY";
The above line create Object
inside Constant Pool , Where as
public enum Days{
MONDAY <-- Created in HEAP
}
Now Coming to
Days.MONDAY.equals(day) --> Why False ?
equals()
method of Enum compares the instances of the Enum
not the data
as String#equals()
does !!
Days.MONDAY.toString().equals(day) --> Why true ?
because it is String#equals()
method which is overloaded !!
Upvotes: 0
Reputation: 2690
The equals method of an Enum compares the Static instances of the Enum. Because any representation of an Enum is pointing to the same object instance. So the equals method of the Enum is not comparing the Name or toString it compares the instances.
Upvotes: 4