As of Java 5.0, you no longer need to write the code to wrap primitive types into their corresponding
wrapper class; the compiler now does this for you behind the scenes. The term autoboxing refers to the compiler automatically converting a primitive type into its corresponding
wrapper class. The term unboxing refers to the compiler automatically unwrapping
a primitive type from its wrapper object. Autoboxing and unboxing do not require
any special syntax. The compiler realizes situations where primitives need to be boxed or
unboxed. For example, if an int needs to be treated as an object, it automatically becomes
an Integer . Similarly, an Integer automatically becomes an int whenever necessary.
Before autoboxing, you would wrap a primitive by instantiating a new wrapper object:
Integer w = new Integer(10);
With autoboxing, you simply assign a wrapper class reference to a primitive type:
Integer w = 10;
Behind the scenes, the compiler instantiates a new Integer object for the int 10 .
Similarly, the compiler unboxes a wrapped primitive whenever necessary. The following
statements are valid:
5. Double pi = 3.14159;
6. double radius = 10.0;
7. double area = pi * radius * radius;
The Double object on line 5 is automatically instantiated and wraps the value 3.14159 .
On line 7, pi is automatically unwrapped into a double before the multiplication occurs.
Before unboxing, we would have used the doubleValue method:
double area = pi.doubleValue() * radius * radius;
The autoboxing occurs whenever the compiler realizes that a primitive needs to be
wrapped in its corresponding wrapper class. For example, the ScoreKeeper class from the
previous section contains the following method:
public void addScore(Object score) {
scores.add(score);
}
The following statements are valid method invocations of addScore :
keeper.addScore(50);
keeper.addScore(23.4);
keeper.addScore(18.5F);
Behind the scenes, the 50 is converted to an Integer , 23.4 is converted to a Double , and
18.5F is converted to a Float .
wrapper class; the compiler now does this for you behind the scenes. The term autoboxing refers to the compiler automatically converting a primitive type into its corresponding
wrapper class. The term unboxing refers to the compiler automatically unwrapping
a primitive type from its wrapper object. Autoboxing and unboxing do not require
any special syntax. The compiler realizes situations where primitives need to be boxed or
unboxed. For example, if an int needs to be treated as an object, it automatically becomes
an Integer . Similarly, an Integer automatically becomes an int whenever necessary.
Before autoboxing, you would wrap a primitive by instantiating a new wrapper object:
Integer w = new Integer(10);
With autoboxing, you simply assign a wrapper class reference to a primitive type:
Integer w = 10;
Behind the scenes, the compiler instantiates a new Integer object for the int 10 .
Similarly, the compiler unboxes a wrapped primitive whenever necessary. The following
statements are valid:
5. Double pi = 3.14159;
6. double radius = 10.0;
7. double area = pi * radius * radius;
The Double object on line 5 is automatically instantiated and wraps the value 3.14159 .
On line 7, pi is automatically unwrapped into a double before the multiplication occurs.
Before unboxing, we would have used the doubleValue method:
double area = pi.doubleValue() * radius * radius;
The autoboxing occurs whenever the compiler realizes that a primitive needs to be
wrapped in its corresponding wrapper class. For example, the ScoreKeeper class from the
previous section contains the following method:
public void addScore(Object score) {
scores.add(score);
}
The following statements are valid method invocations of addScore :
keeper.addScore(50);
keeper.addScore(23.4);
keeper.addScore(18.5F);
Behind the scenes, the 50 is converted to an Integer , 23.4 is converted to a Double , and
18.5F is converted to a Float .
No comments:
Post a Comment