Java中equals与==的用法比较

1.equals用法

个人的归纳总结:

用于String类型之间的比较时,调用的是String类的equals()方法,比较的是字符串的内容而不是地址,并且只能用于比较String类型,因为StringBuffer和StringBuilder类中都没有equals()方法。

用于其他类型之间(包括含有一个Sting类型的情况)的比较时,以o1.equals(o2)为例: 若o1是String类型,则调用的时String类的equals方法,返回值为false;若o1不是String类型,则调用的是Object类的equals()方法(此时与==用法相同),比较的是引用变量的地址而不是内容。

private static void equalsAnal() {
        String s1 = "hello";
        String s2 = "hello";
        String s3 = new String("hello");
        String s4 = new String("hello");
        String s5 = new String(new StringBuffer("hello"));
       
        StringBuffer s6 = new StringBuffer("hello");
        StringBuffer s7 = new StringBuffer("hello");
       
        System.out.println(s1.equals(s2));//true
        System.out.println(s3.equals(s4));//true
       
        System.out.println(s1.equals(s3));//true
       
        System.out.println(s1.equals(s5));//true
        System.out.println(s3.equals(s5));//true
       
        System.out.println(s6.equals(s7));//false
        System.out.println(s1.equals(s6));//false
        System.out.println(s3.equals(s6));//false
        System.out.println(s5.equals(s6));//false
        System.out.println(s6.equals(s1));//false
    }

对于上述代码的结果,我们可以结合String类以及Object类的源码来进行分析:

//String类中的equals方法 
  public boolean equals(Object anObject) {
        if (this == anObject) {
            return true;
        }
        if (anObject instanceof String) {
            String anotherString = (String)anObject;
            int n = value.length;
            if (n == anotherString.value.length) {
                char v1[] = value;
                char v2[] = anotherString.value;
                int i = 0;
                while (n-- != 0) {
                    if (v1[i] != v2[i])
                        return false;
                    i++;
                }
                return true;
            }
        }
        return false;
    }

1    //Object类中的equals方法
2      public boolean equals(Object obj) {
3        return (this == obj);
4    }

2. ==用法

==是一种关系运算符,用于基本数据类型时,比较的是两个变量的值是否相等;用于引用类型时,比较的是两个引用变量的地址是否相同。

int a = 10, b = 10;
        char ch1 = 'A', ch2 = 'A';
        String str1 = "hello";
        String str2 = "hello";
        Integer n1 = new Integer(10);
        Integer n2 = new Integer(10);
       
        System.out.println(a == b);//true
        System.out.println(ch1 == ch2);//true
        System.out.println(str1 == str2);//true
        System.out.println(n1 == n2);//false

3.用法比较

String s1 = "hello";
        String s2 = "hello";
        String s3 = new String("hello");
        String s4 = new String("hello");
        String s5 = new String(new StringBuffer("hello"));
       
        StringBuffer s6 = new StringBuffer("hello");
        StringBuffer s7 = new StringBuffer("hello");
       
        System.out.println(s1.equals(s2));//true
        System.out.println(s1==(s2));//true
       
        System.out.println(s3.equals(s4));//true
        System.out.println(s3==(s4));//false
       
        System.out.println(s1.equals(s3));//true
        System.out.println(s1==(s3));//false       
       
        System.out.println(s1.equals(s5));//true
        System.out.println(s1==(s5));//false
       
        System.out.println(s3.equals(s5));//true
        System.out.println(s3==(s5));//false
       
        System.out.println(s6.equals(s7));//false
        System.out.println(s1.equals(s6));//false
        System.out.println(s3.equals(s6));//false
        System.out.println(s5.equals(s6));//false
        System.out.println(s6.equals(s1));//false

综上所述,equals和==用法相同的情况:

  • 字符串为基本数据类型String的变量;
  • 比较的是两个非String类型的引用变量。

猜你喜欢

转载自www.linuxidc.com/Linux/2017-10/147376.htm