File size: 3,459 Bytes
0f3dc9a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
ArrayList


import java.util.ArrayList;

public class Example {
    public static void main(String[] args){
       ArrayList<Double> results = new ArrayList<>();

       //  variable results refers to an object,
       // so list can be manipulated through it
       results.add(2.5);
       results.add(5.75);
       results.add(4.5);

       for (double result : results) {
           System.out.println(result);
       }
    }
}

Program outputs:
2.5
5.75
4.5



================================



Several references to the same object


import java.util.ArrayList;

public class Example {
    public static void main(String[] args){
        ArrayList<Integer> numbers = new ArrayList<>();
        ArrayList<Integer> numbersTwo = numbers; // refer to the same 'numbers' list
        
        numbers.add(5);
        numbers.add(9);
        numbers.add(15);

        numbersTwo.add(17);
        numbersTwo.add(21);

        System.out.println(numbers);
        System.out.println(numbersTwo);
    }
}


Program outputs:
[5, 9, 15, 17, 21]
[5, 9, 15, 17, 21]







================================


Easy to match same content for BASIC TYPEs

Scanner reader = new Scanner(System.in);
while (true) {
    System.out.print("Give a number: ");
    int num = Integer.valueOf(reader.nextLine());

    if (num == 10) {
        System.out.println("You gave a ten!");
        break;
    }
}

Example execution:
Give a number: 5
Give a number: 6
Give a number: 3
Give a number: 10
You gave a ten!




VS




Difficult to match same object for REFERENCE/OBJECT TYPEs
- match references
- not matching content




ArrayList<Integer> list1 = new ArrayList<>();
ArrayList<Integer> list2 = new ArrayList<>();
ArrayList<Integer> list3 = list1;

list1.add(1);
list1.add(2);
list1.add(3);

list2.add(1);
list2.add(2);
list2.add(3);

System.out.println("list1 == list2? " + (list1 == list2));
System.out.println("list1 == list3? " + (list1 == list3));
System.out.println("list2 == list3? " + (list2 == list3));


Program outputs:
list1 == list2? false     // DIFFERENT REFERENCE BUT SAME CONTENT
list1 == list3? true      // SAME REFERENCE, cuz list3 was refererring to list1, and its contents
list2 == list3? false     // DIFFERENT REFERENCE BUT SAME CONTENT









================================



The 'equals' operator can be used to compare the SIMILARITY of OBJECTS.
The operator returns true if the objects have the SAME CONTENT.



ArrayList<Integer> list1 = new ArrayList<>();
ArrayList<Integer> list2 = new ArrayList<>();
ArrayList<Integer> list3 = list1;

list1.add(1);
list1.add(2);
list1.add(3);

list2.add(1);
list2.add(2);
list2.add(3);

System.out.println("list1.equals(list2) " + (list1.equals(list2)));
System.out.println("list1.equals(list3) " + (list1.equals(list3)));
System.out.println("list2.equals(list3) " + (list2.equals(list3)));


Program outputs:
list1.equals(list2) true
list1.equals(list3) true
list2.equals(list3) true








================================


Since STRINGS are Java OBJECTS, their EQUALITY must also be compared using the EQUALS operator, for example

Scanner reader  = new Scanner(System.in);
System.out.print("Give a name: ");
String name = reader.nextLine();


if (name == "Jack Java") {
    System.out.println("nimi == Jack Java"); // THIS DID NOT RUN
}
if (name.equals("Jack Java")){
    System.out.println("the name is Jack Java"); // THIS RAN
}


Program outputs:
Give a name: Jack Java
the name is Jack Java