ITeye博客

字符串比较之 “==”和 “equals”

tonysmith · 2012-03-05 13:04 · Coding with Java · 编程语言 · Markdown

字符串比较之 “==”和 “equals”

提示:引用存放在栈里,实体存放在堆里。

       String a = "String A";

        String b = new String("String B");

        String c = a;

        String d = new String("String B");

请看堆栈示例图:

fuck ,为什么图片不能粘贴呢。鄙视iteye.附件里面有。

//打印hashcode值:

      System.out.println(a.hashCode());

        System.out.println(b.hashCode());

        System.out.println(c.hashCode());

        System.out.println(d.hashCode());

//打印结果

        System.out.println(a==c);

System.out.println(b==d);

        System.out.println(a.equals(c));

        System.out.println(b.equals(d));

Result:

1859651602

1859651603

1859651602

1859651603

true //a==c

false//b!=d

true//a.equals(c)

true//b.equals(d)

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。