黑马程序员技术交流社区

标题: == 与 equals 区别验证 [打印本页]

作者: xh_javaee    时间: 2016-9-13 00:19
标题: == 与 equals 区别验证
public class EqualsTest {
               public static void main(String[] args) {
                               String s1 = “abc”;
                               String s2 = s1;
                               String s5 = “abc”;
                               String s3 = new String(”abc”);
                               String s4 = new String(”abc”);
                               System.out.println(”== comparison : ” + (s1 == s5));
                               System.out.println(”== comparison : ” + (s1 == s2));
                               System.out.println(”Using equals method : ” + s1.equals(s2));
                               System.out.println(”== comparison : ” + s3 == s4);
                               System.out.println(”Using equals method : ” + s3.equals(s4));
               }
}

结果:
== comparison : true
== comparison : true
Using equals method : true
false
Using equals method :true




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2