forked from PacktPublishing/Java-Coding-Problems
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMain.java
More file actions
30 lines (22 loc) · 758 Bytes
/
Copy pathMain.java
File metadata and controls
30 lines (22 loc) · 758 Bytes
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
package modern.challenge;
import java.util.Optional;
public class Main {
public static void main(String[] args) {
Book book = new Book();
Optional<Book> op1 = Optional.of(book);
Optional<Book> op2 = Optional.of(book);
// Avoid
// op1 == op2 => false, expected true
if (op1 == op2) {
System.out.println("op1 is equal with op2, (via ==)");
} else {
System.out.println("op1 is not equal with op2, (via ==)");
}
// Prefer
if (op1.equals(op2)) {
System.out.println("op1 is equal with op2, (via equals())");
} else {
System.out.println("op1 is not equal with op2, (via equals())");
}
}
}