zoukankan      html  css  js  c++  java
  • Integer类的装箱和拆箱到底是怎样实现的?

    先解释一下装箱和拆箱:

    装箱就是  自动将基本数据类型转换为包装器类型;拆箱就是  自动将包装器类型转换为基本数据类型。

      下表是基本数据类型对应的包装器类型:

    int(4字节) Integer
    byte(1字节) Byte
    short(2字节) Short
    long(8字节) Long
    float(4字节) Float
    double(8字节) Double
    char(2字节) Character
    boolean(未定) Boolean

    下面是代码:

    public class BoxAndUnbox {

    /**
    * @param args
    */
          public static void main(String[] args)

        {
          int value=100;

          Integer obj=value; //装箱

          int result=obj*2; //拆箱
          System.out.println(result);

          System.out.println(obj); 

      }

    }


          Sy


       Integer类的装箱和拆箱到底是怎样实现的?

     反编译class文件之后得到如下内容:

      

    由此可见 在装箱的时候自动调用的是Integer的valueOf(int)方法。而在拆箱的时候自动调用的是Integer的intValue方法。

     因此可以用一句话总结装箱和拆箱的实现过程:

      装箱过程是通过调用包装器的valueOf方法实现的,而拆箱过程是通过调用包装器的 intValue方法实现的.

  • 相关阅读:
    UVa 10055
    UVa 401
    c++中文件应用的一点小变化
    poj2136
    UVa 494
    一台电脑接两个显示器,双屏显示介绍zz
    学习jquery合集
    解决Windows下MinGW显示乱码zz
    QWS_MOUSE_PROTO该如何写
    Qt/e中鼠标设备分析
  • 原文地址:https://www.cnblogs.com/aishangtaxuefeihong/p/4886997.html
Copyright © 2011-2022 走看看