包装类
八种基本类型对应的对象类型,可以直接把基本类型的变量表示为一个对象,执行变量类型相互转换时可以使用这些包装类
八种包装类
基本类型 | 包装类 |
---|---|
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
boolean | Boolean |
char | Character |
基本类型转换成包装类
1.//使用包装类构造方法创建对象
byte b =12; short s =13;
Byte ob = new Byte(b);
Short os =new Sghor(s);
以此类推
2.//使用valueOf方法将转换成对象类型
byte b =12; short =13;
Byte ob =Byte.valueOf(b);
Short os =Short.valueOf(s);
以此类推
对象类型转换成基本类型
Integer i =new Inyeger(10);
Long l = new Long(10L);
1.//将包装类对象类型转换成基本数据类型
int a = i.intValue();
long b = l.intValue();
以此类推
//字符串转换为包装类也是如此
字符串转换成基本类型
使用pars类型方法转换
例;
String str1="10"; String str2="12.5";
//转换↓
int i = Integer.parsInt(str1);
double d = Double.parsDouble(str2);
//小贴士 JDK5.0之后,提供自动装箱/拆箱的功能,即包装类与基本类型自动转换
自动装箱:将一个基本类型数据变为包装类型的过程称为自动封箱
Inter i =new Integer("12");
Long l =new Long("100");
int a =i;
long b =l;
加油!