首頁 >Java >java教程 >在 Java 中拆箱

在 Java 中拆箱

王林
王林原創
2024-08-30 15:09:02924瀏覽

拆箱是將JAVA中包裝類別的物件轉換為原始資料型別的過程。它作為“java.lang”套件的一部分存在,導入該套件後可以在 jAVA 程式中使用。例如,將包裝類別Integer的物件轉換為Int。這與JAVA中的自動裝箱相反。它是作為 JAVA 5 開發的一部分引入的,使開發人員的工作變得更加輕鬆。

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

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

文法

JAVA 中拆箱使用的語法如下:

import java.lang.*
class mainClass
{
public static void main (String args[] )
{
Integer variableName=new Integer( VariableValue);
variableName2 = variableName; //Unboxing in JAVA
}
}

這裡,包裝類別「Integer」的物件被指派給原始資料類型Int,並且該值作為參數傳遞到包裝類別的建構子中。這在自動裝箱中隱式發生。

Java 中拆箱是如何運作的?

拆箱是將物件轉換為原始資料型別的過程。此過程由 JAVA 編譯器自動完成,因為 JAVA 中的庫在 JAVA 第五版及更高版本中支援它。要透過 JAVA 編譯器進行拆箱運行,需要滿足兩個先決條件。下面列出了這兩個先決條件:

  1. 當所使用的包裝類別的物件需要相關原始資料類型的「值」時,編譯器會使用拆箱。
  2. 所使用的包裝類別的物件依序分配給原始資料類型的變數。

以下是包含包裝類別及其相關原始資料類型的表格:

Wrapper class Related primitive data-type
Boolean boolean
Integer Int
Float float
Character char
Byte byte
Long long
Short short
Double double

The data flow and explanation of unboxing in JAVA is explained properly with examples in the below section.

Examples of Unboxing in Java

Some examples are provided below will provide a conceptual clarity of the unboxing technique.

Example #1

Code:

public class test1
{
public static void main(String args[])
{
Integer var1=new Integer(50);
if( var1 > 10) // Unboxing using comparator.
{
int var2=var1 + 10;
int var3=var2;
System.out.println(" The value of variable using unboxing functionality is JAVA is :"+ var3);
} else
{
int var2=var1 - 10;  //Unboxing using assignment operator.
int var3=var2;
System.out.println(" The value of variable using unboxing functionality is JAVA is :"+ var3);
}
}
}

Output:

在 Java 中拆箱

Explanation: In this program, The main class is declared as “test1” as it contains the main() method. The program execution starts with the main() function. An object of the wrapper class “Integer” is created with the name “var1” and assigned with the value “50”. You should focus on the syntax of assigning value to variable “var1”, which is different in comparison to autoboxing. Here, the object is used instead of the data type for declaration and assignment purposes. Once the assignment is done, unboxing is done for that object.

Here, a comparison operator is used to unboxing the object. “If” logic checks if the value of “var1” is more than 10 or not. If not, then the control flows to another part of the program, starting with the “else” keyword and the whole code snippet under if loop will be skipped. In the else section, there is no comparator operator, so it enters the control logic. Assignment operator “=” does the unboxing part in case else is invoked. You can change the value of “var1” by changing the parameter provided to the wrapper class’s constructor (“Integer()” in this example). Finally, value is added or subtracted and printed as per logic.

Example #2

Code:

public class test2
{
public static void main(String args[])
{
Character charName = 'M'; // Autoboxing.
char charName2 = charName; // Unboxing
System.out.println("The process used here is auto-unboxing to display the character : "+ charName2 );
}
}

Output:

在 Java 中拆箱

Explanation: Here, unboxing is done using the assignment operator. The data flow and control execution will work, as explained in the previous example. Here one thing to notice is that we have not used the object of a wrapper class to declare and assign the value to the variable “charName”. Although unboxing is done on “charName” using the assignment operator.

Example #3

Code:

public class test3 {
public static void main (String args[]){
Integer varName = new Integer("1000");
int varName2 = varName.intValue();
System.out.println("Variable name is printed after unboxing using a built-in function is : " + varName2);
}
}

Output:

在 Java 中拆箱

Explanation: This works similar to the previous example with an added function called “intValue(). This function should extract the value from the variable “varName” and assign it to another variable named “varName2”. The function “intValue()” explicitly returns the value of object ”varName”. This is exactly what the compiler does in the backend. You should try removing this function and see the results to compare it with example number 2.

Advantages

Some of the primitive advantages of unboxing in JAVA is presented in the form of the list below:

  1. It represents the true use of JAVA’s object-oriented features.
  2. Standard code and syntax style used throughout the project but increased the number of lines in code.
  3. Cleaner and understandable code.
  4. Easy to maintain by the AMS (Application maintenance support) team for big projects.

Conclusion

Unboxing is the reverse of autoboxing in JAVA. It is converting the wrapper class’s object into a primitive data type. Although we have the functionality of declaring and assigning variables using primitive data type directly (That is called the autoboxing feature of JAVA), objectification of wrapper class and then assigning this object to a primitive data type is used several times when standardization of code is important. This reveals the true object-oriented property of JAVA. It is used mostly in big projects for easy maintenance.

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

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