Possible Duplicate:
How Does the toString(), ==, equals() object methods work differently or similarly on reference and primitive types?
I am trying to understand the difference between == and equals to operator in Java. e.g. == will check if it is the same object while equals will compare the value of the object ... Then why do we use == for comparing primitive data types like int. Because if I have
int i =7; //and
int j = 6.
They are not the same object and not the same memory address in stack. Or does the == behaves differently for primitives comparison.??
Actually, == behaves identically for all variables: it tests whether the values of those variables are equal. In the case of Object obj, obj is a reference to an object. Since == tests whether two object references have the same value, it is testing whether they refer to the identical object (i.e., that the references are equal).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With