sumathi,
in your first class, you can't do the following:
-------------------------
boolean b = true;
boolean b1 = false;
if ( b.equals(b1) ) {
System.out.println("true");
}
-------------------------
both b and b1 are primitive types and not Objects , and the equals method operate only on Objects.
I think the first class will not even compile.
ok , so that is out of the way, let us now talk about the difference between == and equals
the == operator evaluate both sides, and check if they are the same or in another way if the have "the same content"
for example, if you have 2 variables
int i1 = 9;
int i2 = 9;
if we use the == operator on them like this (i1==i2) we are trying to compare their values.
however you can't do (i1.equals(i2)) because as I have said, the equals only operate on Object and i1 and i2 are not objects.
now the equals operator
test NOT the values of the operands, BUT THE OBJECT REFERENCE ITSELF , (i.e. it test if both operands are of the same object)
lets take an example
say we have 2 objects
String s1 = "one";
String s2 = "one";
now if you know something about the JVM, you will know that since both s1 and s2 has the value "one", then both of them refer to the same location in memory (which will contain the value "one"), therefore they both refer to the same object
so if we say:
s1.equals(s2) , this will return true.
at the same time , if we say s1==s2 , this will return true.
BUT
if we have
String s1 = "one";
String s2 = new String("one");
now s1 and s2 points to different location in memory (because we forced s2 to be a new string ) , therefore if we say
s1.equals(s2) this will return false
BUT
if we say s1==s2 , this will return true since we are comparing not the object reference, we are comparing the real value that the objects are holding, in both s1 and s2 the have "one".
and finally
if we have
String s1 = "one";
String s2 = new String("two");
and say : s1.equals(s2) , this will return false (because they are 2 different OBJECTS )
and if we say (s1==s2) , this will return false, (because they have different values)
I hope that this is clear now
Maitham