zoukankan      html  css  js  c++  java
  • 【Java】WrapperClass 包装类

    什么是包装类?

    写写我的想法

    就是对于对象和基本类型的无法匹配和强转,基本类型在面向对象的实例类型中,反而成了个特殊的数据类型的存在

    在一些特定的情况,我们希望通过对象的方式去处理数据,但是基本类型的数据没有像Object的方法可以使用

    所以设计者对所有的基本数据类型进行了封装处理,可以说包装类也称为封装类

    父类

    java.lang.Number

    基本类型  对应的封装类

    boolean    java.lang.Boolean
    
    byte         java.lang.Byte
    
    char   java.lang.Character
    
    short   java.lang.Short
    
    int    java.lang.Integer
    
    long      java.lang.Long
    
    float     java.lang.Float
    
    double  java.lang.Double

    有了类的特点,就可以像对象一样操作了

    基本类型  ->  引用类型

        @Test
        public void test1(){
            // 基本数据类型 转换 包装类
            
            int i = 20;
            Integer integer = new Integer(i); // 默认自动装箱 自动封装
            
            // 或者直接入参
            Integer integer1 = new Integer(100); 
            
            // 支持了字符串入参 如果格式不符合,抛出 NumberFormatException    数值格式异
            Integer integer2 = new Integer("23134");
        }

    引用类型  ->  基本类型

        @Test
        public void test1(){
            // 包装类 转换 基本数据类型
    
            Integer integer = new Integer(100);
    
            // intValue(); 转换返回基本类型值
            int value = integer.intValue(); // 已经自动拆箱处理了
        }

    现在已经是自动装箱拆箱了

    @Test
        public void test1(){
            Integer integer = 100;  // 自动装
            int value = integer ;   // 自动拆
        }

    对String的处理

        @Test
        public void test1(){
            int i = 100;
            String str = i + ""; // 直接拼接即可
            String str2 = String.valueOf(10.03); // String.valueOf()方法
            
            int anInt = Integer.parseInt(str);      // 拆箱
            double v = Double.parseDouble(str2);    // 拆箱
        }

    关于包装类在三元运算会提升数据类型

        @Test
        public void test1(){
            Object object = true ? new Integer(10) : new Double(20);
            System.out.println(object); // 10.0
            
            Object o2;
            if (true) o2 = new Integer(1);
            else o2 = new Double(2);
            System.out.println(o2); //1
        }

    比较判断

        @Test
        public void test1(){
           Integer i = new Integer(10);
           Integer j = new Integer(10);
           System.out.println(i == j);  // false
    
           Integer a = 100;
           Integer b = 100;
           System.out.println(a == b);  // true
    
           Integer c = 128;
           Integer d = 128;
           System.out.println(c == d); // false
        }

    为什么128 就False

    - Integer内部定义了一个IntegerCache结构,IntegerCache定义了Integer[]数组,称为预加载池

    保存了-128 到 127的字面值 可以直接使用,如果调用超出池范围的字面值,就需要扩容了,所以大于127的都是new的,地址自然不一样

  • 相关阅读:
    cnblog项目--20190309
    django js引入失效问题
    Python老男孩 day16 函数(六) 匿名函数
    Python老男孩 day16 函数(五) 函数的作用域
    Python老男孩 day15 函数(四) 递归
    Python老男孩 day15 函数(三) 前向引用之'函数即变量'
    Python老男孩 day15 函数(二) 局部变量与全局变量
    Python老男孩 day14 函数(一)
    Python老男孩 day14 字符串格式化
    Python老男孩 day14 集合
  • 原文地址:https://www.cnblogs.com/mindzone/p/12720825.html
Copyright © 2011-2022 走看看