What exactly does a +
symbol do in front of an expression in Java?
In Java, a mathematical expression can be lead by a -
symbol, as well as the +
symbol, like in the following:
System.out.println(-5); // Prints -5
System.out.println(+5); // Prints 5
System.out.println(-(3 + 2)); // Prints -5
System.out.println(+(3 + 2)); // Prints 5
The -
symbol clearly negates the value of the following expression and the +
symbol does nothing. So if the -
symbol negates an expression, I'd expect the +
symbol to force an expression to be positive, otherwise it'd have no use, but when I tested this...
int pos = 5;
System.out.println(-pos);// -5
System.out.println(+pos);// 5
System.out.println(+ + + + + + + +pos);// 5
int neg = -5;
System.out.println(-neg);// 5
System.out.println(+neg);// -5
System.out.println(+ + + + + + + +neg);// -5
System.out.println(5);// 5
System.out.println(-5);// -5
System.out.println(+5);// 5
System.out.println(-+5);// -5
System.out.println(+-5);// -5
(The comment is what gets printed for that line.) It seems to me that the +
sign in front of an expression is useless, but that makes me wonder why Java allows that code.
So my question is, what is the use of a +
symbol in front of an expression in Java.