简体   繁体   English

Java的一元加运算符的目的是什么?

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

Java's unary plus operator appears to have come over from C, via C++. Java 的一元加号运算符似乎是通过 C++ 从 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将其操作数提升为int ,如果它还不是int或更宽的
  • 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."这个 SO question 中,关于 C# 中的对应运算符,有人说“如果你觉得需要,它就会被重载。”

However, in Java, one cannot overload any operator.但是,在 Java 中,不能重载任何运算符。 So does this unary plus operator exist in Java only because it existed in C++?那么这个一元加号运算符是否只存在于 Java 中,因为它存在于 C++ 中?

The unary plus operator performs an automatic conversion to int when the type of its operand is byte , char , or short .当其操作数的类型为bytecharshort时,一元加运算符执行自动转换为int 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 .请参阅规范,特别是第§15.15.3§5.6.1部分。

Here's a short demonstration of what the unary plus will do to a Character variable:以下是一元加号对 Character 变量的作用的简短演示:

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.它是如何工作的:一元 + 或 - 取消装箱他们的操作数,如果它是一个包装对象,然后将他们的操作数提升为 int,如果还不是一个 int 或更宽。 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 .因此,正如我们所看到的,虽然第一次调用method将选择char重载(仅取消装箱),但第二次调用将选择int版本的method Variable ch of type Character will be passed into method as int argument because of the applied unary plus .由于应用了一元加号Character类型的变量ch将作为int参数传递给method

我不知道,但我怀疑它与(显然必要的)一元减运算符对称。

Java 的设计目标之一是熟悉(对于 C/C++ 开发人员),因此当涉及到这样的运算符时,我很确定他们必须有充分的理由将其排除在外,而不是需要它的充分理由.

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:一元运算符对 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将值增加 1
  • -- Decrement operator; --递减运算符; decrements a value by 1将值减 1
  • ! Logical complement operator;逻辑补运算符; inverts the value of a boolean反转布尔值

Source: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/opsummary.html来源: https : //docs.oracle.com/javase/tutorial/java/nutsandbolts/opsummary.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM