具有两个连续运算符的 Java 数学

发布于 2024-12-16 19:27:50 字数 409 浏览 0 评论 0原文

我最近正在调试一个问题,结果比预期大得多。我本来打算写的是:

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 技术交流群。

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

发布评论

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

评论(4

美煞众生 2024-12-23 19:27:50

它使用一元加运算符 - 所以它是这样的:

y += height * (+rowHeight) * 2;

举一个看起来一点也不奇怪的例子,但使用一元而不是一元加:

y += height * -rowHeight * 2;

希望更有意义:)

It's using the unary plus operator - so it's like this:

y += height * (+rowHeight) * 2;

To give an example which wouldn't look odd at all, but using unary minus instead of unary plus:

y += height * -rowHeight * 2;

Hopefully that makes more sense :)

谈场末日恋爱 2024-12-23 19:27:50

+ 这里只是一个正号(一元运算符)。你可以这样重写:

y += height * (+ rowHeight) * 2;

在java中这样写也是有效的:

int x = +-+-+-+- 5;

+ is just a positive sign here (a unary operator). You can re-write it like:

y += height * (+ rowHeight) * 2;

It is also valid to write this in java:

int x = +-+-+-+- 5;
于我来说 2024-12-23 19:27:50

编译器将表达式视为运算符
y += 高度 * (+ rowHeight) * 2;

The compiler is treating the expression as operator as
y += height * (+ rowHeight) * 2;

终难愈 2024-12-23 19:27:50

+- 也是一元运算符,否则你无法编写如下内容:

int n = -1;
int x = +42;

Both + and - are unary operators as well, otherwise you couldn't write things like:

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