항과 연산자
- 단항 연산자 : 항이 1개 ex) i++
- 이항 연산자 : 항이 2개 ex) a + b
- 삼항 연산자 : 항이 3개 ex) (3 > 1) ? A : B
대입 연산자 (=)
우측의 데이터를 좌측의 변수에 대입
ex) String subject = "국어";
ex) int age = 50;
부호 연산자 (+, -)
부호를 나타내는 연산자
ex) +1, -1
산술 연산자(+, -, *, /, %)
덧셈, 뺄셈, 곱셈, 나눗셈, 나머지
ex) num % 2
증가/감소 연산자 (++, --)
값을 1만큼 늘리거나 1만큼 줄임
ex) i++, i--
ex) count++
관계 연산자 (>, <, >=, <=, ==, !=)
두 항의 값 크기 비교
결과 값은 비교 결과에 따라 true 또는 false
ex) numA > numB
ex) age != 10
논리 연산자 (&&, ||, !)
논리식에 대해 참 거짓 판단
결과 값은 판단 결과에 따라 true 또는 false
ex) (age > 14) && (age < 19)
ex) (10 > 5) || (1 == 2)
&& : And => 앞과 뒤에 논리식 모두 맞아야 true
&& 논리 연산자는 앞과 뒤가 모두 맞아야 true이기 때문에 따라서 앞에 논리식이 false면 뒤에 논리식은 연산하지 않는다.
|| : Or => 앞과 뒤 논리식 중 하나만 맞으면 true
|| 논리 연산자는 앞에 논리식이 true인지 false인지 상관 없이 뒤에 논리식까지 연산한다.
! : Not
복합 대입 연산자
대입 연사자와 다른 연산자를 조합한 연산
코드를 간결하게 작성할 때 사용
ex) num1 += num2 => num1 = num1 + num2
ex) num1 %= num2 => num1 = num1 % num2
System.out.println("[===== 항과 연산자 =====]");
// 단항 연산자
int count = 10;
count++;
System.out.println("count = " + count); // 11
// 이항 연산자
String str1 = "Hello";
String str2 = " World!";
System.out.println(str1 + str2); // Hello World!
// 삼항 연산자
count = 10;
String evenNum = "";
evenNum = ((count % 2) == 0) ? "짝수" : "홀수";
System.out.println("evenNum = " + evenNum); // 짝수
System.out.println("[===== 대입 연산자 =====]");
// 대입 연산자
String subject = "국어";
int[] money = {10, 50, 100, 500};
System.out.println("[===== 부호 연산자 =====]");
int number = 10;
System.out.println(-number); // -10
System.out.println("[===== 산술 연산자 =====]");
System.out.println(4 % 2); // 0
System.out.println(7 % 2); // 1
System.out.println("[===== 증감/감소 연산자 =====]");
int cntNum = 0;
System.out.println(cntNum); // 0
System.out.println(cntNum++); // 0
System.out.println(cntNum); // 1
System.out.println(++cntNum); // 2
System.out.println("[===== 관계 연산자 =====]");
System.out.println(1 > 2); // false
System.out.println(1 < 2); // true
System.out.println("[===== 논리 연산자 =====]");
int age = 10;
System.out.println((age > 9) && (age < 4)); // false
System.out.println((age > 9) || (age < 4)); // true
System.out.println("[===== 복합 대입 연산자 =====]");
int totalPrice = 0;
int applePrice = 1000;
totalPrice += applePrice;
System.out.println(totalPrice); // 1000
2진법
컴퓨터에서 데이터 표현에 사용
2를 기반으로 하는 숫자 체계
10진수 | 0 | 1 | 2 | 3 | 4 |
2진수 | 0 | 1 | 10 | 11 | 100 |
2의 보수
2의 제곱수에서 빼서 얻은 이진수
ex) 2진수 3의 2의 보수 : 11 -> 01
비트 연산자
비트 단위로 연산
기본 연산자와 비트 연산자 비교
기본 연산자 : &&, ||
비트 연산자 : 0101 & 0011, 3 | 2
비트 논리 연산자
AND 연산자 (&) : 두 개의 비트 값이 모두 1인 경우에만 결과 1
OR 연산자 (|) : 두 개의 비트 값 중 하나라도 1이면 결과 1
XOR 연산자 (^) : 두 개의 비트 값이 같으면 0, 다르면 1
반전 연산자 (~) : 비트 값이 0이면 1로, 1이면 0으로 반전
비트 이동 연산자
<< 연산자 : 비트를 왼쪽으로 이동
>> 연산자 : 비트를 오른쪽으로 이동
>>> 연산자 : 비트를 오른쪽으로 이동 (부호비트 상관 없이 0으로 채움)
System.out.println("[===== 2진법 =====]");
System.out.println(Integer.toBinaryString(2)); // 10
System.out.println(Integer.toBinaryString(10)); // 1010
System.out.println("[===== 비트 논리 연산자 =====]");
int a = 2;
int b = 3;
int result1 = 0;
result1 = a & b;
System.out.printf("%04d\n", Integer.parseInt(Integer.toBinaryString(a))); // 0010
System.out.printf("%04d\n", Integer.parseInt(Integer.toBinaryString(b))); // 0011
System.out.printf("%04d\n", Integer.parseInt(Integer.toBinaryString(result1))); // 0010
result1 = a | b;
System.out.printf("%04d\n", Integer.parseInt(Integer.toBinaryString(a))); // 0010
System.out.printf("%04d\n", Integer.parseInt(Integer.toBinaryString(b))); // 0011
System.out.printf("%04d\n", Integer.parseInt(Integer.toBinaryString(result1))); // 0011
result1 = a ^ b;
System.out.printf("%04d\n", Integer.parseInt(Integer.toBinaryString(a))); // 0010
System.out.printf("%04d\n", Integer.parseInt(Integer.toBinaryString(b))); // 0011
System.out.printf("%04d\n", Integer.parseInt(Integer.toBinaryString(result1))); // 0001
result1 = ~a;
System.out.printf("%04d\n", Integer.parseInt(Integer.toBinaryString(a))); // 0010
System.out.printf("%s\n", Integer.toBinaryString(result1)); // 11111111111111111111111111111101
System.out.println("[===== 비트 이동 연산자 =====]");
a = 2;
result1 = a << 1;
System.out.printf("%04d\n", Integer.parseInt(Integer.toBinaryString(a))); // 0010
System.out.printf("%04d\n", Integer.parseInt(Integer.toBinaryString(result1))); // 0100
result1 = a >> 1;
System.out.printf("%04d\n", Integer.parseInt(Integer.toBinaryString(a))); // 0010
System.out.printf("%04d\n", Integer.parseInt(Integer.toBinaryString(result1))); // 0001
a = -2;
result1 = a >>> 1;
System.out.printf("%s\n", Integer.toBinaryString(a)); // 11111111111111111111111111111110
System.out.printf("%s\n", Integer.toBinaryString(result1)); // 1111111111111111111111111111111 맨 앞에 0
'📌Back-End > Java' 카테고리의 다른 글
Java 추상 클래스, 인터페이스, 내부 클래스 (0) | 2024.04.22 |
---|---|
Java 상속과 다형성 (0) | 2024.04.20 |
Java 다차원 배열, 클래스와 객체 (1) | 2024.04.19 |
Java 조건문과 반복문 (1) | 2024.04.18 |
Java 변수와 자료형 (0) | 2024.04.16 |