基本数据类型和包装类之间的转换
public class HelloWorld {
public static void main(String[] args) {
// 定义int类型变量,值为100
int score = 100;
//创建Integer包装类对象,表示变量score1的值
Integer score1=score;
// 将Integer包装类转换为double类型
double score2=score1.doubleValue();
// 将Integer包装类转换为Long类型
long score3=score1.longValue();
// 将Integer包装类转换为int类型
int score4=score1;
//打印输出
System.out.println("score对应的Integer类型结果为:"+score1);
System.out.println("score对应的double类型结果为:"+score2);
System.out.println("score对应的long类型结果为:"+score3);
System.out.println("重新由Integer转换成int类型的结果为:"+score4);
}
}
基本数据类型和字符之间的转换
package xiti;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
//定义float类型变量,赋值为88.99
float f1 =88.99f;
//将基本类型转换为字符串
String a=Float.toString(f1);
//打印输出
System.out.println("f1转换为String型后与整数20的求和结果为:"+a+20);
//定义String类型变量,赋值为"188.55"
String str ="188.55";
// 将字符串转换为基本类型double
Double b=Double.valueOf(str);
//打印输出
System.out.println("Str转换为double型后与整数20的求和结果为:"+(b+20));
}
}
基本数据类型和字符之间的转换
public class StringUse {
public static void main(String[] args) {
double a = 12.5;
//将基本类型转换为字符串
String str1 =Double.toString(a);
System.out.println("a 转换为String型后+10的结果为: "+str1+10);
String str = "2.8";
// 将字符串转换为基本类型
double b =Double.valueOf(str);
System.out.println("str 转换为double型后+10的结果为: "+(b+10));
}
}