Repository navigation
Expand file tree
/
Copy pathDemo.java
More file actions
73 lines (55 loc) · 1.74 KB
/
Copy pathDemo.java
File metadata and controls
73 lines (55 loc) · 1.74 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
import java.util.Objects;
public class Demo {
public static void main(String[] args) throws CloneNotSupportedException {
// Student s = new Student();
// s.name = "Raquib";
// s.age = 25;
// System.out.println(s.toString());
// System.out.println(s);
Student s1 = new Student();
Student s2 = new Student();
s1.name = "Raquib";
s2.name = "Raquib";
s1.age = 32;
s2.age = 32;
// System.out.println(s1.hashCode() == s2.hashCode());
// System.out.println(s1.getClass().getName());
// System.out.println(s1 instanceof Student);
Student s3 = (Student) s1.clone();
System.out.println(s3.toString());
System.out.println(new int[1] instanceof Object);
}
}
class Student extends Object implements Cloneable {
String name;
int age;
@Override
public String toString() {
return String.format("{name: %s, age: %d}", name, age);
}
@Override
public boolean equals(Object obj) {
if (obj == null)
return false;
if (obj == this)
return true;
// if not checked --> ClassCastException
if (obj.getClass() != this.getClass())
return false;
Student s = (Student) obj;
return this.name.equals(s.name) && this.age == s.age;
}
@Override
public int hashCode() {
// int result = 17;
// result = result * 31 + age;
// result = result * 31 + (name == null ? 0 : name.hashCode());
// return result;
return Objects.hash(name, age);
}
@Override
protected Object clone() throws CloneNotSupportedException {
// shallow copy
return super.clone();
}
}