+运算符对Java中的数字和字符串的行为有何不同? [英] How does + operator behaves differently with numbers and strings in Java?

查看:138
本文介绍了+运算符对Java中的数字和字符串的行为有何不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java没有运算符重载的概念。

Java does not have concept of operator overloading.

Still +运算符表现为带数字的加法运算符,并将运算符与字符串连接起来。这类似于运算符重载行为。

Still + operator behaves as addition operator with numbers and concatenate operator with strings. This is similar to the operator overloading behavior.

那么,Java是否有运算符重载?

So, does Java have operator overloading?

推荐答案

是否Java语言超载一些运营商?



是! 正如您所知,运营商 + 可以表示两种不同的东西,字符串连接或数字加法。这是按照定义,运算符重载。

Does the Java language overload some operators?

YES! As you've found out, the operator + can mean two different things, string concatenation or numeric addition. This is, by definition, an operator overload.

以下是所有 Java运算符的列表:

Here's the list of all Java operators:


JLS 3.12运算符



以下37个标记是运算符,由ASCII字符组成:

JLS 3.12 Operators

The following 37 tokens are the operators, formed from ASCII characters:

  =     >     <     !     ~     ?      :
  ==    <=    >=    !=    &&    ||     ++     --
  +     -     *     /     &     |      ^      %     <<     >>    >>>
  +=    -=    *=    /=    &=    |=     ^=     %=    <<=    >>=   >>>=


其中一些运营商超载。以下是一些示例:

Some of those operators are overloaded. Here are some examples:

System.out.println(   3 + 4 + "X"     ); // prints "7X"
System.out.println(   3 + (4 + "X")   ); // prints "34X"
System.out.println(   "X" + 3 + 4     ); // prints "X34"
System.out.println(   "X" + (3 + 4)   ); // prints "X7"

System.out.println(0 == 0);                           // prints "true"
System.out.println(new Integer(0) == new Integer(0)); // prints "false"

System.out.println(true & (1 & 2) == 12); // prints "false"






我们可以重载用Java语言定义的运算符?



绝对不是! 所有Java运算符都意味着完全由语言规范指定。没有语言外的语义:Java运营商可以 从不 执行语言未指定的操作。


Can we overload the operators defined in the Java language?

ABSOLUTELY NOT! All Java operators mean exactly as specified by the language specification. There is no "extra-linguistic" semantics: a Java operator can NEVER do something that isn't specified by the language.

也就是说,除非语言改变,否则以下是保证真理:

That is, unless the language changes, the following are guaranteed truths:


  • someString + 总是 字符串连接

  • referenceType == anotherReferenceType 始终 引用相等

  • 没有像$ code> 3这样的时髦的东西*一位女士或我的心/ 2 甚至 10 ** 3~ = 999

  • someString + whatever is ALWAYS string concatenation
  • referenceType == anotherReferenceType is ALWAYS reference equality
  • No funky things like 3 * "a lady" or "my heart" / 2 or even 10**3 ~= 999

正如上面的代码片段所示,即使是当前的运算符重载状态仍然会让人感到困惑,尤其是初学者。通过不允许语言外过载,至少这种混淆是有限的:一旦程序员了解了Java语言中的操作符 all 在各种重载场景中做了什么,它们在 all <中的确切语义/ em> Java代码变得清晰和精确。

As the above snippet shows, however, even the current state of operator overloading can still be quite confusing, especially for beginners. By not allowing extra-linguistic overloads, at least this confusion is limited: once a programmer learns about what all the operators in the Java language do in various overloaded scenarios, their exact semantics in all Java code becomes clear and precise.

运算符重载可能非常混乱。有些人认为它已经糟糕了。允许用户重载Java操作符以执行语言规范之外的操作只会导致更多的混淆。

Operator overloading can be quite confusing. Some think that it's "bad" enough as it is. To allow users to overload the Java operators to do something outside the language specification can only lead to even more confusion.

这是 Java Puzzlers ,Puzzle 30:Looper之子

Here's an excerpt from Java Puzzlers, Puzzle 30: Son of Looper:


语言设计师的课程与[另外两个谜题]相同。运算符重载可能会令人困惑。也许 + 运算符不应该因字符串连接而被重载。它可能值得提供字符串连接运算符,但它不必是 +

The lesson for language designers is the same as [two other puzzles]. Operator overloading can be confusing. Perhaps the + operator should not have been overloaded for string concatenation. It may well be worth providing a string concatenation operator, but it doesn't have to be +.






您是否需要C ++来支持Java中的运算符重载?



NOPE! 这与完全无关。 Java编译器需要做的就是根据语言的语法规则解析程序源代码,并为每个操作符确定操作数的类型。这些信息足以推断出运营商的意义,然后根据语言的规定采取相应的行动。


Do you need C++ to support operator overloading in Java?

NOPE! This has nothing to do with it at all. All that the Java compiler needs to do is parse the program source code according to the grammatical rules of the language, and determine, for each operator, what the types of the operands are. This information is enough to deduce what the meaning of the operator is, and to then act accordingly as specified by the language.

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆