Post 和预增量运算符 OCJA-1.8
Post and Pre Increment Operator OCJA-1.8
我正在练习 java post 和预递增运算符,我对理解下面程序的输出感到困惑。它如何将输出生成为“8”?
public class Test{
public static void main(String [] args){
int x=0;
x=++x + x++ + x++ + ++x;
System.out.println(x);
}
}
我尝试了更多示例程序,我可以在其中跟踪输出
public class Test{
public static void main(String [] args){
int x=0;
x=++x + ++x + ++x + x++;
// 1 + 2 + 3 + 3 =>9
System.out.println(x);
}
}
这可以说与以下内容相同:
public static void main(String[] args) {
int x=0;
int t1 = ++x;
System.out.println(t1);//t1 = 1 and x = 1
int t2 = x++;
System.out.println(t2);//t2 = 1 and x = 2
int t3 = x++;
System.out.println(t3);//t3 = 2 and x = 3
int t4 = ++x;
System.out.println(t4);//t4 = 4 and x = 4
x= t1 + t2 + t3 + t4;//x = 1 + 1 + 2 + 4
System.out.println(x);//8
}
这可能有助于理解预运算符和 post 运算符的行为。
public class Test{
public static void main(String [] args){
int x=0;
x = ++x + x++ + x++ + ++ x;
//0 = (+1+0) + (1) + (+1 +1) + (+1 +1 +2);
//0 = 1 + 1 + 2 + 4
System.out.println(x); // prints 8.
}
}
我正在练习 java post 和预递增运算符,我对理解下面程序的输出感到困惑。它如何将输出生成为“8”?
public class Test{
public static void main(String [] args){
int x=0;
x=++x + x++ + x++ + ++x;
System.out.println(x);
}
}
我尝试了更多示例程序,我可以在其中跟踪输出
public class Test{
public static void main(String [] args){
int x=0;
x=++x + ++x + ++x + x++;
// 1 + 2 + 3 + 3 =>9
System.out.println(x);
}
}
这可以说与以下内容相同:
public static void main(String[] args) {
int x=0;
int t1 = ++x;
System.out.println(t1);//t1 = 1 and x = 1
int t2 = x++;
System.out.println(t2);//t2 = 1 and x = 2
int t3 = x++;
System.out.println(t3);//t3 = 2 and x = 3
int t4 = ++x;
System.out.println(t4);//t4 = 4 and x = 4
x= t1 + t2 + t3 + t4;//x = 1 + 1 + 2 + 4
System.out.println(x);//8
}
这可能有助于理解预运算符和 post 运算符的行为。
public class Test{
public static void main(String [] args){
int x=0;
x = ++x + x++ + x++ + ++ x;
//0 = (+1+0) + (1) + (+1 +1) + (+1 +1 +2);
//0 = 1 + 1 + 2 + 4
System.out.println(x); // prints 8.
}
}