@Test
public void test() {
Integer i3 =128;
Integer i4=128;
boolean integerGT127=i3==i4;
//false
System.out.println("Integer 128==128"+integerGT127);
Integer i1 =127;
Integer i2=127;
boolean integerLt127=i1==i2;
      //true
System.out.println("Integer 127==127"+integerLt127);
Integer i5 = 5001;
boolean unBoxingInteger=i5 > 500 && i5 < 1000;
System.out.println("Integer unbox "+integerLt127);
Integer i6=500,i7=1000;
boolean unBoxingInteger2=i5.compareTo(i6)>0&&i5.compareTo(i7)<0;
System.out.println("Integer compare "+integerLt127);
//装箱 将 10(int)转换为了 Integer
Integer i9=10;
//拆箱 将i9(Integer)转换为了int
int i10=i9;

}

输出结果:

1 .Integer 128==128false
2 .Integer 127==127true
3 .Integer unbox true
4 .Integer compare true

为什么会出现128==128的结果?输出结果表明i1和i2指向的是同一个对象,而i3和i4指向的是不同的对象。IntegerCache 下面这段代码是Integer的valueOf方法的具体实现:

private static class IntegerCache {
static final int low = -128;
static final int high;
static final Integer cache[]; static {
// high value may be configured by property
int h = 127;
String integerCacheHighPropValue =
VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
if (integerCacheHighPropValue != null) {
try {
int i = parseInt(integerCacheHighPropValue);
i = Math.max(i, 127);
// Maximum array size is Integer.MAX_VALUE
h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
} catch( NumberFormatException nfe) {
// If the property cannot be parsed into an int, ignore it.
}
}
high = h; cache = new Integer[(high - low) + 1];
int j = low;
for(int k = 0; k < cache.length; k++)
cache[k] = new Integer(j++); // range [-128, 127] must be interned (JLS7 5.1.7)
assert IntegerCache.high >= 127;
} private IntegerCache() {}
}

在通过valueOf方法创建Integer对象的时候,如果数值在[-128,127]之间,便返回指向IntegerCache.cache中已经存在的对象的引用;否则创建一个新的Integer对象。

Integer.valueOf(10)

 public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}

Integer.valueOf(10);
Double.valueOf(23.00);
Boolean.valueOf(true);
Boolean b1=true,b2=true;
//true Boolean.valueOf(true);
// final修饰的对象只能指向唯一一个对象,不可以再将它指向其他对象,而static final修饰的对象则可以使一个常量真正做到不被修改
System.out.println(b1==b2);
// true Boolean.valueOf(true);
System.out.println(b1.equals(b2));
Double d1=20.00,d2=20.00;
// false Double.valueOf
System.out.println(d1==d2);
//true
System.out.println(d1.equals(d2));
Boolean.valueOf(true);返回了一个static final 的对象 ,final修饰的对象只能指向唯一一个对象,不可以再将它指向其他对象,而static final修饰的对象则可以使一个常量真正做到不被修改
public static Boolean valueOf(boolean b) {
return (b ? TRUE : FALSE);
} /**
* The {@code Boolean} object corresponding to the primitive
* value {@code true}.
*/
public static final Boolean TRUE = new Boolean(true); /**
* The {@code Boolean} object corresponding to the primitive
* value {@code false}.
*/
public static final Boolean FALSE = new Boolean(false);
Double.valueOf(23.00);new一个新的对象
    public static Double valueOf(double d) {
return new Double(d);
}

最新文章

  1. Attempt to insert non-property list object 报错原因
  2. WPF自定义空心文字
  3. ios 修改webView字体
  4. Ubuntu 16.04 LTS更新
  5. acdream.A Very Easy Triangle Counting Game(数学推导)
  6. Eclipse HibernateTools安装
  7. iOS 8 自动布局sizeclass和autolayout的基本使用
  8. 大型项目使用Automake/Autoconf完成编译配置
  9. WKWebView与Js交互
  10. CSS3伪类实现动画旋转效果
  11. Coins(多重背包+二进制优化)
  12. Sqlserver将数据从一张表插入到另一张表
  13. linux 获取CPU个数
  14. LXC学习实践(1)LXC的概念和用途
  15. PHPstorm快捷键Ctrl + Alt + Left 或Right不起作用
  16. 【java多线程】队列系统之ArrayBlockingQueue源码
  17. Python3 tkinter基础 Canvas create_text 在画布上添加文字
  18. Kali Linux下常用软件安装及配置
  19. Confluence 6 LDAP 用户结构设置
  20. Linux下配置Nginx(在root的/etc/rc.local里配置开机启动功能http://tengine.taobao.org/)

热门文章

  1. iOS MJExtension的使用
  2. [LeetCode] 63. Unique Paths II_ Medium tag: Dynamic Programming
  3. 用python脚本获取运行环境中的module 列表
  4. 解决centos7.x图形化界面卡死(鼠标能动,但不能点击)问题
  5. linux----------fedora 27 如何打开ssh,可以远程链接
  6. python数据类型之列表类型
  7. 网络库压力测试:mongols VS evpp
  8. Spring Boot默认的JSON解析框架设置
  9. Android SDK Manager for Mac 在线更新镜像地址截至2017-10-01亲测有效
  10. JavaScript数组对象常用方法