包裝類(熟悉)
包裝類的概念
通常情況下基本數據類型的變量不是對象,為了滿足萬物皆對象的理念就需要對基本數據類型的變
量進行打包封裝處理變成對象,而負責將這些變量聲明為成員變量進行對象化處理的相關類,叫做包裝
類。
如:
Person p = new Person();
int num = 10;
p是對象,num從語法層面上看不是對象,如果要把num變成對象應該打包到一個類中:
public class myInt{
private int num = 10;
}
java官方提供的八個包裝類:
在很多的場合中要求這些變量打包成對象的時候,我們就可以把它打包成對象間接的完成這些功能。
Integer類的概述
java.lang.Integer類內部包裝了一個int類型的變量作為成員變量,主要用於實現對int類型的包裝並
提供int類型到String類之間的轉換等方法。
下面案例說明:過時的方法也有替代的產品
package com.lagou.task11;
import com.lagou.task10.StaticOuter;
public class IntegerTest {
public static void main(String[] args) {
// 1.打印integer類中常用的常量數值
System.out.println("最大值是:" + Integer.MAX_VALUE);
System.out.println("最小值是:" + Integer.MIN_VALUE);
System.out.println("所表示二進制的位數是: " + Integer.SIZE);
System.out.println("所占字節的個數是:" + Integer.BYTES);
System.out.println("對應int類型的class實例是:" + Integer.TYPE);
System.out.println("-----------------------------------------------");
// 2.使用構造方法來構造Integer類型的對象並打印
Integer it1 = new Integer(123); //已過時的方法會有一條橫線
System.out.println("it1 = "+ it1); // 自動調用tostring方法 調用重寫以后的方法
Integer it2 = new Integer("456"); //該方法已過時,使用valueOf方法取代,相當於從int類型到integer類型的轉換
System.out.println("it2 = " + it2);
// 上述方法已過時,建議使用valueOf()方法取代
Integer it3 = Integer.valueOf(789);
System.out.println("it3 = " + it3);
// parselnt(String s)參數要的時字符串,但是我們要的是int類型,int類型我們可以把他放在valueOf()里面
Integer it4 = Integer.valueOf("456"); //相當於從string類型到integer類型的轉換
System.out.println("it4 = "+ it4);
Integer it5 = new Integer(1);
System.out.println(it5);
//獲取調用對象的整數數據,相當於從integer類型到int類型的轉換
int it6 = it5.intValue();
System.out.println("獲取到的整數數據是:" + it6); // 最后拼接完最后都會得到字符串。
}
}
* @deprecated * It is rarely appropriate to use this constructor. The static factory * {@link #valueOf(int)} is generally a better choice, as it is * likely to yield significantly better space and time performance. */ @Deprecated(since="9") public Integer(int value) { this.value = value; }
在源碼中標注:在java9中標記着已過時,雖然這個方法已過時了但是請使用替代的方法valueOf()
* @deprecated * It is rarely appropriate to use this constructor. * Use {@link #parseInt(String)} to convert a string to a * {@code int} primitive, or use {@link #valueOf(String)} * to convert a string to an {@code Integer} object. */ @Deprecated(since="9") public Integer(String s) throws NumberFormatException { this.value = parseInt(s, 10); }
上面標注寫的很明白parseint可以得到int類型,int類型又可以放到valueOf()里面
總結:
1、已過時的方法通常都有替代方案,通常在源碼注釋中可以看到推薦使用什么方法。
2、java9以后不能使用構造方法得到integer對象,推薦使用valueOf(int類型對象)或者parselnt(字符串對象)或者用valueOf(傳字符串)方法得到integer對象
3、integer類內部實際上就是被int類型包裝,所謂得包裝就是拿int類型的變量作為這個類得成員變量,只要new這個類的變量都會得到這個類的成員變量,這樣就相當於包裝。
4、intValue這個方法沒有static修飾,需要引用點調用該方法,這個方法得到的是一個int類型的數字,所有需要使用int類型保存該數據,當然如果使用字符串與之拼接得到的數據是string類型。
(1)常用的常量
class實例暫時理解為類型的名稱
(2)常用的方法
(3)裝箱和拆箱的概念
在Java5發布之前使用包裝類對象進行運算時,需要較為繁瑣的“拆箱”和“裝箱”操作;即運算前先將
包裝類對象拆分為基本類型數據,運算后再將結果封裝成包裝類對象。
從Java5開始增加了自動拆箱和自動裝箱的功能。
裝箱:在上述編寫的方法中可以實現從int類型到integer類型的轉換,integer類實際上是int類的包裝類,我們把這種從int類型到integer類型的轉換過程,我們把他叫做裝箱。什么叫做裝箱?裝箱的概念就是把箱子。我要想把int類型變成integer類型實際上就需要把箱子打開,integer打開把int類變成integer類的成員變量,這樣相當於打包好了,實際上這個過程就叫做裝箱。
拆箱:所有把integer打開,把里面的數據拿出來這個過程叫做拆箱。
在java5以前我們使用valueOf()、intValue()實現裝箱/拆箱,但是我們從java5開始我們增加了自動裝箱和自動拆箱的機制。
// 2.java5以前裝箱和拆箱 Integer it7 = Integer.valueOf(1); System.out.println("裝箱:" + it7); int it8 = it7.intValue(); System.out.println("拆箱:" + it8); // 3.從java5開始增加了自動裝箱和自動拆箱的機制 Integer it9 = 1001; System.out.println("自動裝箱:"+it9); int it10 = it9; System.out.println("自動拆箱:"+it10);
(4)自動裝箱池
在Integer類的內部提供了自動裝箱池技術,將-128到127之間的整數已經裝箱完畢,當程序中使用
該范圍之間的整數時,無需裝箱直接取用自動裝箱池中的對象即可,從而提高效率。
以下考點為什么127比較是true,128比較是false?在Integer類的內部提供了自動裝箱池技術,將-128到127之間的整數已經裝箱完畢。
// 筆試考點 Integer it11 = 127; Integer it12 = 127; Integer it13 = new Integer(127); Integer it14 = new Integer(127); System.out.println(it11== it12); //比較地址 true System.out.println(it11.equals(it12)); //比較內容 true System.out.println(it13 == it14); //比較地址 false System.out.println(it13.equals(it14)); //比較內容 true
自動裝箱池:就是java把該裝的數據裝在一個水池中,所以調用這個自動裝箱池的數據地址是一樣的,我們使用裝箱池中的數據在范圍內比較自然就是true。
當然我們可以適當的調整自動裝箱池,這一塊概念就涉及到我們的jvm的調優,在后續的課程中有講解。
查找cache,可以在源碼中找到
integer類除了可以把字符串轉換為int類型,還能實現進制轉換:
// 4.實現靜態方法的調用 int it15 = Integer.parseInt("200"); // int it16 = Integer.parseInt("200a"); 編譯ok,運行發生NumberFormatException數字格式異常,因為有字母 System.out.println("字符串轉換為整數的結果是:"+it5); System.out.println("根據參數指定的整數獲取對應的十進制字符串是:"+Integer.toString(200)); System.out.println("根據參數指定的整數獲取對應的二進制字符串是:"+Integer.toBinaryString(200)); System.out.println("根據參數指定的整數獲取對應的十六進制字符串是:"+Integer.toHexString(200)); System.out.println("根據參數指定的整數獲取對應的八進制字符串是:"+Integer.toOctalString(200));