The term autoboxing is used to create an object that represents an original data type. The term unboxing is used to create an original data type that represents the value of an object. The autoboxing/unboxing feature in Java 1.5 allows us to pass the original type parameters to the methods that we expect to wrap the object. Similarly, it also allows us to pass the object to the methods that would expect the original type parameters.
For example, suppose we have a method foo (Interger val ). Before Java 1.5, to pass an original type value to this method, you need to do so in the following ways:
Int primitiveVal = 8;
Foo (Integer. valueOf (8 ));
In Java 1.5, you can use the raw data to directly call foo without creating an instance of the Integer packaging class, as shown below:
Foo (primitiveVal );
If we have a method bar (int val) that expects the original type parameter, there is also a simple method for processing.
The following is an old method:
Integer wrapper = new Integer (8 );
Bar (wrapper. intValue ());
Here is the new method:
Bar (wrapper );
The compiler has already assisted us. If you analyze the code created by the compiler for the following class, you will find that the compiler uses the same calling method in the old version. Here is a summary of the output results of running javap-c for the BoxingTip class:
Public static void main (java. lang. String []);
...
10: bipush 9
12: istore_2
13: iload_2
14: invokestatic #4; // Method
Java/lang/Integer. valueOf :( I) Ljava/lang/Integer;
17: invokestatic #5; // Method foo :( Ljava/lang/Integer;) V
20: aload_1
21: invokevirtual #6; // Method java/lang/Integer. intValue :() I
24: invokestatic #7; // Method bar :( I) V
27: return
...
In row 3, the compiler calls the Integer static method valueOf () to provide the required value for the foo () method. In row 21st, the intValue () method of the existing Integer instance is called to extract the int value and pass it to the bar () method. We can use the autoboxing/unboxing features provided by the compiler without having to understand what is happening, but knowing what the compiler has done for you is never a bad thing.
In Java 1.5, this new feature not only provides syntax convenience. It is an evolution, not a revolution, but it is the arrival of the new era, it will make it easier to write Java code.
Public class BoxingTip {
Public static void main (String [] args ){
Integer wrapper = new Integer (8 );
Int primitiveValue = 9;
Foo (primitiveValue );
Bar (wrapper );
}
Static void foo (Integer val ){
System. out. println (val );
}
Static void bar (int val ){
System. out. print (val );
}
}
Note: the code in this prompt is compiled using Java build 1.5.0-beta-b32c in Windows 2000. To compile this code, you must use the javac "-source 1.5" option.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.