Autoboxing and auto-unboxing
In this chapter you will learn:
- Java Type Wrappers
- What are auto-boxing and auto-unboxing
- How to use methods and auto-boxing
- How do expressions deal with autoboxing/unboxing
- Autoboxing/Unboxing Boolean and Character Values
Type Wrappers
Java uses primitive types, such as int
or double
, to
hold the basic primitive data types.
Primitive types, rather than objects, are used for the sake of performance.
Java provides type wrappers, which are classes that encapsulate a primitive type within an object. The type wrappers are Double, Float, Long, Integer, Short, Byte, Character, and Boolean.
Autoboxing and Auto-unboxing
Autoboxing is the process by which a primitive type is automatically encapsulated (boxed) into its equivalent type wrapper whenever an object of that type is needed.
Auto-unboxing is the process by which the value of a boxed object is automatically extracted (unboxed) from a type wrapper when its value is needed.
For example, the following code constructs an Integer object that has the value 100:
public class Main {
//from j a v a 2 s. c o m
public static void main(String[] argv) {
Integer iOb = 100; // autobox an int
}
}
To unbox an object, simply assign that object reference to a primitive-type variable.
For example, to unbox iOb
, you can use this line:
public class Main {
//j a v a 2s . com
public static void main(String[] argv) {
Integer iOb = 100; // autobox an int
int i = iOb; // auto-unbox
}
}
Here is a program using autoboxing/unboxing:
public class Main {
public static void main(String args[]) {
Integer iOb = 100; // autobox an int
int i = iOb; // auto-unbox
System.out.println(i + " " + iOb); // displays 100 100
}//j av a2 s. c o m
}
Output:
Autoboxing and Methods
autoboxing occurs whenever a primitive type must be converted into an object, while auto-unboxing takes place whenever an object must be converted into a primitive type.
autoboxing/unboxing might occur when an argument is passed to a method, or when a value is returned by a method. For example, consider this example:
public class Main {
static int m(Integer v) {
return v; // auto-unbox to int
}//from j a v a 2 s .co m
public static void main(String args[]) {
Integer iOb = m(100);
System.out.println(iOb);
}
}
The output:
Autoboxing/Unboxing in Expressions
Within an expression, a numeric object is automatically unboxed. The outcome of the expression is reboxed, if necessary. For example, consider the following program:
public class Main {
public static void main(String args[]) {
Integer integerObject, integerObject2;
/* j a va2s . c o m*/
integerObject = 1;
System.out.println(integerObject);
++integerObject;
System.out.println(integerObject);
integerObject2 = integerObject + (integerObject / 3);
System.out.println(integerObject2);
int i = integerObject + (integerObject / 3);
System.out.println(i);
}
}
Auto-unboxing allows you to mix different types of numeric objects in an expression.
public class Main {
public static void main(String args[]) {
Integer integerObject = 10;
Double doubleObject = 9.6;/*from java2 s . c o m*/
doubleObject = doubleObject + integerObject;
System.out.println(doubleObject);
}
}
Because of auto-unboxing, you can use integer numeric objects to control a switch statement.
public class Main {
public static void main(String args[]) {
Integer iOb = 10;
switch (iOb) {
case 1:/*from j a va2s. c o m*/
System.out.println("one");
break;
case 2:
System.out.println("two");
break;
default:
System.out.println("error");
}
}
}
The output:
Autoboxing/Unboxing Boolean and Character Values
The following code shows we can do the autoboxing and unboxing for a char type value.
public class Main {
public static void main(String args[]) {
// Autobox/unbox a boolean.
Boolean b = true;/* j ava 2 s. c om*/
if (b == true){
System.out.println("b is true");
}
// Autobox/unbox a char.
Character ch = 'x';
char ch2 = ch;
System.out.println("ch2 is " + ch2);
}
}
Next chapter...
What you will learn in the next chapter: