What is the purpose of Java's unary plus operator?
Java's unary plus operator appears to have come over from C, via C++.
int result = +1; 开发者_JS百科
It appears to have the following effects:
- Unboxes its operand, if it's a wrapper object
- Promotes its operand to
int
, if it's not already anint
or wider - Complicates slightly the parsing of evil expressions containing large numbers of consecutive plus signs
It seems to me that there are better/clearer ways to do all of these things.
In this SO question, concerning the counterpart operator in C#, someone said that "It's there to be overloaded if you feel the need."
However, in Java, one cannot overload any operator. So does this unary plus operator exist in Java only because it existed in C++?
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.
I don't know, but I suspect it's there for symmetry with the (obviously necessary) unary minus operator.
One of Java's design goals was to be familiar (to a C/C++ developer), so when it came to operators like this I'm pretty sure they would have to have a strong reason to exclude it, not a good reason to need it.
My guess is it's there because sometimes typing the plus out makes things clearer. You might want to emphasize the fact that some number is positive, as opposed to some negative number.
Also, to provide a real world example where it's used, positive temperatures tend to be always prefixed with a plus in some parts of the world.
Many other languages have unary plus. It's customary to include it, and penny-pinching to exclude it. It's only a couple of lines in a compiler.
The unary operators do the following for java:
+
Unary plus operator; indicates positive value (numbers are positive without this, however)-
Unary minus operator; negates an expression++
Increment operator; increments a value by 1--
Decrement operator; decrements a value by 1!
Logical complement operator; inverts the value of a boolean
Source: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/opsummary.html
精彩评论