+, - 와 같은 기호int x = 5;
int y = 10;
int z = x + y; // 5(피연산자) +(연산자) 10(피연산자) 계산
System.out.println(z); // 출력값 : 15
산술 연산자
- + ,-,*,/,%,>>,<<
System.out.println(2 + 2 * 2);
System.out.println((2 + 2) * 2); // 8
비교 연산자
- > ,<=,>,>=,==,!=
- 크거나 작거나 같거나 다르거나를 비교해서 boolean(true/false) 값을 반환한다.
- 예시
System.out.println(10 > 9);// true
System.out.println(10 != 9);// true
System.out.println(10 == 9); // false
논리 연산자
- &&,||,!
- 비교 연산의 결과값으로 받을 수 있는 boolean 값을 연결하는 연산자
- 조건을 연결 했을 때 boolean 값들을 조합하여 참(true) 또는 거짓(false) 값인 boolean 값을 출력
예시
boolean flag1 = true;
boolean flag2 = false;
boolean flag3 = true;
// And -> 둘 다 true면 true다
System.out.println(flag1 && flag2);
System.out.println(flag1 && flag2 && flag3);
System.out.println((5 > 3) && (3 > 1));
System.out.println((5 > 3) && (3 < 1)); // true && false -> false
System.out.println("---------------------------------------");
// or -> 둘 중 하나가 true면 true다
System.out.println(flag1 || flag2);
System.out.println(flag1 || flag2 || flag3); // true || false || true -> true
System.out.println((5 > 3) || (3 > 1)); // true || true -> true
System.out.println((5 > 3) || (3 < 1)); // true || false -> true
System.out.println((5 < 3) || (3 < 1)); // false || false -> false
System.out.println("---------------------------------------");
// not -> true면 false -> boolean 값 반전
System.out.println(!flag1);
System.out.println(!(5 == 5));
대입 연산자
- 기본 연산자 = , 복합 대입 연산자+=,-=,*= 와 증감 연산자 등이 있다.
- 변수를 바로 연산해서 그자리에서 자장하는(대입하는) 연산자
- 예시
// 대입 연산자
int number = 10;
int number2 = 10 + 2;
System.out.println(number2);
System.out.println("-------------------------------------");
// 복합 대입 연산자
System.out.println(number += 2);
System.out.println(number -= 2);
System.out.println(number *= 2);
System.out.println("-------------------------------------");
증감 연산자(전위 vs 후위)
++: 1을 더하는 것과 같은 의미--: 1을 빼는 것과 같은 의미- 전위 vs 후위
- 앞에 있는 경우 : 먼저 바뀐 값이 대입 된 이후 연산이 진행된다.
- 뒤에 있는 경우 : 연산 진행 후, 바뀐값이 된다.
- 예시
// 주의해야할 점 int a = 10; int b = 10; // 앞에 있는 경우 : 연산 진행 이전에 , 바뀐값이 된다. // 뒤에 있는 경우 : 연산 진행 후, 바뀐값이 된다 (b값에 -1 ) int val = ++a + b--; System.out.println(val); // 21 System.out.println(b);
형 변환 연산자
- 괄호 안에 반환할 타입을 넣으면 피연산자의 타입이 변경
- 예시
int intNum = 93 + (int)88.6;
System.out.println(intNum);
삼항 연산자
- 비교연산자와 항상 함께 쓰인다.
- 형태: (조건) ? 참 : 거짓
- (비교 연산자의 결과) : true or false
- 예시
int x = 1;
int y = 10;
boolean b = (x==y) ? true : false;
System.out.println(b);
String s = (x != y) ? "정답" : "오답";
System.out.println(s);
int max = (x > y) ? x : y;
System.out.println(max);
int min = (x < y) ? x : y;
System.out.println(min);
instance of 연산자
- 피연산자가 조건에 명시된 클래스의 객체인지 비교하여 참/거짓을 응답해주는 연산자
int x = 2;
int y = 9;
int z = 10;
boolean result = x < y && y < z; // true && true -> true
System.out.println(result);
result = x + 10 < y && y < z; // false && true -> false
System.out.println(result);
result = (x + 2) * 3 > y;
System.out.println(result);
>> , << // number << x 는 Number * 2 ^x 값이고, Number >> x 는 (int) Number / 2 ^ x 이다 .
System.out.println(3 << 2);
System.out.println(3 << 1);