栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java基础学习之包装类的使用以及理解自动装箱和自动拆箱

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java基础学习之包装类的使用以及理解自动装箱和自动拆箱

包装类的使用以及理解自动装箱和自动拆箱 一、概念

简单的说:

装箱就是 自动将基本数据类型转换为包装器类型;拆箱就是 自动将包装器类型转换为基本数据类型。

例如:

Integer i = 10;  //装箱
int n = i;   //拆箱
二、基本包装类引入 2.1 基本类型包装类的产生

在实际程序使用中,程序界面上用户输入的数据都是以字符串类型进行存储的。而程序开发中,我们需要把字符串数据,根据需求转换成指定的基本数据类型,如年龄需要转换成int类型,考试成绩需要转换成double类型等

概述:将基本的数据类型封装成对象的好处在于可以在对象中定义更多的功能方法操作该数据

常见操作之一:用于基本数据类型与字符串之间的转换

下面是基本数据类型与对应的包装类型。

基本数据类型包装类型
byteByte
booleanBoolean
shortShort
charCharacter
intInteger
longLong
floatFloat
doubleDouble
2.2 包装类的常用方法 2.2.1Integer类其他方法

十进制转成二进制toBinarString(int)十进制转成八进制toOctalString(int)十进制转成十六进制toHexString(int)

【注意】:三个方法,返回值都是以String 形式出现

public static void function_1(){
	System.out.println(Integer.toBinaryString(99));
    System.out.println(Integer.toOctalString(99));
	System.out.println(Integer.toHexString(999));
}
2.2.2 Integer 类的构造方法

public Integer( int value)public Integer ( String s): 注意:这个字符串由数字组成

public class Demo2 {
    public static void main(String[] args) {
        //方式1:
       int i=10;
       Integer ii =new Integer(10);
        System.out.println(ii);//10
 
        //方式2:
        String s= "111";
        Integer iii =new Integer(s);
        System.out.println(iii);//111
    }
}
2.2.3 基本数据类型类型和包装类的相互转换
public class WrapperTest {
	public static void main(String[] args) {
		
		//基本数据类型--->包装类 调用包装类的构造器
		int num = 10;
		//System.out.println(num.toString());
		Integer n1 = new Integer(num);
		System.out.println(n1.toString());//10
		Integer n2 = new Integer("123");
		System.out.println(n2.toString());//123
		Integer n4 = num; //自动装箱
		int a = n4;//自动拆箱
//		Integer n3 = new Integer("123abc");//异常java.lang.NumberFormatException
//		System.out.println(n3.toString());
		
		Float f1 = new Float(12.3f);
		Float f2 = new Float("12.3f");
		System.out.println(f1.toString());//12.3
		System.out.println(f2.toString());//12.3
		
		Boolean b1 = new Boolean(true);
		Boolean b2 = new Boolean("true");
		Boolean b3 = new Boolean("true123");//特殊,可以查看源码
		System.out.println(b3.toString());//false
		
		
		Jelly order = new Jelly();
		System.out.println(order.isMan);//false
		System.out.println(order.isWonman);//null
		
		
		System.out.println("*********************************");
		//包装类----->基本数据类型   调用包装类的xxxValue()
		
		System.out.println(n1.intValue());//10
		
		
		//基本数据类型,包装类---->String
		int num3 = 10;
		//方式一:连接运算
		String str1 = num3 + "";
		//方式二:调用String中重载的valueOf()方法
		String str2 = String.valueOf(num3);
		
		
		Double d4 = new Double(12.5);
		String str3 = String.valueOf(d4);
		System.out.println(str3);
		
		
		//String---->基本数据类型,包装类 调用包装类的parsexxx()方法
		
		String str4 = "123";
		
		//错误的情况
		//int num4 = (int)str4;
		//Integer n5 = (Integer)str4;
		
		int num5 = Integer.parseInt(str4);
		
		//常见面试题
		Object object1 = true ? new Integer(1) : new Double(2.0);
		System.out.println(object1);//1.0 解释:执行三元运算符时,前后数据类型要一致,会出现类型提升(int->double)
		
		
		Integer i = new Integer(1);
		Integer j = new Integer(1);
		System.out.println(i == j);//false
		
		Integer m = 1;
		Integer n = 1;
		System.out.println(m == n);//true
		
		Double b = 1.0;
		Double c = 1.0;
		System.out.println(b == c);//false
		
		Integer x = 128; //相当于new Integer(128)
		Integer y = 128;
		System.out.println(x == y);//false
		//解释:Integer内部定义了IntegerCache结构,IntegerCache中定义了Integer[],
		//保存了冲-128-127范围的整数。如果我们使用自动装箱方式,给Integer赋值的范围在
		//-128-127时,可以直接使用数组中的元素,不用再去new了,目的:提高效率
	} 
}

class Jelly{
	boolean isMan;
	Boolean isWonman;
}
三、装箱和拆箱的原理

装箱过程是通过调用 包装器的**valueOf方法**实现的,而拆箱过程是通过调用包装器的 xxxValue方法实现的。

自动装箱,拆箱的 好处: 基本类型和引用类直接运算

【注意】:建议判断是否为null,然后再使用

自动装箱:

使用Integer.valueOf(整数值)返回一个封装了该整数值的Integer对象即把基本类型转为引用类型

自动拆箱:

使用Integer 对象 .intValue()返回 Integer对象中封装的整数值把引用类型转为基本数据类型 四、 几个题目

1、进行比较前会自动拆箱

class AutoUnboxingTest {
 
    public static void main(String[] args) {
        Integer a = new Integer(3);
        Integer b = 3;                  // 将3自动装箱成Integer类型
        int c = 3;
        System.out.println(a == b);     // false 两个引用没有引用同一对象
        System.out.println(a == c);     // true a自动拆箱成int类型再和c比较
    }
}

2、输出的结果是什么?

public class Test03 {
 
    public static void main(String[] args) {
        Integer f1 = 100, f2 = 100, f3 = 150, f4 = 150;
 
        System.out.println(f1 == f2);
        System.out.println(f3 == f4);
    }
}

这四个变量都是 Integer对象引用,所以下面的 ==运算比较的不是值而是引用。装箱的本质是什么呢?当我们给一个 Integer对象赋一个 int值的时候,会调用 Integer类的静态方法 valueOf。所以整形字面量的值 在-128到127 之间,那么不会new新的Integer 对象,而是直接引用常量池中的 Integer 对象,所以本题中

f1==f2 的true ;f3==f4的值是false。

五、包装类应用场景

1、集合类泛型只能是包装类;

// 编译报错
List list1 = new ArrayList<>();

// 正常
List list2 = new ArrayList<>();

2、成员变量不能有默认值;

private int status;

基本数据类型的成员变量都有默认值,如以上代码 status默认值为 0,如果定义中 0代表失败,那样就会有问题,这样只能使用包装类 Integer,它的默认值为null,所以就不会有默认值影响。

3、方法参数允许定义空值;

private static void test1(int status){	System.out.println(status);}

看以上代码,方法参数定义的是基本数据类型int,所以必须得传一个数字过来,不能传 null,很多场合我们希望是能传递 null 的,所以这种场合用包装类比较合适。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/709132.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号