-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.java
More file actions
34 lines (22 loc) · 994 Bytes
/
test.java
File metadata and controls
34 lines (22 loc) · 994 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
package equal;
// import java.time.Period;
public class test {
public static void main(String[] args) {
Person firstPerson = new Person("Amit", 24, 1252);
Person secondPerson = new Person("Amit", 24, 1252);
System.out.println(firstPerson);
System.out.println(secondPerson);
// This will print different because both the objects are different in memory
if (firstPerson == secondPerson) {
System.out.println("Both the objects are same");
}else{
System.out.println("Both the objects are different");
}
// Now this will print same because we have overridden the equals method in Person class to compare the values of the objects instead of their memory addresses
if (firstPerson.equals(secondPerson)) {
System.out.println("Both the objects are same");
}else{
System.out.println("Both the objects are different");
}
}
}