Yeap, this is because, in the general case, the switch statement is faster than if / else.
Although the generated bytecode is not always the ultimate source of performance mappings, you can consider it to have a better idea.
For example, this code:
class A { enum N { ONE, TWO, THREE } void testSwitch( N e ) { switch( e ) { case ONE : x(); break; case TWO : x(); break; case THREE : x(); break; } } void testIf( Enum e ) { if( e == N.ONE ) { x(); } else if( e == N.TWO ) { x(); } else if( e == N.THREE ) { x(); } } void x(){} }
Creates the following:
Compiled from "A.java" class A extends java.lang.Object{ A(); Code: 0: aload_0 1: invokespecial
In both cases, it is pretty fast.
So, choose the one that is easier to maintain.
OscarRyz Feb 07 2018-11-11T00: 00Z
source share