
단항 연산자 : 피연산자가 하나 존재한다. EX) + x / - x / ! x
이항 연산자 : 피연산자가 두 개 존재한다. EX) x = y / x < y / x != y
삼항 연산자 : 피연산자가 세 개 존재한다. EX) condition : true ? false
대입 연산자는 오른쪽의 결과를 왼쪽에 대입한다.
=는 수학에서 ‘오른쪽 값과 왼쪽 값이 같다’라는 의미이지만, 프로그래밍에서는 ‘오른쪽 값을 왼쪽에 대입’하는 의미로 쓰인다.
- 프로그래밍에서 ‘오른쪽 값과 왼쪽 값이 같다’의 의미는
==이다.
int x = 10;
int y = 20;
System.out.println(“x = ” + x); // x = 10
System.out.println(“y = ” + y); // y = 20
x = y;
System.out.println(“x = ” + x); // x = 20
System.out.println(“y = ” + y); // y = 20
연산자 + 덧셈 - 뺄셈 * 곱셈 / 나눗셈 (몫) % 나눗셈 (나머지)
int x = 10;
int y = 20;
System.out.println(“x + y = ” + (x + y)); // x + y = 30
System.out.println(“x - y = ” + (x - y)); // x - y = -10
System.out.println(“x * y = ” + (x * y)); // x * y = 200
System.out.println(“x / y = ” + (x / y)); // x / y = 0
System.out.println(“x % y = ” + (x % y)); // x % y = 10
복합 대입 연산자는 산술 연산자와 대입 연산자를 결합한 연산자이다.
연산자 += 덧셈 → 대입 -= 뺄셈 → 대입 *= 곱셈 → 대입 /= 나눗셈 → 몫을 대입 %= 나눗셈 → 나머지를 대입
int x = 10;
System.out.pirntln(“x += 10 : ” + (x += 10)); // x += 10 : 20
int x = 10;
System.out.pirntln(“x -= 10 : ” + (x -= 10)); // x -= 10 : 0
int x = 10;
System.out.pirntln(“x *= 10 : ” + (x *= 10)); // x *= 10 : 100
int x = 10;
System.out.pirntln(“x /= 10 : ” + (x /= 10)); // x /= 10 : 1
int x = 10;
System.out.pirntln(“x += 10 : ” + (x += 10)); // x += 10 : 0
관계 연산자는 두 개의 피연산자를 비교해서 참/거짓의 결론을 돌출한다.
| 연산자 | ||
|---|---|---|
| > | a > b | a가 b보다 크면 참 |
| < | a < b | a가 b보다 작으면 참 |
| >= | a >= b | a가 b보다 크거나 같으면 참 |
| <= | a <= b | a가 b보다 작거나 같으면 참 |
| == | a == b | a와 b가 같으면 참 |
| != | a != b | a와 b가 같지 않으면 참 |
int x = 10;
int y = 20;
System.out.println(“x > y : ” + (x > y)); // x > y : false
System.out.println(“x < y : ” + (x < y)); // x < y : true
System.out.println(“x >= y : ” + (x >= y)); // x >= y : false
System.out.println(“x <= y : ” + (x <= y)); // x <= y : true
System.out.println(“x == y : ” + (x == y)); // x== y : false
System.out.println(“x != y : ” + (x != y)); // x != y : true
1만큼의 증가나 감소를 수행한다.
증감 연산자의 위치에 따라 피연산자의 출력값이 달라진다.
연산자 ++ ++a 1만큼 증가 -> 출력 -- --a 1만큼 감소 -> 출력 ++ a++ 출력 -> 1만큼 증가 -- a-- 출력 -> 1만큼 감소
int x = 10;
System.out.println(“++x : ” + (++x)); // ++x : 11
int x = 10;
System.out.println(“--x : ” + (--x)); // --x : 9
int x = 10;
System.out.println(“x++ : ” + (x++)); // x++ : 10
System.out.println(“x : ” + x); // x : 11
int x = 10;
System.out.println(“x-- : ” + (x--)); // x-- : 10
System.out.println(“x : ” + x); // x : 9
연산자 && a && b 논리곱 (AND) : a와 b 모두 참이면 참 || a || b 논리합 (OR) : a와 b 중 하나라도 참이면 참 ! !a 논리부정 (NOT) : a의 상태를 부정
boolean a = false;
boolean b = true;
System.out.println("a && b : " + (a && b)); // a && b : false
System.out.println("a || b : " + (a || b)); // a || b : true
System.out.println("!a : " + !a); // !a : true
System.out.println("!b : " + !b); // !b : false
조건식의 두 개의 피연산자 연산 결과에 따라서 나머지 피연산자가 결정된다.
- 조건식 ? 식1 : 식2
→ 조건식이 참이면 식1이 실행되고, 조건식이 거짓이면 식2가 실행된다.
int x = 10;
int y = 20;
int result = 0;
result = (x > y) ? 100 : 200;
System.out.println("result : " + result); // result : 200
result = (x < y) ? 100 : 200;
System.out.println("result : " + result); // result : 100
result = (x == y) ? 100 : 200;
System.out.println("result : " + result); // result : 200
데이터를 비트(bit) 단위로 환산하여 연산을 수행하며, 다른 연산자보다 연산 속도가 향상된다.
연산자 & a & b AND 연산 : a와 b 모두 1이면 1 | a | b OR 연산 : a와 b 중 하나라도 1이면 1 ^ a ^ b XOR 연산 : a와 b가 같지 않으면 1
int x = 2; // 0 0 1 0
int y = 3; // 0 0 1 1
System.out.println("x & y : " + (x & y)); // x & y : 2
System.out.println("x | y : " + (x | y)); // x | y : 3
System.out.println("x ^ y : " + (x ^ y)); // x ^ y : 1