Java中的a + = a ++ * a ++ * a ++。如何评估? [英] a += a++ * a++ * a++ in Java. How does it get evaluated?

查看:164
本文介绍了Java中的a + = a ++ * a ++ * a ++。如何评估?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在在本网站中遇到过这个问题,并在Eclipse中尝试了但是却无法了解它们的评估方式。

I came across this problem in this website, and tried it in Eclipse but couldn't understand how exactly they are evaluated.

    int x = 3, y = 7, z = 4;

    x += x++ * x++ * x++;  // gives x = 63
    System.out.println(x);

    y = y * y++;
    System.out.println(y); // gives y = 49

    z = z++ + z;
    System.out.println(z);  // gives z = 9

根据网站上的评论,x + = x ++ * x ++ * x ++解析为x = x +((x + 2)*(x + 1)* x),结果为真。我想我错过了关于这个运算符优先级的东西。

According to a comment in the website, x += x++ * x++ * x++ resolves to x = x+((x+2)*(x+1)*x) which turns out to be true. I think I am missing something about this operator precedence.

推荐答案

Java从左到右评估表达式&根据它们的优先顺序。

Java evaluates expressions left to right & according to their precedence.

int x = 3, y = 7, z = 4;

x (3) += x++ (3) * x++ (4) * x++ (5);  // gives x = 63
System.out.println(x);

y = y (7) * y++ (7);
System.out.println(y); // gives y = 49

z = z++ (4) + z (5);
System.out.println(z);  // gives z = 9

后缀增量运算符仅在使用/返回变量后递增变量。一切似乎都是正确的。

Postfix increment operator only increments the variable after the variable is used/returned. All seems correct.

这是后缀增量运算符的伪代码:

This is pseudocode for the postfix increment operator:

int x = 5;
int temp = x;
x += 1;
return temp;

来自JLS 15.14.2(参考):

From JLS 15.14.2 (reference):


后缀增量表达式的值是存储新值之前变量的值。

The value of the postfix increment expression is the value of the variable before the new value is stored.

这篇关于Java中的a + = a ++ * a ++ * a ++。如何评估?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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