The difference between Java foundation (basis) ----- == and equals () of

1. equals method

      We can only handle a reference type variable, whether comparing two objects are equal. Compares the two address values ​​of reference variables are equal, if part of the String class, but also the comparison values ​​are the same

package com.practice;

public class Practice {
    public static void main(String[] args) {
        String s1 = new String("AA");
        String s2 = new String("AA");
        String s3 = "BB";
        String s4 = "BB";
        String s5 = new String("BB");
        System.out.println(s1.equals(s2)); //返回true
        System.out.println(s3.equals(s4)); //返回true
        System.out.println (s1.equals (S5)); // returns to false 
        System.out.println (s4.equals (S5)); // Returns to true 
    } 
}

2. ==

      For basic data types, comparing two values ​​are equal; for a reference type variable, comparing the address value is equal to two reference variables

package com.practice;

public class Practice {
    public static void main(String[] args) {
        String s1 = new String("AA");
        String s2 = new String("AA");
        int m=2;
        int n=2;
        System.out.println(s1==s2); //返回false
        System.out.println(m==n); //返回true
    }
}

 

      

Guess you like

Origin www.cnblogs.com/fengfuwanliu/p/11408999.html