코딩/Java

자바(JAVA) - 연산자(Operator)

숲속의 움비 2021. 1. 26. 23:42
반응형

연산자의 종류와 우선순위


 연산자의 종류

연산자의 종류 

우선순위 

 최우선 연산자

  ., [], () 

 위로 갈수록 우선순위가 높고 아래로 갈수록 우선순위가 낮다.

우선순위가 같으면 좌측 연산자부터 연산된다

 단항 연산자

  ++, --, !, instanceof

 산술 연산자

  +, -, *, /, % 

 쉬프트 연산자 

  >>, <<, >>> 

 비교 연산자

  >,<, >=, <=, ==,  !=

 비트 연산자

 &, |, ^, ~

 논리 연산자

 &&, ||, !

 삼항 연산자

 (조건식)?: 

 대입 연산자

  =. *=, /=, %=, +=, -= 


 - 단항, 대입 연산자를 제외한 모든 연산의 방향은 왼쪽에서 오른쪽

 - 복잡한 연산식에는 괄호 ()를 사용해서 우선순위를 정해주자



더하기 연산자


1
2
3
4
5
6
7
8
9
10
public class example1 {
    public static void main(String[] args) {
        String str1 = 2 + 3 + "";
        String str2 = 2 + "" + 3;
        String str3 = "" + 2 + 3;
        System.out.println(str1);
        System.out.println(str2);
        System.out.println(str3);
    }
}
cs


실행 결과 : 

5

23

23

-  str1은 정수인 2와 3이 먼저 계산되 값이 5가 되었지만
    str2, str3는 숫자 2와 3이 문자로 취급되기때문에 2와 3이 그대로 출력됐다.



증감 연산자


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public static void main(String[] args) {
        int x = 10;
        int y = 10;
        int z;
        
        x++;
        ++x;
        System.out.println("x = " + x);
        
        y--;
        --y;
        System.out.println("y = " + y);
        
        z = ++x;
        System.out.println("z = " + z);
        System.out.println("x = " + x);
        
        z = ++x;
        System.out.println("z = " + z);
        System.out.println("x = " + x);
        
        z = +++ y++;
        System.out.println("z = " + z);
        System.out.println("x = " + x);
        System.out.println("y = " + y);
    }
cs


 - 증감 연산자는 변수의 앞에 오느냐 뒤에 오느냐에 따라서 연산 실행되는 시점이 달라진다.

 - 변수 앞에 증감 연산자가 오면 증감 연산자를 먼저 실행하고 다른 연산을 실행

 - 증감 연산자가 뒤에 오면 다른 연산을 먼저 실행하고 증감 연산자를 실행한다.



논리 연산자


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class example1 {
    public static void main(String[] args) {
       int charCode='A';
       if ((charCode>=97&&(charCode<=122)) {
           System.out.println("소문자 이군요");
       }if (!(charCode<48&& !(charCode>57)) {
           System.out.println("0~9 숫자이군요.");
       }
       
       int value = 6;
       if((value%2==0||(value%3==0)) {
           System.out.println("2 또는 3의 배수이군요.");
       }
    }
}
cs

실행 결과 : 2 또는 3의 배수이군요.


 - 논리곱 (AND) 연산자 : &

입력 중 하나만 거짓이면 결과는 거짓

 - 논리합(OR) 연산자 : ||

입력 중 하나만 참이면 결과는 참

 - 논리 연산자는 결과가 정해지면 연산을 중단한다.

(10<5) && (100>50) -> 10<5가 거짓이므로 결과는 false

(100>50) || (10<5) -> 100>50이 참이므로 결과는 true


논리 부정 연산자


1
2
3
4
5
6
7
8
9
10
11
12
public class example1 {
    public static void main(String[] args) {
        boolean play = true;
        System.out.println(play);
        
        play = !play;
        System.out.println(play);
        
        play = !play;
        System.out.println(play);
    }
}
cs

실행 결과 : 

true

false

true



 - ! 연산자는 참으로 거짓으로, 거짓은 참으로 뒤집는다.



% 연산


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class example1 {
    public static void main(String[] args) {
       int a = 15;
       int b = 2;
       int c = 3;
       
       if(a%b==0) {
           System.out.println(a + "는 " + b + "의 배수입니다.");
       } else {
           System.out.println(a + "는 " + b + "의 배수가 아닙니다.");
       }
       if(a%c==0) {
           System.out.println(a + "는 " + c + "의 배수입니다.");
       } else {
           System.out.println(a + "는 " + c + "의 배수가 아닙니다.");
       }
       
    }
}
cs

실행 결과 : 

15는 2의 배수가 아닙니다.

15는 3의 배수입니다.


1. 비교 연산

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class example1 {
    public static void main(String[] args) {
        int num1 = 10;
        int num2 = 10;
        boolean result1 = (num1 == num2);
        boolean result2 = (num1 != num2);
        boolean result3 = (num1 <= num2);
        System.out.println("result1 = " + result1);
        System.out.println("result2 = " + result2);
        System.out.println("result3 = " + result3);
        
        char char1 = 'A';
        char char2 = 'B';
        boolean result4 = (char1 < char2);
        System.out.println("result4 = " + result4);
    }
}
cs

실행 결과 : 

result1 = true

result2 = false

result3 = true

result4 = true


 - 비교연산자는 연산을 수행하기 전 피연산자의 타입을 일치시킨다.
'A'==65는 'A'가 int로 변환되어 65==65로 비교
3==3.0은 3이 double로 변환되어 3.0==3.0으로 비교

2. 비교 연산 


1
2
3
4
5
6
7
8
9
10
11
12
public class example1 {
    public static void main(String[] args) {
        int v2 = 1;
        
        double v3 = 1.0;
        System.out.println(v2 == v3);         // true
        
        double v4 = 0.1;
        float v5 = 0.1f;
        System.out.println(v4 == v5);         // false
    }
}
cs

실행 결과 : 

true

false


대입 연산


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class example1 {
    public static void main(String[] args) {
        int result = 0;
        result += 10;
        System.out.println("result = " + result);
        result -= 3;
        System.out.println("result = " + result);
        result *= 3;
        System.out.println("result = " + result);
        result /= 3;
        System.out.println("result = " + result);
        result %= 3;
        System.out.println("result = " + result);
    }
}
cs

실행결과 : 

result = 10

result = 7

result = 21

result = 7

result = 1


삼항 연산


1
2
3
4
5
6
7
8
9
10
public class example1 {
    public static void main(String[] args) {
        int a = 10;
        int b = 20;
        int large = a>b? a: b;
        int small = a>b? b: a;
        System.out.println("큰 수 : " + large);
        System.out.println("작은 수 : " + small);
    }
}
cs

실행 결과 : 

큰 수 : 20

작은 수 : 10



반응형