The equals method that comes along java.lang.Object provides an equal method which does the following

<pre>
public boolean equals(Object o)
{
if(this == 0) return true;
}
</pre>

But the above implementation only for Integer, Boolean, Character and Other Wrapper classes because it is overridden.

When we define our own class and define equals the below is how its going to be.

<pre>

class mac
{
int Age;
}
</pre>

Now the below code does not give any output until equals method is overridden if you comment out   objmac2 = objmac1;      .

<pre>
Integer num1 = new Integer(5);
Integer num2 = new Integer(5);

mac objmac1 = new mac();
mac objmac2 = new mac();

objmac2 = objmac1;

objmac1.Age = 25;
objmac2.Age = 25;

if(objmac1 == objmac2)
System.out.println(“They are same”);

if(objmac1.equals(objmac2))
System.out.println(“Objects are same”);

</pre>

 

 

 

 

 

 

Leave a reply