java integer String之equals vs ==

Integer a = new Integer(123);
Integer b = new Integer(123);

System.out.println(a == b);
System.out.println(a.equals(b));

System.out.println(a.equals(123));
System.out.println(a == 123);

输出

false
true
true
true

 

String a = new String("nihao");
String b = new String("nihao");

System.out.println(a==b);
System.out.println(a.equals(b));

System.out.println(a=="nihao");
System.out.println(a.equals("nihao"));

输出

false
true
false
true

 

posted @ 2016-07-10 18:19  范世强  阅读(528)  评论(0编辑  收藏  举报