Autoboxing and auto-unboxing

In this chapter you will learn:

  1. Java Type Wrappers
  2. What are auto-boxing and auto-unboxing
  3. How to use methods and auto-boxing
  4. How do expressions deal with autoboxing/unboxing
  5. 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:

  1. What are arrays in Java
  2. What is One-Dimensional Arrays
  3. How to allocate memory for array
Home » Java Tutorial » Primitive Data Types

Introduction

    Java Primitive Data Types

Boolean

    Java boolean type
    Java boolean type conversion
    Convert string value to boolean
    Convert boolean to string

Char

    Java char type
    Compare two char values
    Change char case
    Java char conversion
    Java char value and its attributes

Byte

    Java byte type
    Convert Byte to String
    Convert String to byte
    Byte object constructor
    Byte's max and min value
    Compare two byte values
    Convert Byte to byte, double, float, int, long and short

Short

    Java short type
    Short min/max value and size
    Create Short object
    Compare short values
    Convert short to String
    Convert Short to primitive types
    Convert string to short
    Reverse bytes

Integer

    Java int type
    int max/min value
    Create Java integer
    Convert int to binary, hexadecimal and octal format
    Compare integer values
    Integer sign
    Convert string to int
    Convert int to primitive types
    Convert int to String
    int bit operations

Long

    Java long type
    Compare two long values
    Convert long to binary, hex and octal
    Convert long value to primitive types
    Convert String to long value
    Convert long to String

Float

    Java float type
    Java float type conversion
    Predefined value for float type
    Compare two float value

Double

    Java double type
    Deal with NaN double value
    Compare two double values
    Java double type creation and comparison
    Java double type conversion

Data Type Conversion

    Java Automatic Type Conversion and Casting
    Data type casting
    Java type promotion
    Autoboxing and auto-unboxing