Integer自动装箱分析
先看看下面的代码会输出什么:
public static void main(String[] args) {
Integer i = 127;
Integer j = 128;
Integer ii = 127;
Integer jj = 128;
System.out.println( i==ii );
System.out.println( j==jj );
}
答案是:true,false
这是为什么呢?原因在于装箱动作是通过valueOf方法实现的,
在valueOf方法中采用了对象池。如果参数在-128~127之间,则
直接从对象池中获得对象,不在该范围之内的才通过new生成包装对象。
我们来看看源代码:
public static Integer valueOf(int i) {
assert IntegerCache.high >= 127;
if (i >= IntegerCache.low && i <= IntegerCache.high)
//low=-128,high=127
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
那么装箱的动作为什么看不到呢?你怎么知道一定是调用valueOf函数呢?
自动装箱是编译其行为。我们可以将代码编译后再进行反编译,看看JVM指令是什么。
下面是上面例子中反编译出来的部分代码:
Compiled from "IntegerTest.java"
public class Test.IntegerTest {
public Test.IntegerTest();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
public static void main(java.lang.String[]);
Code:
0: bipush 127
2: invokestatic #2 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
5: astore_1
6: sipush 128
9: invokestatic #2 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
}
我们可以看到自动装箱确实是调用valueOf函数
Integer自动装箱分析的更多相关文章
- Integer自动装箱和拆箱
Integer a=3; => Integer a=Integer.valueOf(3); /** *@description: 自动装箱和拆箱 *@auther: yangsj *@ ...
- 28.6 Integer 自动装箱和拆箱
public class IntegerDemo2 { public static void main(String[] args) { //自动装箱 // Integer i = new Integ ...
- Integer自动装箱拆箱bug,创建对象在-128到127
1 public class Demo3 { public static void main(String[] args) { Integer a = 1; Integer b = 2; Intege ...
- 理解JDK1.5的自动装箱拆箱
JDK1.5的升级引入了装箱和拆箱概念,简单说就是为了简化书写. JDK1.5之前,创建Integer对象是需要这么写的 Integer i = new Integer("3") ...
- JavaStudy——Java之自动拆箱与自动装箱
java基本类型介绍 java中,基本数据类型一共有8种,详细信息如下表: 类型 大小 范围 默认值 byte 8 -128 - 127 0 short 16 -32768 - 32768 0 int ...
- Java进阶之自动拆箱与自动装箱
序. java基本类型介绍 java中,基本数据类型一共有8种,详细信息如下表: 类型 大小 范围 默认值 byte 8 -128 - 127 0 short 16 -32768 - 32768 0 ...
- 由自动装箱和拆箱引发我看Integer源码
背景和问题 在看别人整理的资料时,看到如下一段代码: package com.sitech.test; /** * 自动装箱和拆箱 jdk1.6 * @author liaowp * */ publi ...
- 转!!Java学习之自动装箱和自动拆箱源码分析
自动装箱(boxing)和自动拆箱(unboxing) 首先了解下Java的四类八种基本数据类型 基本类型 占用空间(Byte) 表示范围 包装器类型 boolean 1/8 true|fal ...
- Integer.valueOf(int)及自动装箱内幕
Integer为什么要提供功能与new Integer(xx)一样的valueOf(xx)方法呢,看了源代码之后,我发现了惊人的内幕. public static Integer valueOf(in ...
随机推荐
- phpnow升级php版本 php-5.2.14-Win32升级至5.3.5
PHPNow自带的PHP版本为5.2.14,而最后一次更新在于2010-9-22.下面来升级PHP5.3.5: 1.下载安装文件: 先下载PHP5.3.5,下载地址:php-5.3.5-Win32-V ...
- [改善Java代码]对字符串排序 持一种宽容的心态
在Java中一涉及到中文处理就会冒出很多的问题来,其中的排序也是一个让人头疼的问题,看代码: import java.util.Arrays; public class Client { public ...
- New MVC World
Note: /Controllers:controllers respond to input from the browser,decide what to do with it,and retur ...
- Unity3d,OnMouseDown()不执行的原因总结
1.代码:看代码有没有附加上要点击的物体上: 2.碰撞:要点击的物体加了碰撞,位置大小都对:而且鼠标屏幕点击的点和它之间没有其他的碰撞遮挡(OnMouseDown()原理利用了射线): 3.相关的摄像 ...
- Unity3D除了在编辑器里,怎么用代码给一个Texture类型的变量赋值
resource.load上来一张贴图就行. using UnityEngine; using System.Collections; public class example : MonoBehav ...
- android使用Intent操作拨打号码发送短信
Activity程序Activity.java package com.example.intentcaseproject; import android.net.Uri; import androi ...
- Rebind and Rewind in Execution Plans
http://www.scarydba.com/2011/06/15/rebind-and-rewind-in-execution-plans/ Ever looked at an execution ...
- spring mvc 拦截器
拦截器作用:可以用于用户操作的安全检查,如:登录.权限等 package com.tool; import java.util.List; import javax.servlet.http.Http ...
- 转:ASP.NET中的SESSION实现与操作方法
在ASP.NET中,状态的保持方法大致有:ApplicationState,SessionState,Cookie,配置文件,缓存. ApplicationState 的典型应用如存储全局数据. Se ...
- 产品经理常用工具Axure、Visio、Mindmanager使用解析(摘)
如果想表现产品的业务流程,那么我建议使用Visio来绘制流程图.如果想表现产品的页面图文布局和页面的跳转关系,我建议使用axure.如果想表现产品的信息架构,我建议使用Mindmanager或Xmin ...