Hi
Firstly, your Obj1 and Obj3 are not refering to the same object. Whenever you do a new it will create a seperate object in memory.
I tried you code with both equals and == but it gave false both time. This is what is expected.
For Object class, since the equals and Hashcode methods are not overriden, it checks for object reference. For Object both equals and == mean same.
Now, the reason why people would ask you to override equals and hashcode is to define you own way of distinguishing between two objects. As a simple example, if you have 2 Objects of Dog class, you need to define why these two Dogs are different. What rule
Java should use to distinguish between them.
For
String class, the equals and Hashcode are overriden.
Try below code and see the difference(I have used object of class String):
class B
{
public static void main(String ar[])
{
Object obj1=new String("Hello");
Object obj2=new String("Hi");
Object obj3=new String("Hello");
if(obj1.equals(obj3))
//if(obj1 == obj3)
System.out.println("True");
else
System.out.println("False");
}
}