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).
boolean
.
Specifically, any of the following types:
byte
short
char
int
float
long
double
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 .
|
1 A unary plus expression.
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.
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.
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.
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.
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