Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

== operator for primitive data types [duplicate]

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.??

like image 645
zoom_pat277 Avatar asked Mar 23 '26 09:03

zoom_pat277


1 Answers

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).

like image 189
Ted Hopp Avatar answered Mar 24 '26 21:03

Ted Hopp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!