自动装箱和自动拆箱(包装类)

package com.njwb18081.day11;

public class Test01 {
    public static void main(String[] args) {
        /*
         *  基本数据类型              包装类
            byte                    Byte
            short                   Short
            int                     Integer
            long                    Long
            float                   Float
            double                  Double
            char                    Character
            boolean                 Boolean
         */
//      手动装箱
        int i=1;
        System.out.println(i);
        Integer it=new Integer(i);
        System.out.println(it);
        Integer it2=new Integer("100");
        System.out.println(it2);
        Integer it3=null;
        System.out.println(it3);
        it3=Integer.valueOf(i);



        Character c=new Character('a');
        System.out.println(c);

        Boolean b=new Boolean(true);
        Boolean b2=new Boolean("true");//填写的值除了"true",其他返回的都是false
        System.out.println(b);
        System.out.println(b2);
//      手动拆箱
        Integer it4=new Integer("10");
        int i2=it4.intValue();
        System.out.println(i2);
        Float fl=new Float(10F);
        System.out.println(fl);
        float f=fl.floatValue();
        System.out.println(f);



//      自动装箱
        Integer it5=10;
        System.out.println(it5);
        Boolean b3=true;

//      自动拆箱
        int i3=it5;
        System.out.println(i3);
        boolean b4=b3;
        System.out.println(b4);

//      基本数据类型-->String
        int i4=10;
        String s1=i4+"";
        s1=100+"";
        System.out.println(s1);

        boolean b5=false;
        String s2=b5+"";
        System.out.println(s2);


//      基本数据类型-->包装类-->String
        int i5=100;
        Integer it6=i5;
        System.out.println(it6.toString());
        System.out.println(Integer.toString(9));

//      String-->Integer
        String s3="1000";
        Integer it7=new Integer(s3);
        System.out.println(it7);

//      String-->int
        String s4="100";
        int i6=Integer.parseInt(s4);
        System.out.println(i6);

    }
}
package com.njwb18081.day11;

public class Student {
    private String name;
    private Integer age; //包装类作为学生年龄的类型
    private Boolean sex;//包装类作为学生年龄的类型
    private Double salary;//包装类作为学生年龄的类型
        //优点是当实例化对象时,对象默认值均为null 而不是0或者false
    public Student() {
        super();
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    public Boolean getSex() {
        return sex;
    }
    public void setSex(Boolean sex) {
        this.sex = sex;
    }
    public Double getSalary() {
        return salary;
    }
    public void setSalary(Double salary) {
        this.salary = salary;
    }
}


package com.njwb18081.day11;

public class TestStudent {
    public static void main(String[] args) {
        Student s=new Student();
        System.out.println(s.getName());
        System.out.println(s.getAge());
        System.out.println(s.getSalary());
        System.out.println(s.getSex());
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值