首页 > 编程语言 > 详细

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

时间:2020-01-05 20:45:16      阅读:82      评论:0      收藏:0      [点我收藏+]
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)) ;
    }
};

 

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

原文:https://www.cnblogs.com/tszr/p/12153266.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!