-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRedundantNullChecks.java
More file actions
39 lines (35 loc) · 1.1 KB
/
RedundantNullChecks.java
File metadata and controls
39 lines (35 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import java.util.Random;
public class RedundantNullChecks {
public static void main(String[] args) {
long startTime = System.nanoTime();
int a = 0;
for (int i=0; i<99999999; i++){
Random random = new Random();
int randomNumber = random.nextInt(20); // Random number between 0 and 19
Object obj;
if (randomNumber < 10) {
obj = null;
} else {
obj = new Object();
}
if (obj == null) {
a+=1;
continue;
} else {
a-=1;
}
// Redundant null checks
// If obj == null, code would not reach here
if (obj != null) {
a-=1;
}
if (obj != null) {
a-=1;
}
}
System.out.println("value of a:"+ a);
long endTime = System.nanoTime();
long duration = (endTime - startTime) / 1000000;
System.out.println("Execution time: " + duration + " milliseconds");
}
}