JAVA SE 包装类,javase包装类
分享于 点击 7972 次 点评:234
JAVA SE 包装类,javase包装类
简介
包装类:让基本数据类型拥有对象的特性。
基本数据类型与其包装类
转换方法
Integer类_构造方法
- Integer (int value) //创建一个Integer对象,表示指定的int值
- Integer (String s) //创建一个Integer对象,表示String参数所指示的int值
Integer类_常用方法
int score1 = 20;
// 创建Integer包装类对象,表示变量score1的值
Integer score2 = new Integer(score1);
// 将Integer包装类转换为double类型
double score3 = score2.doubleValue();
// 将Integer包装类转换为float类型
float score4=score2.floatValue();
// 将Integer包装类转换为int类型
int score5 =score2.intValue();
自动装箱和拆箱(JDK1.5)
装箱:基本类型转换为包装类
拆箱:包装类转换为基本类型
//装箱
int i = 10;
Integer x = new Integer(i);//手动装箱
Integer y = i;//自动装箱
//拆箱
Double d = new Double(10.0);
double m = d.doubleValue();//手动拆箱
double n = d;//自动拆箱
基本类型和字符串的转换
基本类型转字符串
- 使用包装类的toString()方法
- 使用String类的valueOf()方法
- 空字符串+基本类型
int n = 10;
String str1 = Integer.toString(n);
String str2 = String.valueOf(n);
String str3 = n + "";
字符串转基本类型
- 使用包装类的parseXxx方法
- 使用包装类的valueOf()方法,转换为基本类型的包装类,自动拆箱
String str = "10";
int n = Integer.parseInt(str);
int m = Integer.valueOf(str);
注意事项
相关文章
- 暂无相关文章
用户点评