首頁  >  文章  >  Java  >  Java 中的包裝類

Java 中的包裝類

王林
王林原創
2024-08-30 16:00:11604瀏覽

Wrapper類別是java.lang函式庫中的一個重要類別。包裝類別物件為原始資料類型建立包裝器。建立包裝類別的物件時,會在儲存原始資料類型的記憶體中建立空間。包裝類別提供了一些用於將物件轉換為原始資料以及將原始資料轉換為物件的功能,即裝箱/拆箱。從物件到原始資料以及原始資料到物件的轉換是自動發生的。原始資料型態是指int、float、char、double、byte等

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

文法:

下面給出的聲明顯示了 Wrapper 類別如何在 java 程式中運作。

範例:

int i = 100;

在下面給出的範例中,我們可以看到 i 是整數資料型別。有時在java中整數需要作為物件類型傳遞。在這種情況下,我們可以使用包裝類別將整數轉換為物件。

代碼:

Integer intVal = new Integer(i);

在上面給出的語法中,我們可以看到如何使用 Integer 類別物件將原始資料類型轉換為物件。另外,我們可以說原始資料類型被包裝為物件。

Java 中包裝類別的使用

下面給了一些包裝類別的用法:

  • 包裝類別可用於將物件轉換為原始資料類型,反之亦然。
  • 包裝類別的使用提高了程式的效能。
  • 包裝類別有助於物件的序列化,反之亦然。它可以將原始資料轉換為物件。有時我們需要串流物件;在這種情況下,包裝類別可以將它們轉換為序列化格式。
  • 包裝類別提供了一些重要的方法,用於在集合中執行搜尋和排序。
  • 減法&加法,這些類型的操作不能修改Wrapper類別原始整數的舊值;這就是為什麼 Wrapper 類別被稱為不可變的。
  • 包裝類別用於多執行緒進程,因為多執行緒進程需要一個物件來同步進程。包裝類別將不同的資料類型轉換為物件。

在JavaAPI的基礎上,Wrapper類別層次結構將Object保持在不同原始類別的頂部。數字、字元和布林值位於物件之後的第二層。 Byte、Short、Int、Long、Float、Double 屬於第三級 Number 資料型別。

包裝類別使用以下兩種機制自動裝箱和拆箱,用於資料類型的轉換/包裝或將物件轉換為原始資料類型。

  • 自動裝箱:自動裝箱是指使用包裝類別將原始資料類型自動轉換為物件。它被稱為自動裝箱。在下面給出的範例中,int 轉換為 Integer 對象,在範例 c 中,不同的原始資料類型轉換為對應的對象。
  • 拆箱:拆箱是自動裝箱的逆過程。自動將包裝類別物件轉換為對應的原始資料類型稱為拆箱。在下面給出的範例 b 中,Integer 物件被轉換為 int 原始資料類型。

Java 中的包裝類別範例

以下是 Java 中包裝類別的不同範例:

範例#1

在下面給出的範例中,我們可以看到如何透過包裝類別從 int i 手動轉換為物件 k。

代碼:

import java.util.*;
class WrapperExample {
public static void main(String args[]){
int j=100;
//converting int j to integer k as an object
Integer k = new Integer(j);
System.out.println(j + "\n" + k);
}
}

輸出:

Java 中的包裝類

在上面給出的範例中,我們可以看到轉換是如何明確進行的。

範例#2

在下面給出的範例中,我們可以看到此轉換過程有時會自動發生,即稱為自動裝箱。

代碼:

import java.util.*;
class AutoboxingUnboxingExample {
public static void main(String args[]){
int j = 500;
ArrayList<Integer> arrValues = new ArrayList();
arrValues.add(j);  // autoboxing takes place implicitly
System.out.println(arrValues.get(0));
}
}

輸出:

Java 中的包裝類

在上面給出的範例中,int 值會作為物件隱式轉換為物件。另外,這個值可以從ArrayList中取得。

範例#3

在這個例子中,我們將完成拆箱的實作。拆箱是自動裝箱的逆過程。

代碼:

import java.util.*;
class AutoboxingUnboxingExample {
public static void main(String args[]){
ArrayList<Integer> arrValues = new ArrayList();
arrValues.add(250);
//unboxing here as int data type from Integer object
int k = arrValues.get(0);
//value printed is in primitive data type
System.out.println(k);
}
}

輸出:

Java 中的包裝類

在上面給出的範例中,ArrayList 物件欄位被轉換為 k 基本資料類型,即 int k。

Example #4

The following given example have all the details of Autoboxing & Unboxing.

Code:

import java.util.*;
class WrapperConversionExample {
public static void main(String args[]){
int i = 15;
float j = 9.6f;
double k = 120.8;
byte l = 1;
//creating instance of Integer object
Integer iObj = new Integer(i);
//creating instance of Float object
Float fObj = new Float(j);
//creating instance of Double object
Double dObj = new Double(k);
//creating instance of Double object
Byte bObj = new Byte(l);
//value printed is in object
System.out.println("Value as an Integer object > " + iObj);
System.out.println("Value as a Float object > " + fObj);
System.out.println("Value as a Double object > " + dObj);
System.out.println("Value as a Byte object > " + bObj);
//primitive data type from the object
int m = iObj;
float n = fObj;
double o = dObj;
byte p = bObj;
//value printed is in primitive data type
System.out.println("Value as an int primitive type > " + m);
System.out.println("Value as a float primitive type > " + n);
System.out.println("Value as a double primitive type > "+ o);
System.out.println("Value as a byte primitive type > " + p);
}
}

Output:

Java 中的包裝類

In the above-given program, we can see the implementation of Wrapper classes. Wrapper classes are converting the primitive data type to object & object to the primitive data type. The wrapper class provides separate classes for each primitive data type.

Conclusion

Through the Wrapper classes, we can easily understand autoboxing & unboxing how conversion takes place from primitive to object & its vice versa, which can be easily understood through Wrapper classes. For each of the primitive data types, there is a dedicated class in java.

以上是Java 中的包裝類的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn