1+ package sample ;
2+
3+ // Program to test the equals method.
4+ // You can use the equals method of the Object class or the equals
5+ // method of the String or other non primitive class or can override
6+ // the equals method in the class whose object is being created.
7+
8+ class Personnel {
9+ private int id ;
10+ private String name ;
11+
12+ public Personnel (int id , String name ) {
13+ this .id = id ;
14+ this .name = name ;
15+ }
16+
17+ @ Override
18+ public String toString () {
19+ System .out .println ("Inside toString() method." );
20+ return "Person [id=" + id + ", name=" + name + "]" ;
21+ }
22+
23+ @ Override
24+ public int hashCode () {
25+ System .out .println ("Inside hash code" );
26+ final int prime = 31 ;
27+ int result = 1 ;
28+ int result1 = 0 ;
29+ result = prime * result + id ;
30+ System .out .println (result );
31+ result1 = prime * result + name .hashCode ();
32+ System .out .println (result1 );
33+ result = prime * result + ((name == null ) ? 0 : name .hashCode ());
34+ System .out .println (result );
35+ System .out .println (name .hashCode ());
36+ return result ;
37+ }
38+
39+ // Right click, select "Source" and select "Generate hashCode() and equals()"
40+ // This will create the equals method below.
41+
42+ // If this method is not present then the program will call the
43+ // equals method from the Object class.
44+
45+ @ Override
46+ public boolean equals (Object obj ) {
47+ System .out .println ("--> Person [id=" + id + ", name=" + name + "]" );
48+ if (this == obj )
49+ return true ;
50+ System .out .println ("A" ); // Included to find which code is getting executed.
51+ if (obj == null )
52+ return false ;
53+ System .out .println ("B" );
54+ if (getClass () != obj .getClass ())
55+ return false ;
56+ System .out .println ("C" );
57+ Personnel other = (Personnel ) obj ;
58+ if (id != other .id )
59+ return false ;
60+ System .out .println ("D" );
61+ if (name == null ) {
62+ if (other .name != null )
63+ System .out .println ("E" );
64+ return false ;
65+ } else if (!name .equals (other .name ))
66+ return false ;
67+ System .out .println ("F" );
68+ return true ;
69+ }
70+
71+ }
72+
73+ public class EqualsMethod {
74+
75+ public static void main (String [] args ) {
76+ Personnel p1 = new Personnel (2 , "John" );
77+ Personnel p2 = new Personnel (2 , "John" );
78+
79+ System .out .print ("Is object p1 = p2? " );
80+ System .out .println (p1 == p2 );
81+ System .out .println ("-----------------------------" );
82+
83+ // Try the below statement by commenting the equals method
84+ // mentioned in the Personnel class above. The output will be "false" as
85+ // it calls the equals method from the Object class by default.
86+
87+ // Hence you need to override the Object.equals() method
88+ // as seen in the Personnel class above.
89+ System .out .println ("Checking again with eguals method - " + p1 .equals (p2 ));
90+ System .out .println ("-----------------------------" );
91+
92+ p1 = p2 ;
93+ System .out .print ("Is object p1 = p2? " );
94+ System .out .println (p1 == p2 );
95+ System .out .println ("-----------------------------" );
96+ System .out .println ("Checking again with eguals method - " + p1 .equals (p2 ));
97+ System .out .println ("-----------------------------" );
98+
99+ p2 = new Personnel (4 , "Michael" );
100+ System .out .print ("Is object p1 = p2? " );
101+ System .out .println (p1 == p2 );
102+ System .out .println ("-----------------------------" );
103+ System .out .println ("Checking again with eguals method - " + p1 .equals (p2 ));
104+ System .out .println ("-------------DOUBLE----------------" );
105+
106+ Double v1 = 7.2 ;
107+ Double v2 = 7.2 ;
108+
109+ System .out .println (v1 == v2 );
110+ System .out .println (v1 .equals (v2 ));
111+ System .out .println ("-------------INTEGER---------------" );
112+
113+ Integer n1 = 6 ;
114+ Integer n2 = 6 ;
115+ System .out .println (n1 == n2 );
116+ System .out .println (n1 .equals (n2 ));
117+ System .out .println ("--------------STRING--------------" );
118+
119+ String t1 = "Hello" ;
120+ String t2 = "Hello" ;
121+ String t3 = new String ("Hello" );
122+ String t4 = "Helloabskj" .substring (0 , 5 );
123+
124+ System .out .println ("Compare t1 with t2: " + t1 == t2 );
125+ System .out .println ("Compare t1 with t2: " + t1 .equals (t2 ));
126+ System .out .println ("Compare t1 with t3: " + t1 == t3 );
127+ System .out .println ("Compare t1 with t3: " + t1 .equals (t3 ));
128+ System .out .println ("t4 = " + t4 );
129+ System .out .println ("Compare t1 with t4: " + t1 == t4 );
130+ System .out .println ("Compare t1 with t4: " + t1 .equals (t4 ));
131+
132+ System .out .println ("------------HASH CODE-----------------" );
133+ System .out .println (new Object ());
134+ Object obj1 = new Object ();
135+ System .out .println (obj1 .hashCode ());
136+ System .out .println (p2 .hashCode ());
137+ }
138+ }
0 commit comments