天天看點

Java包裝類(Integer 詳解 )

Java包裝類

基本概述

  • 在某些場合要求所有的資料内容都必須是類類型的對象,而Java語言中有8種基本資料類型聲明的變量都不是對象,為了使得這些資料在該場合種能夠使用,則需要好進行對象化處理,此時就需要借助包裝類将變量包裝成對象再進行使用
  • 包裝類是為了值類型資料和對象間能互相轉換、提供裝箱和拆箱機制的類
基本資料類型 包裝類型
byte Byte
Boolean Boolean
short Short
char Character
int Integer
long Long
float Float

自動拆箱與裝箱

  • 自動拆箱: 自動進行包裝類向基本資料類型的轉換
  • 自動裝箱: 自動進行基本資料類型向包裝類的轉換

Integer類型

  • java.lang.Integer 類是Object類的間接子類,用于包裝int類型的資料
  • 該類由final關鍵字修飾,表示該類不能被繼承
  • 常用方法
  • 該類重寫了Object類的equals()、hashCode()以及toString()方法
  • Integer(int value) - 根據參數指定的整數值來構造對象
  • Integer(String s) - 根據參數指定的字元串來構造對象
  • int intValue() - 用于擷取調用對象種含有的int類型資料并傳回
  • 用于實作将Integer類型拆包為int類型(自動拆箱)
  • static Integer valueOf(int i) - 根據參數指定的整數來得到對象
  • 用于實作将int類型包裝成Integer類型(自動裝箱)
  • static int parseInt(String s) - 用于将字元轉類型的資料轉換為int類型的整數
  • 案例
/*
 * 若塵
 */
package packing;

/**
 * 示範Integer類的使用
 * @author ruochen
 * @version 1.0
 */
public class TestInteger {
  public static void main(String[] args) {
    // 使用Integer類中的構造方法來構造對象,該類沒有無參構造方法
    
    Integer it = new Integer(123);
    // 自動調用toString()方法, 得到字元串類型的十進制整數
    System.out.println(it);
    
    Integer it2 = new Integer("234");
    System.out.println(it2);
    System.out.println("----------------------");
    
    // 實作int類型和Integer類型之間的互相轉換
    Integer it3 = Integer.valueOf(222);
    System.out.println(it3);  // String 類型
    
    int res = it3.intValue();
    System.out.println(res);  // int 類型

    System.out.println("----------------------");
    // 實作String類型向int類型的轉換
    int res2 = Integer.parseInt("33333");
    System.out.println(res2);
    
    // java.lang.NumberFormatException
    // 要求字元串中每個字元都是十進制整數的字元,否則産生數字格式異常
    // int res3 = Integer.parseInt("1234a");
    // System.out.println(res3);
    
    System.out.println("----------------------");
    // 自動裝箱和自動拆箱的機制
    Integer it4 = 100;  // int -> Integer 發生自動裝箱,自動調用ValueOf()
    res = it4;  // Integer -> int  發生自動拆箱,自動調用intValue()
    
    
    System.out.println("----------------------");
    Integer it5 = 128;
    Integer it6 = 128;
    Integer it7 = new Integer(128);
    Integer it8 = new Integer(128);
    
    System.out.println(it5.equals(it6));  // true 比較内容
    System.out.println(it5 == it6);  // false 比較位址
    System.out.println(it7.equals(it8));  // true 比較内容
    System.out.println(it7 == it8);  // false 比較位址
    
    
    System.out.println("----------------------");
    // 源碼993行
    // 耗時
    // -128~127 提前裝箱完畢
    // 此處127已經提前裝箱,不需要重新建立對象,兩個指向同一個對象
    // 上面128不在範圍内,需要建立對象
    Integer it9 = 127;
    Integer it10 = 127;
    // 下面是自己手動new的兩個對象,位址不同
    Integer it11 = new Integer(128);
    Integer it12 = new Integer(128);
    
    System.out.println(it9.equals(it10));  // true 比較内容
    System.out.println(it9 == it10);  // false 比較位址
    System.out.println(it11.equals(it12));  // true 比較内容
    System.out.println(it11 == it12);  // false 比較位址
  }
}      
123
234
----------------------
222
222
----------------------
33333
----------------------
----------------------
true
false
true
false
----------------------
true
true
true
false      

自動裝箱池(-128~127)

  • 為了提高性能在Integer類的内部提供了自動裝箱池,也就是把-128 ~ 127 之間的整數提前裝箱完畢,若程式中需要該範圍内的資料則直接從裝箱池中擷取,無需建立新對象