How to compare enums in Java
September 6, 2016Summary The best way to compare enums in Java is == operator, like in an example below:
1 2 3 4 |
Color white = Color.WHITE; System.out.println(white == Color.WHITE); |
Comparison of == and equals methods There are two ways to compare enums in java: == operator and equals() method. First of all, I would like to show you an implementation of method equals() of java.lang.Enum:
1 2 3 4 5 |
public final boolean equals(Object other) { return this==other; } |
As […]