What is the purpose of Java's unary plus operator?

狂风中的少年 提交于 2019-11-26 00:49:12

问题


Java\'s unary plus operator appears to have come over from C, via C++.

int result = +1;   

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 an int 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++?


回答1:


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.




回答2:


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.




回答3:


I don't know, but I suspect it's there for symmetry with the (obviously necessary) unary minus operator.




回答4:


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.




回答5:


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.




回答6:


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.




回答7:


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



来源:https://stackoverflow.com/questions/2624410/what-is-the-purpose-of-javas-unary-plus-operator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!