Java面向对象中 包装类(封装类)的详解

博主前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住也分享一下给大家,
👉点击跳转到网站

包装类的使用:

1.java提供了8种基本数据类型的封装类,使得基本数据类型的变量具有了类的特征。
图解如下:
在这里插入图片描述
2.要点:掌握基本数据类型,包装类,String三者之间的转换。

2.1 基本数据类型—>包装类:调用包装类的适配器。装箱
Example:

public class WarpperTest {
    public static void main(String[] args) {
            int i=10;
            Integer in1=new Integer(i);
            Integer integer2 = Integer.valueOf(i);
//        System.out.println(i.toString); //报异常
            System.out.println(in1.toString()); //10

            Boolean b=new Boolean("TRue");
            System.out.println(b); //true
    }
}

2.2 包装类—>转换为基本数据类型,基本数据类型XXX调用XXXValue()方法。 拆箱
Example:

		Integer a=new Integer(10);
        int c = a.intValue();
        System.out.println(c+1);//11

        Boolean d = new Boolean("R");
        boolean e = d.booleanValue();
        System.out.println(e+"u");//falseu

        Float f=new Float("15.2");
        float g=f.floatValue();
        System.out.println(g+1);//16.2

2.3.JDK5.0 新特性:自动装箱,自动拆箱
Example:

 		//自动装箱
        int h=5;
        Integer y=h; //底层使用的是Integer.valueOf(h)
        
        boolean r=true;
        Boolean r1=r;
        
        //自动拆箱
        System.out.println(y.toString());
        
        int x=y; //底层调用的是intValue()方法
        boolean r2=r1;

2.4.基本数据类型,包装类—>String类型

public class DayTest {

    public static void main(String[] args) {
        int a=10;
        boolean b=true;
        //方式1:连接运算符
        System.out.println(a+"");
        System.out.println(b+"");
        //方式2:调用String的valueOf(XXX xxx)
        int c=11;
        String c1=String.valueOf(c);

        boolean d=true;
        String d1=String.valueOf(d);
        System.out.println(d1+a);

        float e=15.6f;
        String e1=String.valueOf(e);
        System.out.println(e1);
    }
}

2.5. String类型–>包装类,基本数据类型

        String f="123";
        int f1=Integer.parseInt(f);
        System.out.println(f1);

        //报异常
       /* String h="123你好";
        int h1=Integer.parseInt(h);
        System.out.println(h1);*/

        String q="true1";
        boolean q1=Boolean.parseBoolean(q);
        System.out.println(q1);  //输出false

包装类Integer和Character的常用方法如下:

public class WrapperMethod {
    public static void main(String[] args) {
        System.out.println(Integer.MAX_VALUE);//最大值
        System.out.println(Integer.MIN_VALUE); //最小值
        System.out.println(Character.isDigit('1'));//判断是不是数字
        System.out.println(Character.isLetter('a'));//判断是不是字母
        System.out.println(Character.isUpperCase('a'));//判断是不是大写
        System.out.println(Character.isLowerCase('a'));//判断是不是小写

        System.out.println(Character.isWhitespace('a')); //判断是不是空格
        System.out.println(Character.toUpperCase('a')); //转成大写
        System.out.println(Character.toLowerCase('A'));//转成小写
    }
}

输出结果如下:

2147483647
-2147483648
true
true
false
true
false
A
a

Integer的创建机制如下,具体注释已经在代码中给出:

public class WrapperExercise {
    public static void main(String[] args) {
        Integer i = new Integer(1);
        Integer j = new Integer(1);
        System.out.println(i == j); //false

        //这里主要看范围-128~127 就是直接返回,否则就要new Integer(i)
        /*
        解读:
        1、如果i在IntegerCache.low(-128)到IntegerCache.high(127),就直接从数组返回
        2、如果不在-128~127,就直接new Integer(i)
        public static Integer valueOf(int i) {
        if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];
        return new Integer(i);
    }
         */
        Integer m = 1; //底层Integer.valueOf(1) ->阅读源码
        Integer n = 1;
        System.out.println(m == n); //true

        Integer x = 128;
        Integer y = 128;
        System.out.println(x == y); //false
    }
}

Integer面试题的详解

public class WrapperExercise02 {
    public static void main(String[] args) {
        Integer i1 = new Integer(127);
        Integer i2 = new Integer(127);
        System.out.println(i1 == i2); //false

        Integer i3 = new Integer(128);
        Integer i4 = new Integer(128);
        System.out.println(i3 == i4); //false

        Integer i5 = 127;
        Integer i6 = 127;
        System.out.println(i5 == i6); //true

        Integer i7 = 128;
        Integer i8 = 128;
        System.out.println(i7 == i8); //false

        Integer i9 = 127;
        Integer i10 = new Integer(127);
        //i9 是从数组中取出的  而i10是new 出来的 所以不是同一个对象
        System.out.println(i9 == i10); //false

        Integer i11 = 127;
        int i12 = 127;
        //只要有基本数据类型,判断的是值是否相同
        System.out.println(i11 == i12);//true

        Integer i13 = 128;
        int i14 = 128;
        System.out.println(i13 == i14);//true
    }
}

以上是包装类的详解,供大家参考学习,有不当之处,可在评论区指正!

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

路宇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值