首頁 > Java > java教程 > 主體

Java 中的自動裝箱

WBOY
發布: 2024-08-30 15:58:00
原創
936 人瀏覽過

自動裝箱是 JAVA 中遵循的過程,其中編譯器將原始資料轉換為物件類型。因此,例如,如果變數被宣告為“int”,則編譯器會將該變數從原始整數轉換為物件資料類型,並以該格式使用。還有一個過程是自動裝箱的逆轉,稱為「拆箱」。

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

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

定義:將原始資料類型的任何值轉換為對應包裝類別的物件稱為自動裝箱。

文法

自動裝箱使用的語法如下-

primitive_DataType variableName = 'VariableValue';
登入後複製

自動裝箱在 Java 中如何運作?

JAVA編譯器採用原始資料型別並將其轉換為包裝類別的物件。那麼,只有滿足以下兩點,編譯器才能運作:

  1. 變數作為參數傳遞給函數,該函數需要包裝類別的物件建立。
  2. 原始值被指派給變量,以便編譯器可以執行自動裝箱。
  3. 所有原始資料類型都連結到各自的包裝類別(這已經由儲存在其庫中的 JAVA 完成)。原始資料型別及其連結的包裝類別連結為:
Primitive data type Wrapper class
boolean Boolean
byte Byte
long Long
double Double
short Short
char Character
Int Integer
float Float

Examples of Autoboxing in Java

Below are the examples of autoboxing in JAVA which are helpful to understand this concept further.

Example #1

Code:

import java.io.*;
class example1
{
public static void main (String[] args)
{
//here we are creating an integer "test1" having value of "100".
Integer test1 = new Integer(100);
System.out.println("Here is the value of integer test1 which is created into object of the wrapper class integer: " + test1);
//here we are autoboxing a character "test2" with a value of "a".
Character test2 = 'a';
System.out.println("Here is the value of test2 variable after autoboxing using character wrapper class: " + test2);
}
}
登入後複製

Output:

Java 中的自動裝箱

Explanation:

The JAVA library named “java.io” was imported so that JAVA prime functionalities can be used. First, the main class name “example1” was created containing the main function. The main function is the point where the execution of the program will start. Next, an integer named “test1” was created and assigned with the value. It is slightly different from the normal way of declaring and assigning the values; as you can see, the wrapper class “Integer” was instantiated, and the value to be assigned is passed as a parameter to the wrapper class. This process is called objectifying the primitive data type using the wrapper class. No doubt that “JAVA” is called a fully object-oriented language.

In the next section of code, the Character primitive data type is assigned to a variable named “test2” with a value “a.” This is a usual way of declaring and assigning the variable with the value. This is working because the JAVA compiler is smart enough to convert the primitive data type into an object of its wrapper class automatically in the back. This functionality is called autoboxing in JAVA.

Example #2

Code:

public class BoxingWidening
{
// The function below is demonstrating the boxing functionality in JAVA.
static void testFunction(char i)
{
System.out.println("Program output for boxing:: ");
System.out.println("char in short");
}
// This function is demonstrating the widening functionality in JAVA.
static void testFunction(Character i)
{
System.out.println("Program output for widening: ");
System.out.println("Character in full");
}
public static void main(String args[])
{
char ch='a';
testFunction(ch);
}
}
登入後複製

Output:

Java 中的自動裝箱

Explanation:

In this example, the difference between “widening” and “boxing” is demonstrated. Widening is a functionality where the full class name is used, for example, Integer or character instead of int or char. Two functions are defined with the same name, “testFunction,” one with widening and the other with boxing syntax declarations and assignments. In the main function, a character named “ch” is declared and assigned with a value “a,” and a function is called with its function name. This function has two definitions on its name. Since JAVA prefers boxing, the function’s boxing definition is called, and the parameter “ch” is passed to the boxing definition. The final result is displayed using the print() function. This function shows boxing definition in the output screen rather than taking a widening definition. Certainly, autoboxing is much powerful in comparison to the widening functionality offered by the JAVA language.

Advantages

Some of the advantages derived from this useful functionality by JAVA are enlisted below:

  1. The developer has to write less code, and this reduces the burden on the coder so that the coder can concentrate on complex logic.
  2. Cleaner code as the complexity of creating an object and instantiating it is abstracted due to compiler intelligence.
  3. The best strategy for information is picked up by the compiler using this. For example, if we want the value of the integer to be picked up, then we should use code: valueOf (int) rather than new Integer (int). This improves turnaround time.
  4. Promotes abstraction.
  5. Reduces the possibility of bugs in the lengthy code/ project because of abstraction.

Conclusion

Autoboxing is one of the best strategies to abstract complex code from the developer by adding compiler intelligence. This reduces the overhead on the developer of writing small code snippets which are not logically related. This is used so extensively by developers that the instantiating of a wrapper class is not known to many developers. Autoboxing is certainly a powerful and cleaner way of declaring and assigning the values to the variables.

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

相關標籤:
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板