-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEqualsHashCodeExamples.java
More file actions
46 lines (36 loc) · 930 Bytes
/
EqualsHashCodeExamples.java
File metadata and controls
46 lines (36 loc) · 930 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package concept.examples.object;
class Client {
private int id;
public Client(int id) {
this.id = id;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + id;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if ((obj == null) || (getClass() != obj.getClass()))
return false;
Client other = (Client) obj;
if (id != other.id)
return false;
return true;
}
}
public class EqualsHashCodeExamples {
public static void main(String[] args) {
// == comparison operator checks if the object references are pointing
// to the same object. It does NOT look at the content of the object.
Client client1 = new Client(25);
Client client2 = new Client(25);
Client client3 = client1;
System.out.println(client1.equals(client2));// true
System.out.println(client1.equals(client3));// true
}
}