(공감과 댓글 하나는 글쓴이에게 큰 힘이 됩니다.)
연산에 사용하는 기호를 연산자(Operator)라고 하며, 연산에 사용하는 값을 항(Operand)이라고 한다. 예를 들어 1 + 2에서 1과 2는 항이고 +는 연산자이다.
연산자 |
기능 |
예 |
단항 연산자 |
항이 한 개인 연산자 |
++num |
이항 연산자 |
항이 두 개인 연산자 |
num1 + num2 |
삼항 연산자 |
항이 세 개인 연산자 |
(num1 > num2) ? 1 : 0 |
■ 대입 연산자
- 오른쪽에 있는 값이나 수식을 계산하여 왼쪽에 있는 변수에 넣어주는 연산자이다.
|
public class Main {
public static void main(String[] args) {
int x = 10, y = 20;
int z = x + y;
System.out.println("x + y = " + z); // x + y = 30
}
}
|
cs |
■ 부호 연산자
- 양수와 음수를 나타내는 연산자이다.
연산자 |
기능 |
예 |
+ |
변수나 상수의 값을 양수로 만듦 |
+1 |
- |
변수나 상수의 값을 음수로 만듦 |
-1 |
|
public class Main {
public static void main(String[] args) {
int x = +10, y = -5;
System.out.println("x = " + x); // x = 10
System.out.println("y = " + y); // y = -5
}
}
|
cs |
■ 산술 연산자
- 덧셈, 뺄셈, 곱셈, 나눗셈, 나머지 연산을 할 때 사용되는 연산자이다.
연산자 |
기능 |
예 |
+ |
덧셈 |
x + y |
- |
뺄셈 |
x - y |
* |
곱셈 |
x * y |
/ |
나눗셈 |
x / y |
% |
나머지 |
x % y |
|
public class Main {
public static void main(String[] args) {
int x = 3, y = 2;
System.out.println("x + y = " + (x + y)); // x + y = 5
System.out.println("x - y = " + (x - y)); // x - y = 1
System.out.println("x * y = " + (x * y)); // x * y = 6
System.out.println("x / y = " + (x / y)); // x / y = 1
System.out.println("x % y = " + (x % y)); // x % y = 1
}
}
|
cs |
■ 증감 연산자
- 후위 증감 연산자 : 증감 연산자를 사용한 줄에서는 증감하지 않은 값을 사용하고, 해당 줄을 넘어가면 증감시킨다.
- 전위 증감 연산자 : 증감 연산자를 사용한 줄에서부터 증감시킨다.
연산자 |
기능 |
예 |
++ |
항의 값을 1 증가시킨다. |
num++ ++num |
-- |
항의 값을 1 감소시킨다. |
num-- --num |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
public class Main {
public static void main(String[] args) {
int x = 10;
System.out.println("x++ = " + x++); // x++ = 10
System.out.println("x = " + x); // x = 11
System.out.println("++x = " + ++x); // ++x = 12
System.out.println("x = " + x); // x = 12
System.out.println("x-- = " + x--); // x-- = 12
System.out.println("x = " + x); // x = 11
System.out.println("--x = " + --x); // --x = 10
System.out.println("x = " + x); // x = 10
}
}
|
cs |
■ 관계 연산자
- 두 식의 대소관계를 비교하는 연산자로서, 결과를 참(true)이나 거짓(false)으로 표현한다.
연산자 |
기능 |
예 |
> |
왼쪽 항이 크면 참, 아니면 거짓 반환 |
x > y |
< |
왼쪽 항이 작으면 참, 아니면 거짓 반환 |
x < y |
>= |
왼쪽 항이 크거나 같으면 참, 아니면 거짓 반환 |
x >= y |
<= |
왼쪽 항이 작거나 같으면 참, 아니면 거짓 반환 |
x <= y |
== |
두 항이 같으면 참, 아니면 거짓 반환 |
x == y |
!= |
두 항이 다르면 참, 아니면 거짓 반환 |
x != y |
1
2
3
4
5
6
7
8
9
10
11
12
|
public class Main {
public static void main(String[] args) {
int x = 3, y = 5;
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
}
}
|
cs |
■ 논리 연산자
- 논리 곱(AND), 논리 합(OR), 논리 부정(NOT)의 연산을 할 때 사용된다.
연산자 |
기능 |
예 |
&& |
두 항이 모두 참인 경우에만 참 |
x && y |
|| |
두 항 중 하나의 항이라도 참이면 참 |
x || y |
! |
참인 경우 거짓, 거짓인 경우 참으로 바꾼다. |
!x |
|
public class Main {
public static void main(String[] args) {
boolean x = true, y = false;
System.out.println("x && y = " + (x && y)); // x && y = false
System.out.println("x || y = " + (x || y)); // x || y = true
System.out.println("!x = " + (!x)); // !x = false
}
}
|
cs |
■ 복합 대입 연산자
- 대입 연산자와 다른 연산자를 조합해 하나의 연산자로 사용할 수 있다.
연산자 |
기능 |
예 |
+= |
a = a + 2와 동일 |
a += 2 |
-= |
a = a - 2와 동일 |
a -= 2 |
*= |
a = a * 2와 동일 |
a *= 2 |
/= |
a = a / 2와 동일 |
a /= 2 |
%= |
a = a % 2와 동일 |
a %= 2 |
<<= |
a = a << 2와 동일 |
a <<= 2 |
>>= |
a = a >> 2와 동일 |
a >>= 2 |
>>>= |
2는 a = a >>> 2와 동일 |
a >>>= 2 |
&= |
a = a & 2와 동일 |
a &= 2 |
|= |
a = a | 2와 동일 |
a |= 2 |
^= |
a = a ^ 2와 동일 |
a ^= 2 |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
public class Main {
public static void main(String[] args) {
int a = 7;
System.out.println("a += 2 : " + (a += 2)); // a += 2 : 9
System.out.println("a -= 2 : " + (a -= 2)); // a -= 2 : 7
System.out.println("a *= 2 : " + (a *= 2)); // a *= 2 : 14
System.out.println("a /= 2 : " + (a /= 2)); // a /= 2 : 7
System.out.println("a %= 2 : " + (a %= 2)); // a %= 2 : 1
System.out.println("a <<= 2 : " + (a <<= 2)); // a <<= 2 : 4
System.out.println("a >>= 2 : " + (a >>= 2)); // a >>= 2 : 1
System.out.println("a >>>= 2 : " + (a >>>= 2)); // a >>>= 2 : 0
System.out.println("a &= 2 : " + (a &= 2)); // a &= 2 : 0
System.out.println("a |= 2 : " + (a |= 2)); // a |= 2 : 2
System.out.println("a &= 2 : " + (a &= 2)); // a &= 2 : 2
}
}
|
cs |
■ 조건 연산자
- 조건 연산자는 삼항 연산자라고도 하며, 조건식이 참인 경우와 거짓인 경우 각각 다른 값이 나온다.
연산자 |
기능 |
예 |
조건식 ? 식1 : 식2 |
조건식이 참이면 식1, 거짓이면 식2 수행 |
(num % 2 == 0) ? "짝수" : "홀수" |
|
public class Main {
public static void main(String[] args) {
int num1 = 10, num2 = 11;
System.out.println("num1 : " + ((num1 % 2 == 0) ? "짝수" : "홀수")); // num1 : 짝수
System.out.println("num2 : " + ((num2 % 2 == 0) ? "짝수" : "홀수")); // num2 : 홀수
}
}
|
cs |