具有两个连续运算符的 Java 数学
我最近正在调试一个问题,结果比预期大得多。我本来打算写的是:
y += height + rowHeight * 2;
我写的是
y += height * + rowHeight * 2;
我没有立即看到错误,因为显然 * +
是 Java 中的有效运算符,或者至少是 Android java 中的有效运算符。我以前从未听说过这件事,也不知道这意味着什么。
作为一个实验,我发现它的通用形式是,作为正则表达式 [*/%][+-]*
它看起来像某种形式的波兰表示法,但我不知道 Java 支持这样的。
那么...我在哪里可以找到有关该运算符的文档,它到底意味着什么?
I was recently debugging a problem where a result was much larger than expected. What I had intended to write was:
y += height + rowHeight * 2;
What I had written was
y += height * + rowHeight * 2;
I didn't see the error right away because, apparently * +
is a valid operator in Java, or at least Android java. I've never heard anything about this before, and I have no idea what it means.
As an experiment, I found that the generalized form of this is, as a regex [*/%][+-]*
It looks like some form of polish notation, but I was unaware that Java supported such.
So... where would I find documentation about this operator, and what, exactly, does it mean?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
它使用一元加运算符 - 所以它是这样的:
举一个看起来一点也不奇怪的例子,但使用一元减而不是一元加:
希望更有意义:)
It's using the unary plus operator - so it's like this:
To give an example which wouldn't look odd at all, but using unary minus instead of unary plus:
Hopefully that makes more sense :)
+
这里只是一个正号(一元运算符)。你可以这样重写:在java中这样写也是有效的:
+
is just a positive sign here (a unary operator). You can re-write it like:It is also valid to write this in java:
编译器将表达式视为运算符
y += 高度 * (+ rowHeight) * 2;
The compiler is treating the expression as operator as
y += height * (+ rowHeight) * 2;
+
和-
也是一元运算符,否则你无法编写如下内容:Both
+
and-
are unary operators as well, otherwise you couldn't write things like: