The unary plus operator performs an automatic conversion to int when the type of its operand is byte, char, or short. This is called unary numeric promotion, and it enables you to do things like the following:
char c = 'c';
int i = +c;
Granted, it's of limited use. But it does have a purpose. See the specification, specifically sections §15.15.3 and §5.6.1.
Answer from John Feminella on Stack OverflowVideos
Can unary operators be applied to all data types in Java
How does the logical complement operator work
What is the difference between preincrement and postincrement operators
The unary plus operator performs an automatic conversion to int when the type of its operand is byte, char, or short. This is called unary numeric promotion, and it enables you to do things like the following:
char c = 'c';
int i = +c;
Granted, it's of limited use. But it does have a purpose. See the specification, specifically sections §15.15.3 and §5.6.1.
Here's a short demonstration of what the unary plus will do to a Character variable:
private static void method(int i){
System.out.println("int: " + i);
}
private static void method(char c){
System.out.println("char: " + c);
}
public static void main(String[] args) {
Character ch = 'X';
method(ch);
method(+ch);
}
The output of running this programme is:
char: X
int: 88
How it works: Unary + or - unbox their operand, if it's a wrapper object, then promote their operand to int, if not already an int or wider. So, as we can see, while the first call to method will choose the char overload (unboxing only), the second call will choose the int version of method. Variable ch of type Character will be passed into method as int argument because of the applied unary plus.