-[== does not generate NullPointerException / equals occurs](# does not generate nullpointerexception-equals occurs) -[== is lean because there is no type conversion (fast) / equals is wasteful because there is type conversion to Object type (slow)](# is lean because there is no type conversion fast-equals is There is waste because there is a type conversion to the object type. Slow) -[== has type checking at compile time / equals has no type checking](# has type checking at compile time-equals has no type checking)
Nullpo confirmation
public class EnumNullPointerExceptionTest {
enum Season { SPRING, SUMMER, AUTUMN, WINTER; }
public static void main(String[] args) {
Season season = null;
if (season != Season.SPRING) {
System.out.println("The season is not spring.");
}
if (!season.equals(Season.SPRING)) {
System.out.println("The season is not spring.");
}
}
}
Execution result
The season is not spring.
Exception in thread "main" java.lang.NullPointerException
at EnumNullPointerExceptionTest.main(EnumNullPointerExceptionTest.java:13)
== passed and was angry with Nullpo at equals (although I actually passed! =).
NullPointerException does not occur == Tsuyotsuyo: blush: NullPointerException occurs equlas Yowayowa: sweat:
==Processing time measurement
public class EnumSpeedTest1 {
enum Season { SPRING, SUMMER, AUTUMN, WINTER; }
public static void main(String[] args) {
Season season = Season.SPRING;
long startTimeMs = System.currentTimeMillis();
for (int i = 0; i < 2000000000; i++) {
if (season == Season.SPRING) {}
}
long executionTimeMs = System.currentTimeMillis() - startTimeMs;
System.out.println("How long did it take to repeat the comparison 2 billion times?" + executionTimeMs + "Milliseconds");
}
}
==Execution result of
It took 2 milliseconds to repeat the comparison 2 billion times
Equals processing time measurement
public class EnumSpeedTest2 {
enum Season { SPRING, SUMMER, AUTUMN, WINTER; }
public static void main(String[] args) {
Season season = Season.SPRING;
long startTimeMs = System.currentTimeMillis();
for (int i = 0; i < 2000000000; i++) {
if (season.equals(Season.SPRING)) {}
}
long executionTimeMs = System.currentTimeMillis() - startTimeMs;
System.out.println("How long did it take to repeat the comparison 2 billion times?" + executionTimeMs + "Milliseconds");
}
}
Execution result of equals
It took 4 milliseconds to repeat the comparison 2 billion times
It's a little, but == is faster because it doesn't convert.
No type conversion and no waste == Tsuyotsuyo: relaxed: Equals that requires type conversion and is wasteful: sweat:
Compile confirmation
public class EnumCompileTest {
enum Season { SPRING, SUMMER, AUTUMN, WINTER; }
enum Color { RED, GREEN, BLUE; }
enum Animal { DOG, CAT; }
public static void main(String[] args) {
if (Season.SPRING.equals(Color.RED)) {}
if (Season.SPRING == Animal.CAT) {}
}
}
Compile result
$ javac EnumCompileTest.java
EnumCompileTest.java:10:error:Types Season and Animal cannot be compared
if (Season.SPRING == Animal.CAT) {}
^
1 error
The comparison of different types == was angry.
There is a type check at compile time == Tsuyotsuyo: relaxed: Equals without type checking at compile time: sweat:
The language specification says: 8.9.1. Enum Constants
Because there is only one instance of each enum constant, it is permitted to use the == operator in place of the equals method when comparing two object references if it is known that at least one of them refers to an enum constant.
There is only one instance of each enum constant The enum instance is a singleton. So you can compare with ==.
Recommended Posts