Java Tutorial/Data Type/Autobox Unbox

Материал из Java эксперт
Перейти к: навигация, поиск

Autoboxing and Auto-Unboxing

  1. Related to Java"s type wrappers.
  2. How values are moved into and out of a wrapper instance
  3. 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.



   <source lang="java">

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);
 }

}</source>



100


Autoboxing/Unboxing Boolean and Character Values

   <source lang="java">

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);
 }

}</source>



b is true
ch2 is x


Autoboxing/unboxing occurs inside expressions

   <source lang="java">

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);
 }

}</source>



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.

   <source lang="java">

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);
 }

}</source>





Auto-unboxing allows you to mix different types of numeric objects in an expression.

   <source lang="java">

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);
 }

}</source>





Auto-unboxing: mix different types of numeric objects in an expression.

   <source lang="java">

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);
 }

}</source>



dOb after expression: 198.6


Boxing and Unboxing

  1. Boxing refers to the conversion of a primitive to a corresponding wrapper instance, such as from an int to a java.lang.Integer.
  2. Unboxing is the conversion of a wrapper instance to a primitive type, such as from Byte to byte.



   <source lang="java">

public class MainClass{

 public static void main(String[] args){
    
    Integer number = new Integer (100);
    int [] ints = new int [2];
    ints [0] = number;
 }

}</source>





Manually boxes the value 100 into an Integer

   <source lang="java">

public class MainClass {

 public static void main(String args[]) {
   Integer iOb = new Integer(100);
   int i = iOb.intValue();
   System.out.println(i);
 }

}</source>



100


The modern way to construct an Integer object that has the value 100

   <source lang="java">

public class MainClass {

 public static void main(String args[]) {
   Integer iOb = 100; // autobox an int
   System.out.println(iOb);
 }

}</source>



100


To unbox an object

Simply assign that object reference to a variable of its corresponding primitive type



   <source lang="java">

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); 
 }

}</source>





Type conversion (JDK1.5 Autoboxing/Unboxing)

   <source lang="java">

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);
 }

}</source>





Using an integer object to control a switch statement

   <source lang="java">

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");
   }
 }

}</source>



two