for循环j = j++ 和 j = ++j

时间:2021-04-26 23:20:09

package com.test.forname;

public class TestForName {
public static void main(String[] args) throws Exception{

/* A a = (A) Class.forName("com.test.a.A").newInstance();
Class<?> c = Class.forName("com.test.c.C");
System.out.println(a.getClass());
System.out.println(c.getClasses());
System.out.println(c.getClassLoader());*/

int j = 0;
for(int i=0;i<100;i++){
j = j++;
}
System.out.print(j);

}

运行结果:0


package com.test.forname;

import com.test.a.A;
import com.test.c.C;

public class TestForName {
public static void main(String[] args) throws Exception{

/* A a = (A) Class.forName("com.test.a.A").newInstance();
Class<?> c = Class.forName("com.test.c.C");
System.out.println(a.getClass());
System.out.println(c.getClasses());
System.out.println(c.getClassLoader());*/

int j = 0;

j = j++  Java用了中间缓存变量的机制,等同于

temp = j;

j = j + 1;

j = temp;

for(int i=0;i<100;i++){

j = ++j;

}
System.out.println(j);

}

}

输出结果:100


int j = 0,k = 0;
for(int i=0;i<3;i++){
j = j++;

k = j++ + ++j;

if(i == 2){
System.out.println("j-"+j++);
}
}
System.out.println("j---"+j);
System.out.println("k---"+k);
}

输出结果:

j-6
j---7
k---10