- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEqualsHashCodeExamples.java
More file actions
Latest commit
46 lines (36 loc) · 930 Bytes
/
Copy pathEqualsHashCodeExamples.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
packageconcept.examples.object;
classClient {
privateintid;
publicClient(intid) {
this.id = id;
}
@Override
publicinthashCode() {
finalintprime = 31;
intresult = 1;
result = prime * result + id;
returnresult;
}
@Override
publicbooleanequals(Objectobj) {
if (this == obj)
returntrue;
if ((obj == null) || (getClass() != obj.getClass()))
returnfalse;
Clientother = (Client) obj;
if (id != other.id)
returnfalse;
returntrue;
}
}
publicclassEqualsHashCodeExamples {
publicstaticvoidmain(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.
Clientclient1 = newClient(25);
Clientclient2 = newClient(25);
Clientclient3 = client1;
System.out.println(client1.equals(client2));// true
System.out.println(client1.equals(client3));// true
}
}