吴裕雄--天生自然JAVA面向对象高级编程学习笔记:包装类

时间:2023-03-08 16:00:52
吴裕雄--天生自然JAVA面向对象高级编程学习笔记:包装类
public class WrapperDemo01{
public static void main(String args[]){
int x = 30 ; // 基本数据类型
Integer i = new Integer(x) ; // 装箱:将基本数据类型变为包装类
int temp = i.intValue() ;// 拆箱:将一个包装类变为基本数据类型
}
};
public class WrapperDemo02{
public static void main(String args[]){
float f = 30.3f ; // 基本数据类型
Float x = new Float(f) ; // 装箱:将基本数据类型变为包装类
float y = x.floatValue() ;// 拆箱:将一个包装类变为基本数据类型
}
};
public class WrapperDemo03{
public static void main(String args[]){
Integer i = 30 ; // 自动装箱成Integer
Float f = 30.3f ; // 自动装箱成Float
int x = i ; // 自动拆箱为int
float y = f ; // 自动拆箱为float
}
};
public class WrapperDemo04{
public static void main(String args[]){
String str1 = "30" ; // 由数字组成的字符串
String str2 = "30.3" ; // 由数字组成的字符串
int x = Integer.parseInt(str1) ; // 将字符串变为int型
float f = Float.parseFloat(str2) ; // 将字符串变为int型
System.out.println("整数乘方:" + x + " * " + x + " = " + (x * x)) ;
System.out.println("小数乘方:" + f + " * " + f + " = " + (f * f)) ;
}
};
public class WrapperDemo05{
public static void main(String args[]){
int x = Integer.parseInt(args[0]) ; // 将字符串变为int型
float f = Float.parseFloat(args[1]) ; // 将字符串变为int型
System.out.println("整数乘方:" + x + " * " + x + " = " + (x * x)) ;
System.out.println("小数乘方:" + f + " * " + f + " = " + (f * f)) ;
}
};