선행연산
int j = 1;
System.out.println(++j);
j = j + 1 먼저 수행 후
System.out.println(j) 수행
후행연산
int k = 1;
System.out.println(k++);
System.out.println(k) 먼저 수행 후
k = k + 1 수행
예제
public class Ex4 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int i = 0;
++i;
System.out.println(i);
++i;
System.out.println(i);
++i;
System.out.println(i);
int j = 1;
System.out.println(++j); // 2 (j = j + 1)
int k = 1;
System.out.println(k++); // 1
System.out.println(k); // 2
}
}
public class Ex5 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int i = 10;
--i;
System.out.println(i); // 9
--i;
System.out.println(i); // 8
--i;
System.out.println(i); // 7
System.out.println(--i); // 6
System.out.println(i--); // 6
System.out.println(i); // 5
System.out.println("=========================");
i = 10;
int j = 10;
int z = ++i + j++;
System.out.println("z = " + z); // 21
System.out.println("i = " + i); // 11
System.out.println("j = " + j); // 11
}
}