개발모음집

[JAVA Basic] 10강 ~ 13강 연산자 본문

JAVA

[JAVA Basic] 10강 ~ 13강 연산자

void 2016. 6. 2. 00:00

연산: 주어진 식을 계산하여 결과를 얻어내는 과정

피연산자: 연산자의 작업 대상

 

"a + 5" -> “”=

‘+’, ‘-’, ‘==’, ‘/’ ... -> 연산자

'a'= 피연산자


 

~: 비트별 not 연산자 2 -> 0010 -> ~0010 -> 1101

!: 논리not 연산자 true -> false

boolean a =truefalse라는 것

instanceof(class비교연산자, java에만 존재하는 연산자.)

같은 클래스면 true, 다른 클래스면 false

 

(타입) 강제형변항도 단항 연산자이다.

 

비트 연산자 &(AND), ^(XOR), |(OR)

ex)

2&3 -> 0010&0011 -> 0010 -> 2

2|3 -> 0010|0011 -> 0011 -> 3

2^3 -> 0010^0011 -> 0001 -> 1 // ^: 서로 다르면 1, 같으면 0

 

&&, ||는 비교문(if, switch)에서 사용

ex) a=true; b=true; a&&b->true

 

삼항 연산자

변수=(조건)?:거짓

 

대입 연산자(할당 연산자)

a =a*3보다 a*=3의 수행속도가 빠르다.

 

연산자 우선순위

( ) - 단항 - 산술 - 비교 - 논리 - 삼항 - 대입


ex)operatorEx


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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// 연산자
public class operatorEx
{
 
    public static void main(String [] args)
    {
        System.out.println("1. 산술 연산자");
        int a = 100;
        
        System.out.println("a*5 = "+a*5);
        System.out.println("a*5 = "+a/5);
        
        /* 쉬프트 연산자 >>, <<, >>>(자바에만 있는 연산자)
        
             00000011 맨 앞은 부호비트로 쓴다.
             0은 '+', 1은 '-'
             3은 0000 0011 이고
               -3은 1111 1100 이다.
               -3을 일반적으로 계산하면 -127+ ...+1= -3    
          
           1) >> ---> 오른쪽으로 주어진 비트수만큼 이동하라
                     앞에 비어있는 빈 칸은 부호비트로 채운다.
           2) << ---> 왼쪽으로 주어진 비트수만큼 이동하라
                    뒤에 비어있는 빈 칸은 부호비트로 채운다.
           3) >>>(자바에만 있는 연산자) ---> 오른쪽으로 주어진 비트수만큼 이동
                           앞에 비어있는 빈칸은 0으로 채운다.
           4) <<<(자바에만 있는 연산자) ---> 왼쪽으로 주어진 비트수만큼 이동
                             뒤에 비어있는 빈칸은 0으로 채운다.
                         
            */
            
            System.out.println("(-6 >> 2) ="+(-6 >> 2));
            /* 8 bit로 계산하자.
               6: 0000 0110 
              -6: 1111 1010 
            
            +을 -로, -을 +로 쉽게 바꾸는 법
            뒤에서부터 보는데 1을 만나면 그 1은 납두고 그 앞의 숫자들을 바꿔준다
            
            1111 1010을 >> 한다면 1111 1110이고 1이 많으니 이걸 +로 바꿔야 계산하기 쉽다.
            -> 0000 0010은 2니까 값 -2 
            */
        
            System.out.println("(-6 >>> 2) ="+(-6 >>> 2));
            /* 결과값: 1073741822
             * 이렇게 하면 기본형인 int, 32bit로 계산한다.
                -6: 00111111 11111111 11111111 11111110
             */
            
            System.out.println("(-6 << 2) ="+(-6 << 2));
            
        
        
        System.out.println("2. 논리연산자(비트 연산자)");
        int b = 3;
        int c = 5;
        
        System.out.println("b AND c ="+(b&c));
        // 연산자 우선순위때문에 b&c에 ()를 해준다. 
        System.out.println("b OR c ="+(b|c));
        System.out.println("b XOR c ="+(b^c));
        
        // &&, ||(조건 연산자)
        int mm = 30;
        int nn = 20;
        
        if((mm>nn) && (mm==10))
            System.out.println("참입니다.");
        else
            System.out.println("거짓입니다.");
        // == 비교연산자
    
        System.out.println("3. 비교 연산자)");
        int m = 100, n = 100;
        System.out.println("m>n ="+(m>n));
        //비교연산자 L>R (L이 R보다 큰), >=(L이 R보다 크거나 같으면), <(R보다 작은) ==(L과 R이 같은) != (서로 같지 않은)
//        if(m>n)
//            System.out.println("m은 n보다 크다");    
//        else
//            System.out.println("m은 n보다 작거나 같다");
    
        if(m!=n)
            System.out.println("m과 n은 같지 않다.");
        else
            System.out.println("m과 n은 같다");
    
        System.out.println("4. 삼항 연산자)");
        int aa=100, bb=200;
        //변수 = (조건)? 값1:값2; 참이면 값1, 거짓이면 값2
        int result =(aa>bb)?aa:bb;
        System.out.println("result= "+result);
        
        String str = (aa>bb)? "aa는 bb보다 큼":"aa는 bb보다 작거나 같다";
        
        System.out.println("str = "+str);
        
        System.out.println("5. 대입 연산자)");
        int ii=10;
        ii+=10// ii=ii+10;보다 연산속도가 빠르다
            
        System.out.println("ii ="+ii);
    
        int jj=10;
        jj *=3;
        System.out.println("jj ="+jj);
    
        int x = 5;
        x<<=2;
        System.out.println("x ="+x);
        // 0000 0101 -> << 연산 0001 0100 -> 20 
        
        int y = 3;
        y ^=2//y=y^2; XOR 연산 서로 다른 비트인 경우 값이 1
        System.out.println("y ="+y);
        
        System.out.println("6. 단항 연산자");
        // ++, -- (증감연산자) , +, - (부호연산자)
        int xx = 10, yy=20;
        System.out.println(+xx);
        System.out.println(+yy);
        
        int k = 10;
        byte kk = 20;
        k++// k=k+1;
        kk--;// kk=kk-1;
    
        System.out.println("k="+k);
        System.out.println("kk ="+kk);
        
        int cc = 10;
        int dd =++cc; // cc값을 먼저 증가시키고 다음에 dd에 대입을 한다.
        System.out.println("cc= "+cc); //출력값: 11
        System.out.println("dd= "+dd); //출력값: 11
        
        cc = 10;
        int ee = cc++// 대입을 먼저한 후 cc값을 증가시킨다.
        System.out.println("cc= "+cc); //출력값: 11
        System.out.println("ee= "+ee); //출력값: 10
    
        // 비트별 not 연산자 : ~
        int gg = 0xfffffff3//int=4byte,32bit
        System.out.println("~gg= "+~gg); //출력값:  12
        // ~ 11111111 11111111 11111111 11110011 = 00000000 00000000 00000000 00001100
    
        // 논리 부정 연산자: !
        boolean bool = true;
        System.out.println("!bool ="+!bool);
    }
}
 
cs