对象的比较

 

1.==运算符和equals()方法的区别

public class Compare {
    public static void main(String[] args) {
        String s1 = new String("abc");
        String s2 = "abc";
        String s3 = s1;
        System.out.println("s1==s2: " + (s1==s2));
        System.out.println("s2==s3: " + (s2==s3));
        System.out.println("s1==s3: " + (s1==s3));
        System.out.println("s1 equals s2: "+s1.equals(s2));
    }
}

运行结果:

s1==s2: false
s2==s3: false
s1==s3: true
s1 equals s2: true

==:比较的是对象引用的地址是否相等

equals():比较的是对象所引用所指的内容是否相等。

 

点赞