java2db

Difference between equals method and == operator in java



Both the equals method and == operator uses to compare two numbers or Strings . Internally the equals method uses == operator for comparison . Wrapper classes overridden the equals method .

 

equals method and == operator In case of  primitives , the == operator compare its values .  if  objects, it compares  addresses (location) .

equals method and == operatorThe equals method compares  values of the objects . But not the location .

 

equals method and == operator in Integer and Floating points :

java2db

equals method and == operator If the comparison between an object and a primitive using == operator , returns true ,because the  JVM internally applies unboxing( wrapper class to its corresponding primitive).

equals method and == operator The equals method returns true ,  it compares the object values . The actual implementation as follows .

 

equals method and == operator in Strings :

java2db

equals method and == operator object comparison using == operator returns false , because it compares the addresses .

 

 

Example for equals method and == operator:

 

 

Example implementation for equals method, to compare String objects :

 




Related Posts :
PL/SQL Operators
   [will not be published]



^ <