【Java笔记】包装类(Wrapper)与自动拆装箱的使用

针对八种基本数据类型定义相应的引用类型,就是包装类。

有了包装类,我们就可以将基本数据类型变成类,从而调用类的方法。

目录

八种包装类

基本数据类型与包装类的相互转换

自动装箱与自动拆箱

基本数据类型、包装类与String的相互转换

包装类常见面试题


八种包装类

基本数据类型包装类
byteByte
shortShort
intInteger
longLong
floatFloat
doubltDoublt
booleanBoolean
charCharacter

其中,Byte,Short,Integer,Long,Float,Doublt 的父类均为 Number

基本数据类型与包装类的相互转换

基本数据类型 ——> 包装类:调用包装类的构造器

public class WrapperTest {
	public static void main(String[] args) {
	    WT wt = new WT();
	    wt.test();
        System.out.println(order.isOK);
	    System.out.println(order.IsOK);
	}
}
class WT{
	public void test(){
		int num = 10;
		Integer in1 = new Integer(num); // 包装类,可以调用类的方法
		System.out.println(in1.toString());
		Integer in2 = new Integer("123");
		System.out.println(in2.toString());
//		Integer in3 = new Integer("123abc"); // 报错 NumberFormatException
//		System.out.println(in3.toString());

		Float f1 = new Float(12.3f);
		Float f2 = new Float("12.3");
		System.out.println(f1);
		System.out.println(f2);

		Boolean b1 = new Boolean(true);
		Boolean b2 = new Boolean("TrUe");
		Boolean b3 = new Boolean("true123");
		System.out.println(b1);
		System.out.println(b2);
		System.out.println(b3);
	}
}
class Order{
	boolean isOK;
	Boolean IsOK;
}

>>> 10
    123
    12.3
    12.3
    true
    true
    false
    false
    null

包装类 ——> 基本数据类型:调用包装类的 xxxValue()

public class WrapperTest {
	public static void main(String[] args) {
	WT wt = new WT();
	wt.test();
	}
}
class WT{
	public void test(){
		int num = 10;
		Integer in1 = new Integer(num);
		int i1 = in1.intValue(); // 调用转换方法
		System.out.println(i1 + 1);	
	}
}

>>> 11

自动装箱与自动拆箱

自动装箱:基本数据类型 ——> 包装类

自动拆箱:包装类 ——> 基本数据类型

public class WrapperTest {
	public static void main(String[] args) {
	WT wt = new WT();
	wt.test();
	}
}
class WT{
	public void test(){
		// 自动装箱:基本数据类型 ——> 包装类
		int num = 10;
		Integer in1 = num; // 自动装箱
		System.out.println(in1.toString());
		// 自动拆箱:包装类 ——> 基本数据类型
		int num2 = in1; // 自动拆箱
		System.out.println(num2 + 1);
	}
}

>>> 10
    11

基本数据类型、包装类与String的相互转换

基本数据类型、包装类 ——> String 类型,调用 String 重载的 valueOf()

public class WrapperTest {
	public static void main(String[] args) {
	WT wt = new WT();
	wt.test();
	}
}
class WT{
	public void test(){
		int num = 10;
		String str = String.valueOf(num);
		Double d1 = new Double(12.5);
		String str2 = String.valueOf(d1);
		System.out.println(str);
		System.out.println(str2);
	}
}

>>> 10
    12.5

String 类型 ——> 基本数据类型、包装类,调用包装类中的 parseXxx(String s)

public class WrapperTest {
	public static void main(String[] args) {
	WT wt = new WT();
	wt.test();
	}
}
class WT{
	public void test(){
		String str1 = "123";
		// 错误情况
//		int num1 = (int)str1;
//		Integer in1 = (Integer)str1;
		int num2 = Integer.parseInt(str1);
		System.out.println(num2 + 1);
	}
}

>>> 124

包装类常见面试题

public class WrapperTest {
	public static void main(String[] args) {
	WT wt = new WT();
	wt.test();
	}
}
class WT{
	public void test(){
        Object o1 = true ? new Integer(1) : new Double(2.0);
		System.out.println(o1);

		Integer i = new Integer(1);
		Integer j = new Integer(1);
		System.out.println(i == j);
		
		Integer m = 1;
		Integer n = 1;
		System.out.println(m == n);
		
		Integer x = 128;
		Integer y = 128;
		System.out.println(x == y);
	}
}

>>> 1.0
    false
    true
    false

o1 为 1.0 是因为三则运算符会自动提升为两个算式中较大的数据类型,int ——> double

Integer 内部定义了 IntegerCache 结构,IntegerCache 中定义了 Integer [ ],保存了从 -128 ~ 127 范围的整数。如果我们使用自动装箱的方式,给 Integer 赋值的范围在 -128 ~ 127 范围内时,可以直接使用数组中的元素,不用再去 new 了,提高了效率。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

java小白。。

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值