-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReferences.java
113 lines (82 loc) · 3.45 KB
/
References.java
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
public class References {
private static final int STARTING_VALUE = 42;
public static void main(String[] args) {
// 0. First let's look at how values normally change.
modifyValuesNormally();
// 1. What happens if we modify copies?
//modifyCopies();
// 2. What happens if we modify method arguments?
//modifyInMethod();
// 3. What happens if we modify array elements in a standard for loop?
//modifyInForLoop();
// 4. What happens if we modify array elements in a for-each loop?
//modifyInForEachLoop();
}
private static void modifyValuesNormally() {
System.out.println("Modifying values normally...");
int basic = STARTING_VALUE;
NumberClass reference = new NumberClass(STARTING_VALUE);
System.out.println("Before: basic type = " + basic
+ ", object reference = " + reference);
++basic;
++reference.num;
System.out.println("After: basic type = " + basic
+ ", object reference = " + reference);
}
private static void modifyCopies() {
System.out.println("Modifying copies...");
int basic = STARTING_VALUE;
NumberClass reference = new NumberClass(STARTING_VALUE);
int basicCopy = basic;
NumberClass referenceCopy = reference;
System.out.println("Before: basic type = " + basic
+ ", object reference = " + reference);
++basicCopy;
++referenceCopy.num;
System.out.println("After: basic type = " + basic
+ ", object reference = " + reference);
}
private static void modifyInMethod() {
System.out.println("Modifying arguments of a method call...");
int basic = STARTING_VALUE;
NumberClass reference = new NumberClass(STARTING_VALUE);
System.out.println("Before: basic type = " + basic
+ ", object reference = " + reference);
modifyArguments(basic, reference);
System.out.println("After: basic type = " + basic
+ ", object reference = " + reference);
}
private static void modifyArguments(int basicArg,
NumberClass referenceArg) {
++basicArg;
++referenceArg.num;
}
private static void modifyInForLoop() {
System.out.println("Modifying values inside a for loop...");
int[] basic = {STARTING_VALUE};
NumberClass[] reference = {new NumberClass(STARTING_VALUE)};
System.out.println("Before: basic type = " + basic[0]
+ ", object reference = " + reference[0]);
for (int i = 0; i < 1; ++i) {
++basic[i];
++reference[i].num;
}
System.out.println("After: basic type = " + basic[0]
+ ", object reference = " + reference[0]);
}
private static void modifyInForEachLoop() {
System.out.println("Modifying values inside a for-each loop...");
int[] basic = {STARTING_VALUE};
NumberClass[] reference = {new NumberClass(STARTING_VALUE)};
System.out.println("Before: basic type = " + basic[0]
+ ", object reference = " + reference[0]);
for (int value: basic) {
++value;
}
for (NumberClass value: reference) {
++value.num;
}
System.out.println("After: basic type = " + basic[0]
+ ", object reference = " + reference[0]);
}
}