-2
If I have two reference object with different name in main class Such as:

AA aa = new AA();
AA bb = new AA();

and if i compare it using aa.equals(bb); then what it will return. 

and if i will use 

BB bb = new BB();

and i compare it using aa.equals(bb);

Then what is difference both of them

I always confused null behaviour of object.

royhowie
  • 11,075
  • 14
  • 50
  • 67
abhi
  • 759
  • 6
  • 15
  • 3
    There are many questions like this on SO http://stackoverflow.com/questions/1643067/whats-the-difference-between-equals-and – Prasad Kharkar Jun 22 '13 at 05:55

1 Answers1

3

The operator, ==, tests to see if two object reference variables refer to the exact same instance of an object.

The method, .equals(), tests to see if the two objects being compared to each other are equivalent -- but they need not be the exact same instance of the same object.

Example #1:

Integer i = new Integer(10);
Integer j = i;

in the above code. i == j is true because both i and j refer to the same object.

Example #2:

Integer i = new Integer(10);
Integer j = new Integer(10);

In the above code, i == j is false because, although they both have the value 10, they are two different objects.

Also, in the above code, i.equals(j) is true because although they are two different objects, they are equivalent in the fact that they represent the same number, 10.

Matthieu
  • 2,736
  • 4
  • 57
  • 87
Sain Pradeep
  • 3,119
  • 1
  • 22
  • 31