如何在 Java 中將包裝器物件轉換為基本型別?


在 Java 中,基本型別(例如 int、double、float 等)是內置於語言中的基本資料型別,不是物件。基本型別具有固定的大小,可以直接用於計算和操作。

包裝器物件只不過是基本型別的物件表示形式。例如,Integer 類是 int 基本型別的包裝器物件。包裝器物件是類的例項,並具有可以呼叫的方法,而基本型別則沒有。

但是,使用包裝器物件比使用基本型別需要更多的記憶體,因為每個包裝器物件都必須建立並存儲在記憶體中。總之,雖然基本型別更簡單、更高效,但包裝器物件為基本資料提供了面向物件的介面,並提供了更多功能。

根據問題陳述,我們必須將給定的包裝器物件轉換為相應的基本型別。

讓我們開始吧!

向您展示一些例項

例項 1

  • Integer integer = new Integer(10);

  • int primitiveInt = 10;

例項 2

  • Double doubleWrapper = new Double(10.5);

  • double primitiveDouble = 10.5;

例項 3

  • Long longWrapper = new Long(10L);

  • long primitiveLong = 10L;

演算法

演算法 1:(透過使用 intValue() 方法)

  • 步驟 1 - 建立一個 Integer 包裝器物件併為其賦值 10。

  • 步驟 2 - 使用 intValue() 方法將 Integer 物件轉換為基本 int。

  • 步驟 3 - 列印其值。

演算法 2:(透過使用 valueOf() 方法)

  • 步驟 1 - 建立一個 Double 包裝器物件併為其賦值 10.5。

  • 步驟 2 - 使用 valueOf() 方法將 Double 物件轉換為基本 double。

  • 步驟 3 - 列印其值。

演算法 3:(透過使用拆箱運算子 (int))

  • 步驟 1 - 建立一個 Long 包裝器物件併為其賦值 10L。

  • 步驟 2 - 使用拆箱將 Long 物件轉換為基本 long。

  • 步驟 3 - 列印其值。

演算法 4:(透過使用型別轉換)

  • 步驟 1 - 建立一個 Float 包裝器物件併為其賦值 10.5f。

  • 步驟 2 - 使用型別轉換將 Float 物件轉換為基本 float。

  • 步驟 3 - 列印其值。

多種方法

我們以不同的方法提供瞭解決方案。

  • 使用 intValue() 方法

  • 使用 valueOf() 方法

  • 使用拆箱運算子 (int)

  • 使用型別轉換

讓我們逐一檢視程式及其輸出。

方法 1:透過使用 intValue() 方法

在這種方法中,我們宣告一個隨機整數並建立一個整數包裝器物件。然後透過使用 intValue() 方法將其轉換為基本型別。

示例

public class Main {
   public static void main(String args[] ) {
      
      // Create an Integer wrapper object
      Integer integer = new Integer(10);
  
      // Convert the Integer wrapper object to a primitive int using intValue()
      int primitiveInt = integer.intValue();

      // Print the primitive int value
      System.out.println("Primitive int value: " + primitiveInt);
   }
}

輸出

Primitive int value: 10

方法 2:透過使用 valueOf 方法

在這種方法中,我們宣告一個隨機雙精度值並建立一個雙精度包裝器物件。然後透過使用 valueOf() 方法將其轉換為基本型別。

示例

public class Main {
   public static void main(String args[] ) {
      
      // Create a Double wrapper object
      Double doubleWrapper = new Double(10.5);
 
      // Convert the Double wrapper object to a primitive double using valueOf()
      double primitiveDouble = Double.valueOf(doubleWrapper);

      // Print the primitive double value
      System.out.println("Primitive double value: " + primitiveDouble);
   }
}

輸出

A Primitive double value: 10.5 

方法 3:透過使用拆箱運算子

在這種方法中,我們宣告一個隨機長整型值並建立一個長整型包裝器物件。然後透過使用拆箱運算子方法將其轉換為基本型別。

示例

public class Main {
   public static void main(String args[] ) {
      
      // Create a Long wrapper object
      Long longWrapper = new Long(10L);
      
      // Convert the Long wrapper object to a primitive long using unboxing
      long primitiveLong = longWrapper;
      
      // Print the primitive long value
      System.out.println("Primitive long value: " + primitiveLong);
   }
}

輸出

Primitive long value: 10

方法 4:使用型別轉換

在這種方法中,我們宣告一個隨機浮點數並建立一個浮點數包裝器物件。然後透過使用型別轉換方法將其轉換為基本型別。

示例

public class Main {
   public static void main(String args[] ) {
      
      // Create a Float wrapper object
      Float floatWrapper = new Float(10.5f);
      
      // Convert the Float wrapper object to a primitive float using type casting
      float primitiveFloat = (float) floatWrapper;     
      
      // Print the primitive float value
      System.out.println("Primitive float value: " + primitiveFloat);
   }
} 

輸出

Primitive float value: 10.5

在本文中,我們探討了使用 Java 程式語言將包裝器物件轉換為基本型別的不同方法。

更新於: 2023 年 5 月 4 日

3K+ 次瀏覽

開啟您的 職業生涯

透過完成課程獲得認證

開始學習
廣告