korean IT student

[Java-Live-Study] 3주차 - 연산자 본문

back-end/JAVA

[Java-Live-Study] 3주차 - 연산자

현창이 2021. 8. 16. 16:40

 

목표

자바가 제공하는 다양한 연산자를 학습하세요.

학습할 것

  • 산술 연산자
  • 비트 연산자
  • 관계 연산자
  • 논리 연산자
  • instanceof
  • assignment(=) operator
  • 화살표(->) 연산자
  • 3항 연산자
  • 연산자 우선 순위
  • (optional) Java 13. switch 연산자

1. 산술 연산자

  • 산술 연산자는 사칙연슨을 다루는 연산자로, 가장 기본적이면서 가장 많이 사용되는 연산자 중 하나입니다.
  • 산술 연산자는 모두 두 개의 피연선자를 가지는 이항 연산자이며, 피연산자들의 결합 방향은 왼쪽에서 오른쪽입니다.
int a = 2;
int b = 3;

// 연산자: +, 피연산자: a, b, 출력 : 5
a + b

// 연산자: -, 피연산자: a, b, 출력 : -1
a - b

// 연산자: *, 피연산자: a, b, 출력 : 6
a * b;

// 연산자: /, 피연산자: a, b, 출력 : 0
a / b;

// 연산자: %, 피연산자: a, b,  출력 : 2 
a % b;

 

2. 비트 연산자

  • & (AND)
  • | (OR)
  • ^ (XOR)
  • << (left shift)
  • >> (right shift)
  • >>> (unsigned right shift)  : 비트값을 오른쪽으로 이동한 이후 왼쪽 공간은 모두 0로 채움, C/C++ 에 없음
    int a = 3 & 1;  // 0011 & 0001 = 1
    int b = 2 | 1;  // 0010 | 0001 = 3
    int c = 3 ^ 1;  // 0011 ^ 0001 = 2
    int d = b >> 1; // 0011 에서 왼쪽으로 1칸 이동, 1(0001)
    int e = b << 1; // 0011 에서 오른쪽으로 1칸 이동, 6(0110)
    int f = ~a;     // 0001 -> 1111 1111 1111 1111 1111 1111 1111 1110
    // 1000 0000 0000 0000 0000 0000 0000 0000
    // ->
    // 0100 0000 0000 0000 0000 0000 0000 0000
    int g = -2147483648 >>> 1;​

3. 관계 연산자

  •  == (Equal to)
  •  != (Not equal to)
  •  > (greater than)
  •  < (less then)
  •  >= (greater than or equal to)
  •  <= (less than or equal to)
  • primitive type 인 피연산자에서 연산 가능
  • 연산 결과 타입은 boolean

4. 논리 연산자

  •  && (AND)
  •  || (OR)
  •  피연산자의 타입은 boolean
  •  연산 결과 타입은 boolean

5. instanceof

  • instanceof는 객체 타입을 확인하는 연산자입니다.
    class Parant {
    }
    class Child extends Parant{
    }
    
    public class Test {
         public static void main(String[] args){
    
            Parent parent = new Parent();
    
            Child child = new Child();
    
            System.out.println( parent instanceof Parent );  // true
            System.out.println( child instanceof Parent );   // true
            System.out.println( parent instanceof Child );   // false
            System.out.println( child instanceof Child );   // true
        }
    
    }​
     
  • parent 자신의 클래스를 찾아서 true
  • child이 상속받은 Parent를 찾았으니 true

6. assignment(=) operator (대입 연산자)

  • = (ASSIGN)
  • += (ADD and ASSIGN)
  • -= (SUBTRACT and ASSIGN)
  • *= (MULTIPLY and ASSIGN)
  • /= (DIVIDE and ASSIGN)
  • %= (MODULO and ASSIGN)
  • &= (AND and ASSIGN)
  • ^= (XOR and ASSIGN)
  • |= (OR and ASSIGN)
  • <<= (LEFT SHIFT and ASSIGN)
  • >>= (RIGHT SHIFT and ASSIGN)
  • >>>= (UNSIGNED RIGHT SHIFT and ASSIGN)
  • 변수에 값을 대입할 때 사용하는 이항 연산자입니다.
int num1 = 3, num2 = 4, num3 = 5;

num1 = num1 + 5;
num2 -= 5; // num2 = num2 - 4 , num2 = -1
num3 =- 5; // num3 = -5

 

7. 화살표(->) 연산자

  • 자바 8 버전부터 람다 표현식이 공식적으로 적용
  • 함수형 프로그래밍 표현
interface Test{
    int func(int a);
}

class Test2 {
    public void func(Test test){
        int value = test.func(3);
        System.out.println(value);
    }
}

class Operator{
    public static void main(String[] args) {
        Test2 test2 = new Test2();
		// lambda expression 사용 X 버전
        test2.func(new Test() {
            public int func(int a){
                return a + 2;
            }
        });
		// lambda expression 사용 버전
        test2.func((a) ->{
            return a + 2;
        });
    }
}

8. 3항 연산자

  • 구조  -  조건식 ? 피연산자1 : 피연산자2
  • 조건식의 참이라면 결과는 피연산자1이 선택되고, 거짓이라면 피연산자2 선택된다.
    int a = 0;
    
    //삼항 연산자 a가 3보다 크면 10 아니면 30 출력
    a = a > 3 ? 10 : 30;

9. 연산자 우선 순위

 

10. (optional) Java 13. switch 연산자

 

 

 

[참고]

https://www.notion.so/3-f3a94e0092664d8aa1debe7e88dec43b

 

Comments