Search code examples
javaintegerboxingunboxing

Java boxing and unboxing


I have an example in which I cannot determine the number of boxing(s) and unboxing(s), which taking place in the Java code below :

int x = 5;
Integer y = x + x;

From my point of view I see one type of boxing (Integer y = x + x). Am I wrong? Is there any unboxing as well?


Solution

  • There is no unboxing. Just boxing happening.

    First the expression x+x calculated which is an int and that is boxed to Integer.

    So in the whole statement there is no conversion of Integer to int, hence no unboxing.