Just for the fun of it, I tried compiling the two suggested implementations (ternary operator vs the explicit if statement). They both use if_icmple
instruction so I guess performance will be identical:
The if
version:
public static void main(java.lang.String[]);
Code:
0: iconst_0
1: istore_1
2: iload_1
3: sipush 8640
6: if_icmple 13
9: sipush 8640
12: istore_1
13: return
Using '?' ternary operator:
public static void main(java.lang.String[]);
Code:
0: iconst_0
1: istore_1
2: iload_1
3: sipush 8640
6: if_icmple 15
9: sipush 8640
12: goto 16
15: iload_1
16: istore_1
17: return
There's a slight inefficiency in using the ?
operator (At least in this specific case) due to the :
clause (the instructions labeled 15 and 16 in the above listing). The JVM will probably optimize away these redundant operations.