The difference between == and the equals Java in () of

1.  For the basic type (byte, short, int, long , char, float, double, boolean)

    == operator compares the value of the two numbers can not be equals () comparison, because it is not the object, equals () method of class Object

2. For reference types

   2.1 == address comparison is carried out

        Animal a custom class

 1 public class Animal {
 2 
 3     publicString name;
 4 
 5     public Animal(){
 6     }
 7 
 8     Animal(String name) {
 9         this.name = name;
10     }
11 }

      Creating Animal objects and compare ==

1 public class DemoEquals {
2     public static void main(String[] args) {
3         Animal cat1 = new Animal("傻猫");
4         Animal cat2 = new Animal("傻猫2");
5         System.out.println("cat1: " + cat1);
6         System.out.println("cat2: " + cat2);
7         System.out.println(cat1 == cat2);
8     }
9 }

  operation result

    

        analysis

        cat1 == cat2, comparing the address value, address values ​​from the results, operation is not the same, so cat1 == cat2 is false;

        Now we verify,

. 1  public  class DemoEquals {
 2      public  static  void main (String [] args) {
 . 3          Animal CAT1 = new new Animal ( "silly cat" );
 . 4          Animal CAT2 = new new Animal ( "silly Cat 2" );
 . 5          System.out.println ( "CAT1:" + CAT1);
 . 6          System.out.println ( "CAT2:" + CAT2);
 . 7          System.out.println ( "========" );
 . 8          CAT1 = CAT2;    // let cat1 cat2 objects and objects share a memory area 
9          System.out.println ( "cat1:" + cat1);
10         System.out.println("cat2: " + cat2);
11         System.out.println(cat1 == cat2);
12     }
13 }

        operation result 

        

        analysis

        It can be seen then, perform the cat1 = cat2, cat1 cat2 and the address value is the same, the comparison result is performed == true of. So, the application type, address comparison is carried out ==.

 

    2.2 equals () comparison

        First of all we need to know, equals (Object obj) This method is a method Object class definition. All of our custom class inherit the Object class, so also have equals () this method. Then we look at how the Object class is defined in this method

       

        this indicates the currently used equals method of the object, obj is the object parameter we pass. But here it is clearly == comparison, ah, should not do is address comparison. Why are we equals () can be value in comparing it with the String object? Because the String class overrides this method, equals () method is the default address comparison, after rewriting the String class, is a value comparison, then we look at how the source code is a rewrite of the String method.

       

        This code can understand just fine, I do not understand it does not matter, as long as know, String class overrides equals () method, so in order to compare the value, otherwise the default address comparisons are carried out. We may be instances

        Creating the Animal class

 1 public class Animal {
 2 
 3     publicString name;
 4 
 5     public Animal(){
 6     }
 7 
 8     Animal(String name) {
 9         this.name = name;
10     }
11 }

         Then create the Animal object equals comparisons

public  class DemoEquals {
     public  static  void main (String [] args) { 
        Animal CAT1 = new new Animal ( "silly cat" ); 
        Animal CAT2 = new new Animal ( "silly Cat 2" ); 
        System.out.println ( "CAT1:" + CAT1); 
        System.out.println ( "CAT2:" + CAT2); 
        System.out.println (cat1.equals (CAT2)); 
        System.out.println ( "========" ); 
        = cat2.name cat1.name ;    // change the name, so that the two objects of the same name 
        System.out.println ( "cat1:" ++ cat1);
        System.out.println("cat2: " + cat2);
        System.out.println(cat1.equals(cat2));
    }
}

        operation result

        

       analysis

        We can see and compare the results of previous comparative == the same, despite their name, but the result of the comparison is false, because we Animal object and does not override equals () method, so it automatically inherits the Object equals () method, or the address value comparison

        Now we rewrite equals () method in the Animal class

. 1  @Override
 2      public  Boolean equals (Object obj) {
 . 3          // override equals method of Object class 
. 4          IF (obj == the this ) return  to true ;
 . 5          IF (obj == null ) return  to false ;
 . 6          IF (obj the instanceof Animal ) {
 . 7              Animal Animal = (Animal) obj;   // type strong turn
 8              // because String name is a reference type, it is directly equals comparison value to be compared 
. 9              return animal.name.equals ( the this .name); 
10          }
 11          return  false ;
12      }

       Then we run the above example again

        operation result

       

       This time you can see, after rewriting the equals () method, the comparison result is the value of the result of the comparison.

 

3. Summary

    3.1 basic types :

          == to compare the address that can not be equals () comparison, because equals () method is defined in Object, only a reference to an object in order to use this method

    3.2 In reference types:

          == address comparison is performed. equals () default done is address comparison, if a class overrides equals () method, then it would see him rewriting the source code using any more. For example, the String class is a value comparison.

Guess you like

Origin www.cnblogs.com/pyexile/p/11426010.html