Java Tutorial/Data Type/Autobox Unbox
Содержание
- 1 Autoboxing and Auto-Unboxing
- 2 Autoboxing/unboxing: an argument passed to a method or returned from a method
- 3 Autoboxing/Unboxing Boolean and Character Values
- 4 Autoboxing/unboxing occurs inside expressions
- 5 Autoboxing/unboxing takes place with method parameters and return values.
- 6 Auto-unboxing allows you to mix different types of numeric objects in an expression.
- 7 Auto-unboxing: mix different types of numeric objects in an expression.
- 8 Boxing and Unboxing
- 9 Manually boxes the value 100 into an Integer
- 10 The modern way to construct an Integer object that has the value 100
- 11 To unbox an object
- 12 Type conversion (JDK1.5 Autoboxing/Unboxing)
- 13 Using an integer object to control a switch statement
Autoboxing and Auto-Unboxing
- Related to Java"s type wrappers.
- How values are moved into and out of a wrapper instance
- Java primitive type wrappers: Boolean, Byte, Character, Double, Float, Long, Integer, Short.
Autoboxing/unboxing: an argument passed to a method or returned from a method
Autoboxing/unboxing takes place with method parameters and return values.
public class MainClass {
static int m(Integer v) {
return v ; // auto-unbox to int
}
public static void main(String args[]) {
Integer iOb = m(100);
System.out.println(iOb);
}
}
100
Autoboxing/Unboxing Boolean and Character Values
public class MainClass {
public static void main(String args[]) {
Boolean booleanObject = true;
if (booleanObject){
System.out.println("b is true");
}
Character ch = "x"; // box a char
char ch2 = ch; // unbox a char
System.out.println("ch2 is " + ch2);
}
}
b is true ch2 is x
Autoboxing/unboxing occurs inside expressions
public class MainClass {
public static void main(String args[]) {
Integer intObject, intObject2;
int i;
intObject = 100;
System.out.println("Original value of iOb: " + intObject);
++intObject;
System.out.println("After ++iOb: " + intObject);
intObject2 = intObject + (intObject / 3);
System.out.println("iOb2 after expression: " + intObject2);
i = intObject + (intObject / 3);
System.out.println("i after expression: " + i);
}
}
Original value of iOb: 100 After ++iOb: 101 iOb2 after expression: 134 i after expression: 134
Autoboxing/unboxing takes place with method parameters and return values.
class AutoBox2 {
static int m(Integer v) {
return v; // auto-unbox to int
}
public static void main(String args[]) {
Integer iOb = m(100);
System.out.println(iOb);
}
}
Auto-unboxing allows you to mix different types of numeric objects in an expression.
class AutoBox4 {
public static void main(String args[]) {
Integer iOb = 100;
Double dOb = 98.6;
dOb = dOb + iOb;
System.out.println("dOb after expression: " + dOb);
}
}
Auto-unboxing: mix different types of numeric objects in an expression.
public class MainClass {
public static void main(String args[]) {
Integer intObject = 100;;
Double doubleObject = 98.6;
doubleObject = doubleObject + intObject;
System.out.println("dOb after expression: " + doubleObject);
}
}
dOb after expression: 198.6
Boxing and Unboxing
- Boxing refers to the conversion of a primitive to a corresponding wrapper instance, such as from an int to a java.lang.Integer.
- Unboxing is the conversion of a wrapper instance to a primitive type, such as from Byte to byte.
public class MainClass{
public static void main(String[] args){
Integer number = new Integer (100);
int [] ints = new int [2];
ints [0] = number;
}
}
Manually boxes the value 100 into an Integer
public class MainClass {
public static void main(String args[]) {
Integer iOb = new Integer(100);
int i = iOb.intValue();
System.out.println(i);
}
}
100
The modern way to construct an Integer object that has the value 100
public class MainClass {
public static void main(String args[]) {
Integer iOb = 100; // autobox an int
System.out.println(iOb);
}
}
100
To unbox an object
Simply assign that object reference to a variable of its corresponding primitive type
public class MainClass {
public static void main(String args[]) {
Integer iOb = 100; // autobox an int
int i = iOb; // auto-unbox
System.out.println(i + " " + iOb);
}
}
Type conversion (JDK1.5 Autoboxing/Unboxing)
public class Main {
public static void main(String... args) {
Integer integer = 1; // int into Integer
System.out.println(integer);
int i = integer + 3; // mix Integer and ints
System.out.println(i);
}
}
Using an integer object to control a switch statement
public class MainClass {
public static void main(String args[]) {
Integer intObject = 2;
switch(intObject) {
case 1: System.out.println("one");
break;
case 2: System.out.println("two");
break;
default: System.out.println("error");
}
}
}
two