Java. Auto-boxing and unboxing in expressions and the switch statement

Auto-boxing and unboxing in expressions and the switch statement. Using autoboxing and unboxing values for boolean and char types


Search other resources:

1. Autoboxing and unboxing in expressions. Features of implementation

Autoboxing and unboxing are performed in expressions in cases where a mutual conversion of a value of a primitive type and the corresponding object of the wrapper type is required.
If a numeric value is boxed into a shell type object, then when used in expressions containing operations (+,  –, *, ++, –, etc.), this value is pre-unpacked.
Also, the value of the wrapper type object is unboxed when used in a switch statement.
It is allowed to perform operations in expressions on packed objects of various shell types (Integer, Double, Float, etc.).


2. Examples of autoboxing and unboxing in expressions
2.1. Binary operation +

The example shows the unboxing process when adding two values that are objects of wrapper types. Other operations work on a similar principle -, *, /,%, etc.

// Autoboxing and unboxing in expressions

// 1. Binary operation +
Double objD1, objD2;
Double resD;
objD1 = 25.5; // autoboxing Double <= double
objD2 = 30.5; // autoboxing Double <= double

// 2. Summation operation.
// 2.1. Unboxing objD1, objD2.
// 2.2. Calculation 25.5+30.5 => 56.0
// 2.3. Autoboxing 56.0 => Double
resD = objD1 + objD2;

In the above example, in the line

resD = objD1 + objD2;

the following sequence of actions is performed:

  • the objects objD1 and objD2 are unboxed and the values 25.5 and 30.5 of the primitive double type are obtained;
  • the sum for the primitive type double is calculated;
  • the result of the sum calculation is packed into a resD object.


2.2. Increment ++

The example shows the use of the increment operation on an object of wrapper type. The same principle of execution is carried out when using the decrement operation .

// Autoboxing and unboxing in expressions

// Increment operation ++
// 1. Declaring a variable
Integer objI = 40; // Boxing Integer <= int

// 2. Call the operator ++
// 2.1. Unpacking the value 40 into an int
// 2.2. Incrementing an int value by 1 gives 41
// 2.3. Boxing the value 41 in an Integer wrapper

In the above example, in the line


the following steps are performed:

  • first, the value of objI is unpacked;
  • the value of the objI object is incremented by 1. As a result, objI has the value 41;
  • the result is wrapped in an Integer wrapper.


3. Examples of autoboxing and unboxing in a switch statement

The example demonstrates the use of the auto-unboxing process in a switch statement. The day of the week entered is of the wrapper type Integer. In the switch statement, this day of the week is used as a control expression. When comparing in case statements, automatic unboxing from type Integer to type int occurs.

import java.util.Scanner;

public class TrainAutoPacking {

  public static void main(String[] args) {

    // Auto-boxing and unboxing in a switch statement
    // 1. Enter the day of the week, which is of type Integer
    Scanner input = new Scanner(;

    System.out.print("day = ");
    Integer dayWeek = input.nextInt();

    // 2. Using the Integer type as a control expression in a switch statement
    // Here, the value of the dayWeek object is unpacked into a value of type int
    switch (dayWeek) {
      case 1:
      case 2:
      case 3:
      case 4:
      case 5:
      case 6:
      case 7:
        System.out.println("Incorrect day");



4. Autoboxing and unboxing values between boolean and Boolean types. Features of use

The autoboxing and unboxing process for a boolean type is used when a conversion between the boolean primitive type and the Boolean wrapper type is required. This conversion can occur in the following cases:

  • when assigning (=) to a variable of type boolean an object of wrapper type Boolean. In this case, auto-unpacking occurs (boolean <= Boolean);
  • when assigning (=) to an object of shell type Boolean the value of a variable of primitive boolean type. In this case, autoboxing occurs (Boolean<=boolean);
  • in a conditional jump statement if, when an object of wrapper type Boolean is used as a condition. In this case, the boolean value is automatically unboxed into the boolean type;
  • in all for, while, do-while loop statements, provided that the Boolean shell type object is used as the iteration execution condition. In this case, an object of type Boolean is auto-unboxed into a primitive boolean type.


4.1. Use in assignment statements (=)


// Autoboxing and unboxing for Boolean type

// Autoboxing Boolean <= boolean
Boolean objB = true;

// Unboxing boolean <= Boolean
boolean b = ! objB; // false


4.2. Use in an if statement


// Autoboxing and unboxing for Boolean type
int x = 8;
int y = 5;
Boolean objB = x > y;

// Unboxing the value of the objB object: Boolean => boolean
if (objB)
  System.out.println("x > y");
  System.out.println("x < y");


4.3. Use in for, while, do-while loop statements

The example shows the use of an object of the Boolean wrapper type in the for, while, do-while loop statements. When checking the condition that determines the execution of the next iteration of the loop, the process of unpacking the value of the Boolean wrapper object into the boolean primitive type takes place.

import java.util.Scanner;

public class TrainAutoPacking {

  public static void main(String[] args) {

    // Autoboxing and unboxing for Boolean type.
    // Using in loops for, while, do-while

    // 1. The for loop. The sum calculation s = 1 + 2 + ... + 10
    int i = 1;
    Boolean objB = i<=10;
    int s = 0;

    // Unboxing the value objB: Boolean => boolean
    for (; objB; i++) {
      s += i;

      // forming the condition result
      objB = i<10;

    System.out.println("s = " + s);

    // 2. Loop while. Calculation the sum: 2 + 4 + 6 + ... + 100
    s = 0;
    i = 0;
    objB = i<=100;

    // Unboxing objB: Boolean => boolean
    while (objB) {
      s += i;
      i += 2;
      objB = i<=100;

    System.out.println("s = " + s);

    // 3. Loop do-while.
    // Task. Calculate the sum of numbers entered from the keyboard.
    // The end of the input is the number 0.
    Scanner input = new Scanner(;
    int number;

    System.out.println("Input numbers:");
    s = 0;
    do {
      number = input.nextInt(); // input the number
      s += number; // increase the sum
      objB = number != 0; // form a condition
    } while (objB); // automatic unboxing Boolean => boolean

    System.out.println("s = " + s);

The result of the program

s = 55
s = 2550
Input numbers:
s = 14


5. Autoboxing and unboxing values between char and Character types

The char type also performs autoboxing and unboxing processes.

Example. The example demonstrates the processes of mutual auto-boxing and auto-unboxing of char and Character types for the assignment (=) operation and the switch statement.

public class TrainAutoPacking {

  public static void main(String[] args) {

    // Autoboxint and unboxing for char and Character types.

    // 1. In the assignment operator
    Character objC = 'z'; // autoboxing Character <= char
    char c = objC; // unboxing char <= Character

    // 2. In the switch statement
    objC = '+';

    // here is the auto-unboxing of objC into char type
    switch (objC) {
      case '+':
      case '-':
      case '*':
      case '/':
        System.out.println("Undefined operation");


Related topics