연산자
package chap_02;
public class _01_Operator1 {
public static void main(String[] args) {
// 산술 연산자
// 일반 연산
System.out.println(4 + 2); // 6
System.out.println(4 - 2); // 2
System.out.println(4 * 2); // 8
System.out.println(4 / 2); // 2
System.out.println(5 / 2); // 2
System.out.println(4 % 2); // 2
// 우선 순위 연산
System.out.println(2 + 2 * 2); // 6
System.out.println((2 + 2) * 2); // 8
System.out.println(2 + (2 * 2)); // 6
// 변수 연산
int a = 10;
int b = 5;
int c;
c = a + b;
System.out.println(c); // 15
c = a - b;
System.out.println(c); // 5
c = a * b;
System.out.println(c); // 50
c = a / b;
System.out.println(c); // 2
c = a % b;
System.out.println(c); // 0
}
}
package chap_02;
public class _02_Operator2 {
public static void main(String[] args) {
// 산술 연산자
// 증감 연산자
int val;
val = 10;
System.out.println(val); // 10
System.out.println(val++); // 수행 우선 10
System.out.println(val); // 11
val = 10;
System.out.println(val); // 10
System.out.println(++val); // 연산 우선 11
System.out.println(val); // 11
val = 10;
System.out.println(val); // 10
System.out.println(--val); // 연산 우선 9
System.out.println(val); // 9
int waiting = 0;
System.out.println(String.format("대기 인원: %d", waiting++)); // 0
System.out.println(String.format("대기 인원: %d", waiting++)); // 1
System.out.println(String.format("대기 인원: %d", waiting++)); // 2
System.out.println(String.format("총 대기 인원: %d", waiting));
}
}
package chap_02;
public class _03_Operator3 {
public static void main(String[] args) {
// 대입 연산자
int num = 10;
num = num + 2;
System.out.println(num); // 12
num = num - 2;
System.out.println(num); // 10
// 복합 대입 연산자
num += 2;
System.out.println(num); // 12
num -= 2;
System.out.println(num); // 10
}
}
package chap_02;
public class _04_Operator4 {
public static void main(String[] args) {
// 비교 연산자
System.out.println(5 > 3); // true
System.out.println(5 < 3); // false
System.out.println(5 >= 3); // true
System.out.println(5 <= 3); // false
System.out.println(5 == 3); // false
System.out.println(5 != 3); // true
}
}
package chap_02;
public class _05_Operator5 {
public static void main(String[] args) {
// 논리 연산자
boolean 김치찌개 = true;
boolean 계란말이 = true;
boolean 제육볶음 = true;
boolean 사료 = false;
System.out.println(김치찌개 || 계란말이 || 제육볶음); // or 연산 true
System.out.println(김치찌개 || 계란말이 || 제육볶음 || 사료); // or 연산 true
System.out.println(김치찌개 && 계란말이 && 제육볶음 && 사료); // and 연산 false
System.out.println(김치찌개 && 계란말이 && 제육볶음); // and 연산 true
}
}
package chap_02;
public class _06_Operator6 {
public static void main(String[] args) {
// 삼항 연산자 // 조건 ? true 리턴 값 : false 리턴 값
int x = 5;
int y = 3;
int maxNum = (x > y) ? x : y;
int minNum = (x < y) ? x : y;
System.out.println(maxNum);
System.out.println(minNum);
boolean b = (x == y) ? true : false;
System.out.println(b); // false
}
}