自動裝箱 和 自動拆箱

2021-08-22 10:05:24 字數 689 閱讀 8753

自動裝箱 auto-boxing

基本型別就自動地封裝到與它相同型別的包裝中,如:

integer i= 100;

本質上是,編譯器編譯時為我們新增了:

integer i = new integer(100);

自動拆箱 unboxing:

包裝類物件自動轉換成基本型別資料,如:

int a = new integer(100);

本質上,編譯器編譯時為我們新增了:

int a = new integer(100).intvalue();

快取問題

intvalue就是把integer型別轉化為int型別

/**

* 測試自動裝箱和自動拆箱

* (加了**而已)

* @author administrator

* */

public class test02

}

結果顯示:

2000

false

true

****************************

true

true

****************************

false

true

裝箱和拆箱,自動裝箱和自動拆箱

以integer的建立為例。裝箱 把基本資料型別轉換成包裝類物件 int integer integer num1 new integer 17 拆箱 把乙個包裝類的物件,轉換成基本型別的變數 integer int int num2 num1.intvalue 自動裝箱 integer num3 ...

自動裝箱和自動拆箱

public class test206 system.out.println tostring 1234,靜態方法 parseint string s 把數字字串變成int型別的數字 tobinarystring int i 把int數字變為二進位制數的字串形式 tooctalstring int...

自動裝箱和拆箱

概念 裝箱就是自動將基本型別資料轉為包裝型別 拆箱就是自動將包裝型別轉為基本型別。具體實現 自動裝箱 integer total1 99 編譯後 integer total integer.valueof 99 自動拆箱 int total2 total1 編譯後 int total2 total1...