Java 的一元加运算符似乎是通过 C++ 从 C 而来的。
int result = +1;
它似乎具有以下效果:
- 如果它是包装对象,则将其操作数拆箱
- 如果它还不是
int
或更宽的
- 操作数,则将其提升为
int
使解析稍微复杂化包含大量连续加号的邪恶表达式
在我看来,有更好/更清晰的方法来完成所有这些事情。
在这个SO问题中,关于C#中的对应运算符,有人说“如果你觉得有需要,它就可以超载。”
然而,在 Java 中,不能重载任何运算符。那么这个一元加运算符在 Java 中存在只是因为它在 C++ 中存在吗?
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++?
发布评论
评论(7)
当操作数的类型为
byte
、char
或short
时,一元加运算符会自动转换为int
>。这称为一元数字提升,它使您能够执行以下操作:当然,它的用途有限。但它确实有一个目的。请参阅规范,特别是 §15.15.3 和 §5.6.1。
The unary plus operator performs an automatic conversion to
int
when the type of its operand isbyte
,char
, orshort
. This is called unary numeric promotion, and it enables you to do things like the following:Granted, it's of limited use. But it does have a purpose. See the specification, specifically sections §15.15.3 and §5.6.1.
下面是一元加号对
Character
变量执行的操作的简短演示:运行该程序的输出是:
工作原理:一元 + 或 - 取消装箱其操作数,如果它是包装对象,则将它们的操作数提升为 int(如果还不是 int 或更宽的值)。因此,正如我们所看到的,虽然第一次调用
method
将选择char
重载(仅拆箱),但第二次调用将选择int
>方法
的版本。由于应用了一元加法,Character
类型的变量ch
将作为int
参数传递到method
中。Here's a short demonstration of what the unary plus will do to a
Character
variable:The output of running this programme is:
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 thechar
overload (unboxing only), the second call will choose theint
version ofmethod
. Variablech
of typeCharacter
will be passed intomethod
asint
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.
Java 的设计目标之一是(对于 C/C++ 开发人员)熟悉,因此当涉及到这样的运算符时,我很确定他们必须有充分的理由排除它,而不是需要它的充分理由。
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.
一元运算符对 java 执行以下操作:
+
一元加运算符;表示正值(但是,如果没有这个,数字就是正数)-
一元减运算符;对表达式求反++
增量运算符;将值增加 1--
递减运算符;将值减 1!
逻辑求补运算符;反转布尔值源:https://docs.oracle。 com/javase/tutorial/java/nutsandbolts/opsummary.html
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 booleanSource: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/opsummary.html