java常用类(包装类)

1 什么是包装类

一种可以将基本数据类型数据当成对象使用的技术

2 包装类的作用

存放大量的方法,方便对基本数据的处理

3 包装类

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

4 包装类的定义以及类型的转换

包装类的定义

例如:

Integer it1 = new Integer(int i);

Integer it2 = new Integer(String s);

 类型的转换

例如:

int -> Integer

Integer it1 = new Integer(int i);

Integer it2 = i.intValue();

Integer it3 = Integer.valueOf(int i); 

 5 装箱和拆箱

装箱:基本数据类型转换成包装类;

拆箱:包装类转换成基本数据类型。 

//手动装箱

//基本数据类型 -> 包装类

int a = 10;

Integer i = Integer.valueOf(a);

System.out.println(i);

//手动拆箱

//包装类 -> 基本数据类型

Integer it1 = new Integer(5);

int b = i.intValue();

自动装箱拆箱

 JDK自从1.5版本以后,就引入了自动拆装箱的语法,也就是在进行基本数据类型和对应的包装类转换时,系统将自动进行,这将大大方便程序员的代码书写

例如:

//包装类 -> 基本数据类型

Integer it1 = new Integer(5);

int a = it1;

//基本数据类型 -> 包装类

Integer it1 = 5;

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值