- 연산자 : 연산에 사용되는 표시나 기호 ex) +, -, *, ==
연산자의 종류
- 산술 연산자 ex) +, -, *, /, %
- 증감 연산자 ex) ++, --
- 비교 연산자 ex) ==, !=
- 논리 연산자 ex) &&, ||
- 대입 연산자 ex) =, +=, -=
피연산자 : 연산식에서 연산되는 데이터 ex) x, y, z
연산식 : 연산자와 피연산자를 이용하여 연산의 과정을 기술한 것
=> 연산식은 반드시 하나의 값을 산출하고, 값 대신에 연산식을 사용할 수 있다.
- 연산의 방향과 우선순위
- 단항, 이항, 삼항 연산자 순으로 우선순위를 가진다.
- 산술, 비교, 논리, 대입 연산자 순으로 우선순위를 가진다.
- 단항, 부호, 대입 연산자를 제외한 모든 연산의 방향은 왼쪽에서 오른쪽이다.
- 복잡한 연산식에서는 괄호()를 사용하여 우선순위를 정한다.
- 연산자는 피연산자의 수에 따라 단항, 이항, 삼항 연산자로 구분한다.
ex) 단항 연산자 : ++x;
ex) 이항 연산자 : x + y;
ex) 삼항 연산자 : (sum > 90) ? "A" : "B";
부호 연산자(+, -) : 부호 연산자는 변수의 부호를 유지하거나 변경한다.
증감 연산자(++, --) : 변수의 값을 증가시키거나 감소 시키는 연산자
=> 증감 연산자가 변수 앞에 있으면 우선 변수값을 1 증가 또는 1감소시킨 후에 다른 연산자를 처리한다.
=> 증감 연산자가 변수 뒤에 있으면 다른 연산자를 먼저 처리한 후 변수 값을 1 증가 또는 1 감소한다.
public static void main(String[] args) {
int x = 10;
int y = 10;
int z;
System.out.println("=====================");
x++; // 11
++x; // 12
System.out.println("x=" + x); // 12
System.out.println("=====================");
y--; // 9
--y; // 8
System.out.println("y=" + y); // 8
System.out.println("=====================");
z = x++;
System.out.println("z =" + z); // 12
System.out.println("x =" + x); // 13
System.out.println("=====================");
z = ++x;
System.out.println("z =" + z); // 14
System.out.println("x =" + x); // 14
System.out.println("=====================");
z = ++x + y++; // 15 + 8
System.out.println("z=" + z); // 23
System.out.println("x=" + x); // 15
System.out.println("y=" + y); // 9
}
- 이항 연산자 : 피연산자가 2개인 연산자
산술 연산자
+
: 덧셈 연산-
: 뺄셈 연산*
: 곱셈 연산/
: 나눗셈 연산%
: 나머지를 구하는 연산
=> long타입을 제외한 정수 타입 연산은 int타입으로 산출되고, 피연산자 중 하나라도 실수 타입이면 실수 타입으로 산출된다.
public static void main(String[] args) {
char c1 = 'A' + 1; // 65 + 1
char c2 = 'A';
// char c3 = c2 + 1; // c3타입은 int가 되어야 한다
System.out.println("c1: " + c1); // B
System.out.println("c2: " + c2); // A
// System.out.println("c3: " + c3);
}
public static void main(String[] args) {
String str1 = "JDK" + 6.0;
String str2 = str1 + "특징";
System.out.println(str2); // JDK6.0특징
String str3 = "JDK" + 3 + 3.0;
String str4 = 3 + 3.0 + "JDK";
System.out.println(str3); // JDK33.0
System.out.println(str4); // 6.0JDK
}
비교 연산자(<, <=, >, >=, ==, !=) : 피연산자으 대소 또는 동등을 비교해 true/false를 산출
=> 흐름 제어문인 조건문(if), 반복문(for, while)에서 주로 이용되어 실행 흐름을 제어
=> <기본 타입 변수>의 값을 비교 할 떄에는 <==연산자>를 사용하지만 [참조 타입인 String 변수]를 비교할 때에는 [equals() 메서드]를 사용
논리 연산자(&&, ||, &, |, ^, !) : 논리곱(&&), 논리합(||), 배타적 논리합(^), 논리 부정(!) 연산을 수행하고 boolean 값을 산출
대입 연산자(=, +=, -=, *=, /=, %=) : 오른쪽 피연산자의 값을 왼쪽피연산자인 변수에 저장
=> 가장 낮은 연산 순위를 가지고 있어 제일 마지막에 수행, 진행 방향은 오른쪽 -> 왼쪽
public static void main(String[] args) {
int result = 0;
result += 10;
// result = result + 10;
System.out.println("result= " + result); // 10
result -= 5;
// result = result - 5;
System.out.println("result= " + result); // 5
result *= 3;
// result = result * 3;
System.out.println("result= " + result); // 15
result /= 5;
// result = result / 5;
System.out.println("result= " + result); // 3
result %= 3;
// result = result % 3;
System.out.println("result= " + result); // 0
}
public static void main(String[] args) {
int score = 85;
char grade = (score > 90) ? 'A' : ((score > 80) ? 'B' : 'C');
System.out.println(score + "점은 " + grade + "등급입니다.");
// 85점은 B등급입니다.
}