前缀和后缀“++”的语义。 Java中的运算符 [英] Semantics of pre- and postfix "++" operator in Java
本文介绍了前缀和后缀“++”的语义。 Java中的运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道为什么这段代码会给出输出 112
最后一位如何 2
正在创建?
I wondering to know why this snippet of code give output 112
How this last digit 2
was creating?
public static void main(String[] args) {
int i = 0;
System.out.print(++i);
System.out.print(i++);
System.out.print(i);
为什么会发生这种情况?
Why does this happen?
推荐答案
你的片段被翻译为
int i = 0;
i = i + 1; // 1
System.out.print(i); // 1
System.out.print(i); // 1
i = i + 1; // 2
System.out.print(i); // 2
这就是最终结果为2的原因。
That's why the final result it's a 2.
++ i
它在被print方法调用之前递增变量而 i ++
它正在递增方法执行后的变量。
++i
it's incrementing the variable before being called by the print method and i++
it's incrementing the variable after the method execution.
这篇关于前缀和后缀“++”的语义。 Java中的运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文