运算符和方法有什么区别?
Java中运算符和方法的求值规则有什么区别?
What is the difference in the evaluation rule of an operator and a method in Java?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
Java中运算符和方法的求值规则有什么区别?
What is the difference in the evaluation rule of an operator and a method in Java?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(4)
运算符可以被视为语法糖,只是
写成
right + left
比写Class.Plus(right, left)
更方便。Operators can be seen as syntactic sugar for
It's just more convinient to write
right + left
instead ofClass.Plus(right, left)
.Femaref 的回答非常好;我想对此进行扩展。
运算符(通常)硬连接到语言中:像
+
、-
、*
和/
之类的东西通常由编译器直接翻译成机器语言(如果这是该语言系统的底层机制),而不需要显式调用库中的方法。例如,这就是 C 语言中的情况。如果语言中未定义这些运算符,则必须编写plus(2,2)
而不是2 + 2
。该语言中定义的运算符具有内置优先级的优点。
*
和/
通常比+
和-
具有更高的优先级,因此可以写成3 * 3 + 4 * 4
并得到 25,而不是没有这种优先级或不同优先级时得到的 52 或 84。当编译器识别运算符但仍委托给库时,该线会变得有点灰色。 FORTRAN 中的复数就是一个例子:编译器通常不会直接将复杂运算编译成机器代码,而是生成机器代码来调用库。
大多数人只想到算术运算符,例如
+
、-
等。但是,例如,您也可以将方括号 ([ ]
) 视为数组索引的运算符。有些语言允许您重载其运算符,因此您可以用对方法的调用来代替运算符通常执行的操作。
大多数语言不允许您定义自己的运算符来支持与内置运算符相同的机制。 Scala 是一个例外;如果您愿意,您可以定义一个 +++ 运算符并将其连接到您提供的方法。有些人认为运算符重载会使代码更难理解,因此对于这是否是一个好主意尚无定论。
Femaref's answer is pretty good; I'd like to expand on it.
Operators are (usually) hard-wired into the language: Stuff like
+
,-
,*
and/
are usually directly translated by the compiler into machine language (if that's the underlying mechanism for that language system) without the need to explicitly call a method in a library. This is how it is/was in C, for instance. If those operators weren't defined in the language, you'd have to codeplus(2,2)
instead of2 + 2
.Operators defined in the language come with the benefit of built-in priority.
*
and/
usually have higher priority than+
and-
, so you can write3 * 3 + 4 * 4
and get 25, not 52 or 84 that you'd get without such priorization or different priorities.The line becomes a little grey when operators are recognized by the compiler but still delegated to a library. Complex numbers in FORTRAN may be an example: Often the compiler will not bother to directly compile complex operations into machine code but will generate machine code to call a library.
Most people think only of the arithmetic operators, like
+
,-
and so forth. But you could also consider square brackets ([ ]
) an operator for array indexing, for example.Some languages let you overload their operators, so you can substitute a call to a method for whatever the operator normally does.
Most languages don't let you define your own operators that support the same mechanisms as the built-in ones. Scala is an exception here; you can define a
+++
operator if you like and have it hook up to a method you provide. Some people think operator overloading makes code harder to understand, so the jury is still out about whether this is a good idea.在 C 和 Java 中,您无法覆盖类的“+”运算符,因此您的问题对于其他语言来说是有意义的。
有些语言可以覆盖运算符,例如 C++ 和 Python。如果您对这样的比较感兴趣为什么不尝试一下呢?即使您的测试表明“+”运算符比 .add() 方法更慢/更快,如果您更改编译器或环境,这可能会改变。
我认为你应该使用看起来适合你的东西。
Java 中建议使用
StringBuffer
而不是+
运算符与String
一起使用,但最新的 Java 实现对+
进行了更好的优化> 运算符,所以这个建议对于新代码来说不太好。In both C and Java you cannot override '+' operator for your classes so your question makes sense for other languages.
There are languages where operators can be overriden such as C++ and Python. If you are interested in such comparison why not to try? Even of your tests shows that '+' operator is slower/faster than .add() method this can change if you change compiler or environment.
I think you should use something that looks good for you.
There was advices in Java to use
StringBuffer
instead of+
operator withString
but newest Java implementations make better optimalization for+
operator, so this advice is not that good for new code.我会对你的问题做出不同的猜测。例如,您可能会问,Java 对以下两种情况下
bar()
和baz()
的执行顺序有何保证:在这两种情况下,< code>bar() 肯定在
baz()
之前调用 (JLS 15.7.1,4)I'll take a different guess at what you're asking. You might be asking, for instance, what Java guarantees about the order in which
bar()
andbaz()
are executed in the following two cases:In both cases,
bar()
is definitely called beforebaz()
(JLS 15.7.1,4)