码迷,mamicode.com
首页 > 编程语言 > 详细

Java中Integer和int的异同

时间:2015-08-10 17:38:20      阅读:106      评论:0      收藏:0      [点我收藏+]

标签:

 1 public void Test1() {
 2         int a = 128;
 3         Integer b = 128;
 4         Integer c = 128;
 5         //Integer会自动拆箱成int,所以为ture
 6         System.out.println(a==b);
 7         System.out.println(a==c);
 8     }
 9     
10     @Test
11     public void Test2() {
12         Integer a = 127;
13         Integer b = 127;
14         System.out.println(a == b);//此处结果为true,应该没什么疑问Java在编译 Integer c时候编译成 Integer.valueOf(127)
15         
16         Integer c = 128;
17         Integer d = 128;
18         System.out.println(c == d);//此处结果为false,
19         /*Integer.valueOf()方法基于减少对象创建次数和节省内存的考虑,缓存了[-128,127]之间的数字。
20         此数字范围内传参则直接返回缓存中的对象。在此之外,直接new出来。*///源码如下
21         /* public static Integer valueOf(int i) {
22                 assert IntegerCache.high >= 127;
23                 if (i >= IntegerCache.low && i <= IntegerCache.high)
24                     return IntegerCache.cache[i + (-IntegerCache.low)];
25                 return new Integer(i);
26             }*/
27     }

 

Java中Integer和int的异同

标签:

原文地址:http://www.cnblogs.com/liujie-/p/4718273.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!