[b]Integer比较[/b]
[b]输出如下[/b]
[list]
[*]呵呵呵
[*]哈哈哈
[/list]
[b]原因[/b]
Integer在-128到127的值之间使用的是缓存,所以为同一个对象。
我们可以看看源码
[img]http://dl2.iteye.com/upload/attachment/0119/7522/ca92c3f8-a71a-3ff8-8b93-c98edb40e1e6.png[/img]
public static void main(String[] args) {
Integer a = 100;
Integer b =100;
if (a >= b && a <= b && a != b) {
System.out.println("哈哈哈");
}else{
System.out.println("呵呵呵");
}
Integer c = 200;
Integer d =200;
if (c >= d && c <= d && c != d) {
System.out.println("哈哈哈");
}else{
System.out.println("呵呵呵");
}
}
[b]输出如下[/b]
[list]
[*]呵呵呵
[*]哈哈哈
[/list]
[b]原因[/b]
Integer在-128到127的值之间使用的是缓存,所以为同一个对象。
我们可以看看源码
[img]http://dl2.iteye.com/upload/attachment/0119/7522/ca92c3f8-a71a-3ff8-8b93-c98edb40e1e6.png[/img]