Repository navigation
Expand file tree
/
Copy pathQ6_Tricky.java
More file actions
79 lines (66 loc) · 2.47 KB
/
Copy pathQ6_Tricky.java
File metadata and controls
79 lines (66 loc) · 2.47 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import java.util.Objects;
public class Q6_Tricky {
// ── PREDICT: What does main() print for lines 1-6? ──
public static void main(String[] args) {
// Q6a: Integer cache
Integer a = 127;
Integer b = 127;
System.out.println("1: " + (a == b));
Integer c = 128;
Integer d = 128;
System.out.println("2: " + (c == d));
// Q6b: String interning
String s1 = "frc";
String s2 = "frc";
String s3 = new String("frc");
System.out.println("3: " + (s1 == s2));
System.out.println("4: " + (s1 == s3));
System.out.println("5: " + s1.equals(s3));
// Q6c: null unboxing
try {
Integer e = null;
int f = e;
System.out.println("6: " + f);
} catch (Exception ex) {
System.out.println("6: " + ex.getClass().getSimpleName());
}
// ── WRITE ──
// 1) Implement safeEquals that:
// - returns true if both are null
// - returns false if exactly one is null
// - uses .equals() otherwise
// Do NOT use Objects.equals (pretend it doesn't exist).
assert safeEquals(null, null) == true;
assert safeEquals("a", null) == false;
assert safeEquals(null, "a") == false;
assert safeEquals("frc", new String("frc")) == true;
assert safeEquals("frc", "nope") == false;
System.out.println("safeEquals ok");
// 2) Implement safeUnbox that takes an Integer and a default int.
// If the Integer is null, return the default. Otherwise unbox it.
assert safeUnbox(null, 42) == 42;
assert safeUnbox(Integer.valueOf(7), 42) == 7;
assert safeUnbox(null, -1) == -1;
System.out.println("safeUnbox ok");
// 3) Without running it: what does this print? Write your answer
// as a comment, then uncomment to verify.
//
// Integer x = 0;
// for (int i = 0; i < 5; i++) {
// x = x + 1; // how many Integer objects does this create total?
// }
// System.out.println(x);
// System.out.println(x == Integer.valueOf(5));
// System.out.println(x == new Integer(5));
}
// TODO
static boolean safeEquals(Object a, Object b) {
// your code here
return false;
}
// TODO
static int safeUnbox(Integer val, int defaultVal) {
// your code here
return 0;
}
}