Skip to content

Instantly share code, notes, and snippets.

@jjangga0214
Created September 23, 2017 12:49
Show Gist options
  • Save jjangga0214/ffc80af30291a35f0f7b314c012f677d to your computer and use it in GitHub Desktop.
Save jjangga0214/ffc80af30291a35f0f7b314c012f677d to your computer and use it in GitHub Desktop.
자바에서 전위 연산자와 후위 연산자를 비교하기 위한 문서입니다.
public class PmOperator {
public static void main(String[] args) {
int x = 0;
int y = 0;
/*
* 후위 증감연산자
*/
System.out.println(x++);// 0이 그대로 출력된다. 출력된 후 x의 값은 1로 증가한다.
System.out.println(x);// 1이 출력된다. 바로 위 라인에서 이 라인으로 넘어오기 직전 1로 값이 증가했으므로 1이 출력된다.
System.out.println(x--);// 1이 출력된다. 이 라인에는 1을 출력하고 다음부터는 0으로 값이 세팅된다.
/*
* 전위 증감연산자
*/
System.out.println(--y);// -1이 출력된다. 출력문에 쓰이기 직전에 0에서 -1로 y의 값이 증가되어 세팅되는 것이다.
System.out.println(y);// -1이 출력된다. 윗 라인에서 y의 값이 -1로 세팅되었기 때문이다.
System.out.println(++y);// 0이 출력된다. 출력문에 쓰이기 직전에 -1에서 0로 y의 값이 증가되어 세팅되는 것이다.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment