1-21基本类型和包装类

包装类

什么是包装类?

Java里面8个基本数据类型都有相应的类,这些类叫做包装类。

包装类有什么优点

可以在对象中定义更多的功能方法操作该数据,方便开发者操作数据,例如基本数据类型和字符串之间的转换。
基本数据类型和对应的包装类

包装类都在java.lang包里面

基本数据类型 包装类
byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean
下面以Integer为例来学习一下包装类
获取int类型的最小值:

Integer.MIN_VALUE
获取int类型的最大值:

Integer.MAX_VALUE
创建Integer对象:
1.传入int类型,会将int类型的10转换为Integer类型:

Integer i1 = new Integer(10);
2.传入String类型,会将String类型的”1024″转换为Integer类型,注意:如果传入的字符串不是数字,将会报出NumberFormatException

Integer i2 = new Integer("1024");
将Integer类型转换为int类型:

Integer i1 = new Integer(10);
int i2 = i1.intValue();
将String类型转换为int类型,传入的字符串必须是数字字符串:

int count = Integer.parseInt("10");
将int转换为Integer类型:

Integer i3 = Integer.valueOf(10);
将String类型转换为Integer类型:

Integer i4 = Integer.valueOf("10");
将int类型的十进制转换成2进制:

String s1 = Integer.toBinaryString(10);
将int类型的十进制转换成16进制:

String s2 = Integer.toHexString(10);
将int类型的十进制转换成8进制:

String s3 = Integer.toOctalString(10);
int,Integer,String三种类型之间的转换
int–>Integer

Integer i1 = Integer.valueOf(10);
Integer–>int

int i2 = i1.intValue();
String–>Integer

Integer i3 = Integer.valueOf("10");

Integer–>String

String s1 = i3.toString();

String–>int

int i4 = Integer.parseInt("123");

int–>String

String s2 = 10 + "";

猜你喜欢

转载自www.cnblogs.com/superfly123/p/10446948.html
今日推荐