Unary Plus

Converts its operand to int, long, float, or double. The value is preserved.

The unary plus operator may have any primitive numeric type (or corresponding wrapper type). The result of the operation is the same value as the input, but converted to a primitive type (if the operand's type is a wrapper type) and converted to int (if the operand is byte, char, short, or a wrapper of one of those types).

A primitive numeric type is any primitive type other than boolean. Specifically, any of the following types:

Syntax

Unary Plus can be expressed through:

1 + operand

where...

operand is an expression whose type is a primitive numeric type or a wrapper of a primitive numeric type (such as Integer or Long.

Syntax Elements

1 A unary plus expression.

Inputs

Like all other unary operators (except for the cast operator), the unary plus operator accepts an expression of any primitive, numeric type (byte, short, char, int, long, float, or double), or an expression of any numeric wrapper type (Byte, Short, Character, Integer, Long, Float, or Double). If a wrapper type is provided, the type is first unboxed into its primitive counterpart.

If a wrapper type is provided as the operand of a unary plus operator, it is unboxed before a promotion to int is attempted. For example,
Integer x = Integer.valueOf(123); // Variable x stores an Integer object that represents the value 123
System.out.println(+x); // x is "unboxed" into 123 as an int

The above code is equivalent to:

Integer x = Integer.valueOf(123);
System.out.println((int) x);

That is to say that the operand of + is converted to its primitive form if not a primitive type.

Behavior

Unboxing

If the operand expression has a type that is a wrapper type, the value is unboxed to its primitive form. For example, an operand of type Long will be converted to long. See the below examples for details.

Numeric Promotion

After unboxing, (if applicable), the operand is promoted to type int if its type is byte, short, or char. This conversion does not change the numeric value of the operand, (since every byte, short, and char can be represented as an int as well.

Operands of type int, long, float, and double are not changed by the unary plus operator.

Examples

Unboxing

The unary plus operator will first unbox its argument before attempting numeric promotion:

static void example(long arg) {
	System.out.println("Primitive method called");
}
static void example(Long arg) {
	System.out.println("Wrapper method called");
}

public static void main(String[] args) {
	Long x = 123L;
	example(x);	// Calls example(Long)
	example(+x);	// Calls example(long)
}

Output:

Wrapper method called
Primitive method called