• equals 与 ==

    equals 与 ==

    • == 比较对象的引用
    • equals 比较对象的值

    题目一

    1. public class Test{
    2. public static void main(String[] args){
    3. String s1 = "abc";
    4. String s2 = s1;
    5. String s3 = new String("abc");
    6. String s4 = new String("abc");
    7. String s5 = "abc";
    8. System.out.println(s1==s5);
    9. System.out.println(s1==s2);
    10. System.out.println(s1.equals(s2));
    11. System.out.println(s3==s4);
    12. System.out.println(s1.equals(s4));
    13. System.out.println(s3.equals(s4));
    14. }
    15. }

    输出是 true true true false true true