• java wrapper


    java - wrapper
    Wrapper包装类

    基本数据类型由于不是类,不能够使用java类库提供的大量方法,所以在设计上,我们为每一个基本数据类型都对应一个类,同时数据存储的范围不变,此时基本数据类型就具备了类的特点。

    基本数据类型 包装类
    byte Byte
    char Character
    short Short
    int Integer
    long Long
    double Double
    float Float
    boolean Boolean

    继承关系

    Character

    image-20220517114112625

    Boolean

    image-20220517114029832

    Byte,Short,Integer,Long,Double,Float

    image-20220517113855028

    装箱

    基本数据类型包装成包装类的实例

    Integer i = 1;
    

    实现方式

    /**
         * The value of the {@code Integer}.
         *
         * @serial
         */
    private final int value;
    
    /**
         * Constructs a newly allocated {@code Integer} object that
         * represents the specified {@code int} value.
         *
         * @param   value   the value to be represented by the
         *                  {@code Integer} object.
         */
    public Integer(int value) {
      this.value = value;
    }
    
    /**
         * Returns an {@code Integer} instance representing the specified
         * {@code int} value.  If a new {@code Integer} instance is not
         * required, this method should generally be used in preference to
         * the constructor {@link #Integer(int)}, as this method is likely
         * to yield significantly better space and time performance by
         * caching frequently requested values.
         *
         * This method will always cache values in the range -128 to 127,
         * inclusive, and may cache other values outside of this range.
         *
         * @param  i an {@code int} value.
         * @return an {@code Integer} instance representing {@code i}.
         * @since  1.5
         */
    public static Integer valueOf(int i) {
      if (i >= IntegerCache.low && i <= IntegerCache.high)
        return IntegerCache.cache[i + (-IntegerCache.low)];
      return new Integer(i);
    }
    

    解析过程:先执行valueOf方法,当条件满足的时候,会直接返回InterCache中的值,否则,会new一个Integer对象

    拆箱

    获取包装类对象中包装的基本数据类型变量

    调用包装类的.xxxValue()方法

    int i = Integer.intValue();
    
    字符串和基本数据类型的相互转换
    • 基本数据类型转换成字符串

      1. 通过空字符+基本数据类型

        String str = "" + 10;
        
      2. 使用String类的静态方法valueOf

        String str = String.valueOf(10);
        
      3. 先将基本类型装修对象,然后调用对象的toString 方法

        Integer i = new Integer(10);
        String str = i.toString();
        
    • 字符串转换成基本数据类型

      1. 调用各自的包装类的静态方法

        String str = "100";
        int i = Integer.parseInt(str);
        
      2. 直接new包装类

        int i = new Integer(str);
        
    包装类和字符串相互转换
    • 包装类转换成字符串

      1. 通过空字符+基本数据类型

        String str = "" + i;
        
      2. 使用String类的静态方法valueOf

        String str = String.valueOf(i);
        
      3. 调用对象的toString 方法

        String str = i.toString();
        
    • 字符串转换成包装类

      1. 通过new 包装类

        Integer i = new Integer(str);
        
      2. 通过包装类的valueOf 方法

        Integer i = Integer.valueOf(str);
        
    整体图解

    image-20220517182241233

  • 相关阅读:
    VUE.JS 使用axios数据请求时数据绑定时 报错 TypeError: Cannot set property 'xxxx' of undefined 的解决办法
    vue-cli:渲染过程理解2(vue init webpack方式创建)
    vue2 mint-ui loadmore(下拉刷新)
    vue-resource: jsonp请求百度搜索的接口
    vue中组件绑定事件时是否加.native
    vue 子组件修改父组件传来的props值,报错
    测试开发工程师的角色
    不要信仰BAT!
    简历不能怎么写?
    测试工程师面试,全国各地有哪些知名互联网公司可以去?
  • 原文地址:https://www.cnblogs.com/ywjcqq/p/16281933.html
Copyright © 2020-2023  润新知