IMLC.ME
Search…
en-US
Shall I avoid Exclamation Mark(!) in If-Statement in Java
Today I read a post that we should not use "!" in If statement in order to gain a better Perfermance. Because the "!" operator adds extra action for your program. It sounds not a good idea. Is it really truth?
I did a simple experiment.
I wrote 2 Java snippets:
1
// App.java
2
public final class App {
3
public static void main(String[] args) {
4
if(App.shouldPrintMessage()) {
5
System.out.println("Hello, wolrd");
6
}
7
}
8
9
public static boolean shouldPrintMessage() {
10
return true;
11
}
12
}
Copied!
Ran javac App.java and then I got App.class. Ran javap -c App.class to view the bytecode.
Here it is:
1
public final class App {
2
public App();
3
Code:
4
0: aload_0
5
1: invokespecial #1 // Method java/lang/Object."<init>":()V
6
4: return
7
8
public static void main(java.lang.String[]);
9
Code:
10
0: invokestatic #2 // Method shouldPrintMessage:()Z
11
3: ifeq 14 // If-Equal !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
12
6: getstatic #3 // Field java/lang/System.out:Ljava/io/PrintStream;
13
9: ldc #4 // String Hello, wolrd
14
11: invokevirtual #5 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
15
14: return
16
17
public static boolean shouldPrintMessage();
18
Code:
19
0: iconst_1
20
1: ireturn
21
}
Copied!
And then I added ! in If-Statement
1
public final class App {
2
public static void main(String[] args) {
3
if(!App.shouldPrintMessage()) {
4
System.out.println("Hello, wolrd");
5
}
6
}
7
8
public static boolean shouldPrintMessage() {
9
return true;
10
}
11
}
Copied!
Checked the bytecode
1
public final class App {
2
public App();
3
Code:
4
0: aload_0
5
1: invokespecial #1 // Method java/lang/Object."<init>":()V
6
4: return
7
8
public static void main(java.lang.String[]);
9
Code:
10
0: invokestatic #2 // Method shouldPrintMessage:()Z
11
3: ifne 14 // If-Not-Equal !!!!!!!!!!!!!!!!!!!!!!!!!!!!!
12
6: getstatic #3 // Field java/lang/System.out:Ljava/io/PrintStream;
13
9: ldc #4 // String Hello, wolrd
14
11: invokevirtual #5 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
15
14: return
16
17
public static boolean shouldPrintMessage();
18
Code:
19
0: iconst_1
20
1: ireturn
21
}
Copied!
As you can see, the main difference is the ifeq and the ifne.
Let me tried the example exactly provided in the post I read.
1
public final class App {
2
public static void main(String[] args) {
3
int a = 2;
4
if(!(a <= 1)) {
5
System.out.println("Hello, wolrd");
6
}
7
}
8
9
}
10
11
// vs
12
13
public final class App {
14
public static void main(String[] args) {
15
int a = 2;
16
if(a > 1) {
17
System.out.println("Hello, wolrd");
18
}
19
}
20
}
Copied!
And here is the output
1
public final class App {
2
public App();
3
Code:
4
0: aload_0
5
1: invokespecial #1 // Method java/lang/Object."<init>":()V
6
4: return
7
8
public static void main(java.lang.String[]);
9
Code:
10
0: iconst_2
11
1: istore_1
12
2: iload_1
13
3: iconst_1
14
4: if_icmple 15
15
7: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream;
16
10: ldc #3 // String Hello, wolrd
17
12: invokevirtual #4 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
18
15: return
19
}
20
21
// vs
22
23
public final class App {
24
public App();
25
Code:
26
0: aload_0
27
1: invokespecial #1 // Method java/lang/Object."<init>":()V
28
4: return
29
30
public static void main(java.lang.String[]);
31
Code:
32
0: iconst_2
33
1: istore_1
34
2: iload_1
35
3: iconst_1
36
4: if_icmple 15
37
7: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream;
38
10: ldc #3 // String Hello, wolrd
39
12: invokevirtual #4 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
40
15: return
41
}
Copied!
They're exactly the same in bytecode level.
Two examples showed us that there is NO addtional operation even though we use "!" in If-Statement. And futhermore, don't forget we have runtime optimazation in Java. In 2019, I quite believe the JVM is smart enough to resolve this problem. So I think we can choose whatever we want in If-Statement from the readability perspective.
Last modified 1yr ago
Copy link