一句话概括自动装箱/拆箱
生活随笔
收集整理的這篇文章主要介紹了
一句话概括自动装箱/拆箱
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
自動裝箱過程是通過調用包裝類的valueOf()方法實現的,二自動拆箱過程是通過調用包裝類的xxxValue()方法實現的(xxx代表對應的基本數據類型,如intValue,doubleValue等)。
package demo06;public class TestWrapper2 {public static void main(String[] args) {//1.自動裝箱和自動拆箱Integer in = 5;Integer in2 = new Integer(5);//valueOf()int i = in2;int i2 = in2.intValue();//2.== equalsInteger in3 = new Integer(56);Integer in4 = new Integer(56);System.out.println(in3==in4);//falseSystem.out.println(in3.equals(in4));//trueInteger in5 = 25;Integer in6 = 25;System.out.println(in5==in6);//trueSystem.out.println(in5.equals(in6));//trueInteger in7 = 256;Integer in8 = 256;System.out.println(in7==in8);//falseSystem.out.println(in7.equals(in8));//true} }Integer類提供了一個靜態內部類IntegerCache,對于定義一個靜態數組cache,長度為256,賦值為-128到127。對于自動裝箱時如果是-128到127范圍內的數據,
直接獲取數組的指定值;對于中國范圍之外的數據,通過new Integer()重新創建對象,這么做的目的是提高效率。
?
總結
以上是生活随笔為你收集整理的一句话概括自动装箱/拆箱的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 192.168.8.1手机登陆_高端机型
- 下一篇: mulitpartfile怎么接收不到值