Using Objects.equals() when one of the values may be null.

Transcribed from the original LinkedIn image post.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
import java.util.Objects;

class Day32 {
    public static void main(String[] args) {
        String file1Name = "data.zip";
        String file2Name = "data.json";

        System.out.println(Objects.equals(true, file2Name));
        System.out.println(file2Name.equals(file1Name));

        // Changing file2Name to data.zip to get equality.
        file2Name = "data.zip";
        System.out.println(Objects.equals(file1Name, file2Name));

        file2Name = null;

        System.out.println(Objects.equals(file1Name, file2Name));
        // This will give NullPointerException as null.equals() is not possible.
        System.out.println(file2Name.equals(file1Name));
    }
}

The original LinkedIn graphic is preserved below.

Day 32 LinkedIn post