Java - Unary Plus Operator +

What is Unary Plus Operator?

The unary plus operator (+) is used in the form

+operand 
  

The operand must be a primitive numeric type.

If the operand is of the byte, short, or char type, the unary plus operator promotes it to int type.

 
byte b1 = 10; 
byte b2 = +5; 
b1 = b2;  // Ok. byte to byte assignment 

In the following code, b2 is of the type byte. unary plus operator on b2 promoted its type to int. +b2 is of the type int. int to byte assignment is not allowed.

byte b1 = 10; 
byte b2 = +5; 
b1 = +b2; // A compile-time error. 
  

To fix the error, add casting.

b1 = (byte) +b2; // Ok