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

发布于 2024-08-28 18:22:53 字数 485 浏览 11 评论 0 原文

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

つ低調成傷 2024-09-04 18:22:53

当操作数的类型为 bytecharshort 时,一元加运算符会自动转换为 int >。这称为一元数字提升,它使您能够执行以下操作:

char c = 'c';
int i = +c;

当然,它的用途有限。但它确实有一个目的。请参阅规范,特别是 §15.15.3§5.6.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.

无戏配角 2024-09-04 18:22:53

下面是一元加号对 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);        
}

运行该程序的输出是:

char: X
int: 88

工作原理:一元 + 或 - 取消装箱其操作数,如果它是包装对象,则将它们的操作数提升为 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:

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.

明媚如初 2024-09-04 18:22:53

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

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

娇柔作态 2024-09-04 18:22:53

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.

酒废 2024-09-04 18:22:53

我的猜测是它在那里,因为有时输入加号会让事情变得更清楚。您可能想强调以下事实:某些数字是正数,而不是某些负数。

此外,为了提供一个真实世界的使用示例,在世界某些地区,正温度往往总是带有加号前缀。

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.

醉殇 2024-09-04 18:22:53

许多其他语言都有一元加号。人们习惯将其包括在内,而吝啬地将其排除。它在编译器中只有几行。

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.

ヅ她的身影、若隐若现 2024-09-04 18:22:53

一元运算符对 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 boolean

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文