The difference between "==" and "equals ()" in Java

==

  1. Can be used to compare basic data types and reference data types.
  2. The basic data type compares whether the values ​​are equal. Note: 34 and 34.0f return true by comparing with "=".
  3. The reference data type compares whether the address values ​​of the objects are equal. Note: The objects of the String class are stored in the constant pool of the method area. Once the space for a String class object is opened, the String class objects with the same string value are not Open up new space, but share the same address value.

equals()

  1. Can only be used for comparison of reference data types.
  2. By default, the equals () method of the Object class is called. The bottom layer is judged by "==", and the comparison is also whether the address values ​​of the two objects are equal.
  3. String class, Date class, wrapper class, etc. have rewritten the equals () method to see if the values ​​of the two objects compared are equal.
Published 5 original articles · Likes0 · Visits 52

Guess you like

Origin blog.csdn.net/za_zhi/article/details/105600335