オートボックス化は、JAVA で行われるプロセスであり、プリミティブ データの変換がコンパイラーによってオブジェクト型に変換されます。したがって、たとえば、変数が「int」として宣言されている場合、変数はコンパイラによってプリミティブ整数からオブジェクト データ型に変換され、その形式で使用されます。もう 1 つプロセスがあり、これはオートボックス化の逆であり、「アンボックス化」と呼ばれます。
無料ソフトウェア開発コースを始めましょう
Web 開発、プログラミング言語、ソフトウェア テスト、その他
定義: プリミティブ データ型の値をそれぞれのラッパー クラスのオブジェクトに変換することをオートボクシングと呼びます。
構文
オートボクシングに使用される構文は次のとおりです-
primitive_DataType variableName = 'VariableValue';
JAVA コンパイラはプリミティブ データ型を受け取り、それをラッパー クラスのオブジェクトに変換します。そして、コンパイラは以下の 2 点が満たされた場合にのみ動作します。
Primitive data type | Wrapper class |
boolean | Boolean |
byte | Byte |
long | Long |
double | Double |
short | Short |
char | Character |
Int | Integer |
float | Float |
Below are the examples of autoboxing in JAVA which are helpful to understand this concept further.
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:
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.
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:
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.
Some of the advantages derived from this useful functionality by JAVA are enlisted below:
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 中国語 Web サイトの他の関連記事を参照してください。