`
HelloSure
  • 浏览: 308391 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

关于Java包装类装箱拆箱的小例子

 
阅读更多
简单来说:装箱就是把值类型转变为引用类型,拆箱就是把引用类型转变为值类型
其实这东西没什么好说的,上代码看看就明白了:
/** 
 * @author  hellosure 
 * @time 2011-7-27 上午8:10:46 
 * @description:装箱拆箱例子
 */
public class Test {

	public static void main(String arg[]) {
		int v1 = 100;
		int v2 = 100;
		//自动装箱
		Integer autovalue1  =   100 ;
		Integer autovalue2  =   100 ;
		//手动装箱两种方式
		Integer value1 = Integer.valueOf(v1);
		Integer value2 = Integer.valueOf(v2);
		Integer va1 = new Integer(v1);
		Integer va2 = new Integer(v2);
		//自动拆箱
		int autov1 = autovalue1;
		int autov2 = autovalue2;
		//手动拆箱
		int vv1 = value1.intValue();
		int vv2 = value2.intValue();
		
		
		System.out.println(" v1 == v2 is "+ (v1 == v2));
		System.out.println(" autovalue1 == autovalue2 is "+ (autovalue1 == autovalue2));
		System.out.println(" value1 == value2 is  " + (value1 == value2));
		System.out.println(" va1 == va2 is "+ (va1 == va2));
		System.out.println(" va1 equals va2 is "+ (va1.equals(va2)));
		System.out.println(" autov1 == autov2 is "+ (autov1 == autov2));
		System.out.println(" vv1 == vv2 is "+ (vv1 == vv2));

		System.out.println("-----------------------------------------------------");
		
		String strv1 = "100";
		String strv2 = "100";
		String stringv1 = new String("100");
		String stringv2 = new String("100");
		Integer strvalue1 = Integer.parseInt(strv1);
		Integer strvalue2 = Integer.parseInt(strv2);
		Integer stringvalue1 = Integer.parseInt(stringv1);
		Integer stringvalue2 = Integer.parseInt(stringv2);
		Integer newstrv1 = new Integer(strv1);
		Integer newstrv2 = new Integer(strv2);

		System.out.println(" strv1 == strv2 is "+ (strv1 == strv2));
		System.out.println(" stringv1 == stringv2 is "+ (stringv1 == stringv2));
		System.out.println(" stringv1 equals stringv2 is "+ (stringv1.equals(stringv2)));
		System.out.println(" strvalue1 == strvalue2 is "+ (strvalue1 == strvalue2));
		System.out.println(" stringvalue1 == stringvalue2 is "+ (stringvalue1 == stringvalue2));
		System.out.println(" newstrv1 == newstrv2 is "+ (newstrv1 == newstrv2));
		System.out.println(" newstrv1 equals newstrv2 is "+ (newstrv1.equals(newstrv2)));
		
		System.out.println("-----------------------------------------------------");
		
		int v3 = 200;
		int v4 = 200;
		//自动装箱
		Integer autovalue3  =   200 ;
		Integer autovalue4  =   200 ;
		//手动装箱两种方式
		Integer value3 = Integer.valueOf(v3);
		Integer value4 = Integer.valueOf(v4);
		Integer va3 = new Integer(v3);
		Integer va4 = new Integer(v4);
		//自动拆箱
		int autov3 = autovalue3;
		int autov4 = autovalue4;
		//手动拆箱
		int vv3 = value3.intValue();
		int vv4 = value4.intValue();
		
		
		System.out.println(" v3 == v4 is "+ (v3 == v4));
		System.out.println(" autovalue3 == autovalue4 is "+ (autovalue3 == autovalue4));
		System.out.println(" value3 == value4 is  " + (value3 == value4));
		System.out.println(" va3 == va4 is "+ (va3 == va4));
		System.out.println(" va3 equals va4 is "+ (va3.equals(va4)));
		System.out.println(" autov3 == autov4 is "+ (autov3 == autov4));
		System.out.println(" vv3 == vv4 is "+ (vv3 == vv4));

		System.out.println("-----------------------------------------------------");
		
		String strv3 = "200";
		String strv4 = "200";
		String stringv3 = new String("200");
		String stringv4 = new String("200");
		Integer strvalue3 = Integer.parseInt(strv3);
		Integer strvalue4 = Integer.parseInt(strv4);
		Integer stringvalue3 = Integer.parseInt(stringv3);
		Integer stringvalue4 = Integer.parseInt(stringv4);
		Integer newstrv3 = new Integer(strv3);
		Integer newstrv4 = new Integer(strv4);

		System.out.println(" strv3 == strv4 is "+ (strv3 == strv4));
		System.out.println(" stringv3 == stringv4 is "+ (stringv3 == stringv4));
		System.out.println(" stringv3 equals stringv4 is "+ (stringv3.equals(stringv4)));
		System.out.println(" strvalue3 == strvalue4 is "+ (strvalue3 == strvalue4));
		System.out.println(" stringvalue3 == stringvalue4 is "+ (stringvalue3 == stringvalue4));
		System.out.println(" newstrv3 == newstrv4 is "+ (newstrv3 == newstrv4));
		System.out.println(" newstrv3 equals newstrv4 is "+ (newstrv3.equals(newstrv4)));
		
		System.out.println("-----------------------------------------------------");
		
	}
}

运行结果:
 v1 == v2 is true
 autovalue1 == autovalue2 is true
 value1 == value2 is  true
 va1 == va2 is false
 va1 equals va2 is true
 autov1 == autov2 is true
 vv1 == vv2 is true
-----------------------------------------------------
 strv1 == strv2 is true
 stringv1 == stringv2 is false
 stringv1 equals stringv2 is true
 strvalue1 == strvalue2 is true
 stringvalue1 == stringvalue2 is true
 newstrv1 == newstrv2 is false
 newstrv1 equals newstrv2 is true
-----------------------------------------------------
 v3 == v4 is true
 autovalue3 == autovalue4 is false
 value3 == value4 is  false
 va3 == va4 is false
 va3 equals va4 is true
 autov3 == autov4 is true
 vv3 == vv4 is true
-----------------------------------------------------
 strv3 == strv4 is true
 stringv3 == stringv4 is false
 stringv3 equals stringv4 is true
 strvalue3 == strvalue4 is false
 stringvalue3 == stringvalue4 is false
 newstrv3 == newstrv4 is false
 newstrv3 equals newstrv4 is true
-----------------------------------------------------

小结:
  • 对于new创建出的两个对象,用==比较肯定是不同的,因为指向的不是同一内存
  • Integer装箱过程中调用的是valueOf方法,而 valueOf方法对值在-128到127之间的数值缓存了,源代码如下:

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

可见,Integer缓存中有一个静态的Integer数组,在类加载时就将-128 到 127 的Integer对象创建了,并保存在cache数组中,一旦程序调用valueOf 方法,如果i的值是在-128 到 127 之间就直接在cache缓存数组中去取Integer对象。
2
3
分享到:
评论

相关推荐

    Java入门教程(微学苑)-part1

    3.19 Java包装类、拆箱和装箱详解 54 3.20 包装类的应用 54 3.20.1.1 1) 实现 int 和 Integer 的相互转换 54 3.20.1.2 2) 将字符串转换为整数 55 3.20.1.3 3) 将整数转换为字符串 55 3.21 自动拆箱和装箱 56 3.22 再...

    数据结构与算法分析Java语言描述(第二版)

    Java51.4.1 使用Object表示泛型1.4.2 基本类型的包装1.4.3 使用接口类型表示泛型1.4.4 数组类型的兼容性1.5 利用Java5泛性实现泛型特性成分1.5.1 简单的泛型类和接口1.5.2 自动装箱/拆箱1.5.3 带有限制的通配符...

    数据结构与算法分析_Java语言描述(第2版)]

    Java51.4.1 使用Object表示泛型1.4.2 基本类型的包装1.4.3 使用接口类型表示泛型1.4.4 数组类型的兼容性1.5 利用Java5泛性实现泛型特性成分1.5.1 简单的泛型类和接口1.5.2 自动装箱/拆箱1.5.3 带有限制的通配符...

    Java开发技术大全 电子版

    10.2一个关于泛型的简单例子305 10.3带两个类型参数的泛型类308 10.4有界类型309 10.5通配符参数311 10.6泛型方法313 10.7泛型接口315 10.8泛型类的继承317 10.8.1以泛型类为父类317 10.8.2以非泛型类为父类...

    数据结构与算法分析 Java语言描述第2版

    Java51.4.1 使用Object表示泛型1.4.2 基本类型的包装1.4.3 使用接口类型表示泛型1.4.4 数组类型的兼容性1.5 利用Java5泛性实现泛型特性成分1.5.1 简单的泛型类和接口1.5.2 自动装箱/拆箱1.5.3 带有限制的通配符...

    数据结构与算法分析_Java语言描述(第2版)

    1.5.2 自动装箱/拆箱 1.5.3 带有限制的通配符 1.5.4 泛型static方法 1.5.5 类型限界 1.5.6 类型擦除 1.5.7 对于泛型的限制 1.6 函数对象 小结 练习 参考文献 第2章 算法分析 2.1 数学基础 2.2 模型 2.3 要分析的...

    数据结构与算法分析-Java语言描述(第2版)_2_2

    java5 1.4.1 使用object表示泛型 1.4.2 基本类型的包装 1.4.3 使用接口类型表示泛型 1.4.4 数组类型的兼容性 1.5 利用java5泛性实现泛型特性成分 1.5.1 简单的泛型类和接口 1.5.2 自动装箱/拆箱....

    数据结构与算法分析-Java语言描述(第2版)_1_2

    java5 1.4.1 使用object表示泛型 1.4.2 基本类型的包装 1.4.3 使用接口类型表示泛型 1.4.4 数组类型的兼容性 1.5 利用java5泛性实现泛型特性成分 1.5.1 简单的泛型类和接口 1.5.2 自动装箱/拆箱....

    阿里巴巴编码规范 基础技能认证 考题分析(考题+答案).docx

    因为JAVA的自动装箱与拆箱机制,不需要根据场景来区分数据类型。 D .所有的局部变量推荐使用基本数据类型。 多选 12.关于索引的设计,下列哪些说法符合《阿里巴巴Java开发手册》:ACD A .对varchar类型的字段...

    c#学习笔记.txt

    2, 装箱和拆箱(取消装箱) 装箱是值类型到 object 类型或到该值类型所实现的任何接口类型的隐式转换。将一个值的值装箱会分配一个对象实例并将该值复制到新的对象中。关键字 object. 取消装箱是从 object 类型到值...

Global site tag (gtag.js) - Google Analytics