I have seen in this documentation that Autoboxing and Unboxing apply to the = operator, and that Unboxing apply to the % operator and the += operator.
But which other operators does Autoboxing and Unboxing apply to?
I have seen in this documentation that Autoboxing and Unboxing apply to the = operator, and that Unboxing apply to the % operator and the += operator.
But which other operators does Autoboxing and Unboxing apply to?
Autoboxing happens if you have an expression resulting in a primitive type but you use it where its wrapper class is expected like here:
Integer i=1+2;//int is boxed to Integer
Similarily, automated unboxing happens if you have an expression resulting in a wrapper class but a corresponding primitive expression is expected like here:
Integer boxed=Integer.valueOf("1337");//some boxed integer
int unboxed=boxed;//automated unboxing
Both assignments and method calls can result in implicit boxing and unboxing.
Since there are some operators that support primitive types but not wrapper classes, the compiler will unbox boxed primitives implicitely if used with these operarors. Such operators are all arithmetic and boolean and comparison operators (+, -, *, /, %, >>, <<, >>>, ^, &, &&, !, ~, |, ||, >, <, >=, <=, ++, --, ?)
Use of operators that assign the left hand variable to the result of a computation (+= etc.) can result both in implicit unboxing (for the computation) and unboxing (for the assignment)
Aside from this, the == (and !=) operator can result in automatic unboxing if one operand is a primitive expression and the other is an expression resulting in a value of the corresponding wrapper class.