nme.kr

Ch.04 연산

대입연산

대입연산자

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
package chapter04;
 
public class OpEx1 {
 
    public static void main(String[] args) {
        /*
        int a = 10;
        10 = a;     // 에러 1
        a + 1 = 10; // 에러 2
        10 = 1;     // 에러 3
        a = a + 10; // 정상
        */
         
        int number1 = 10;
        System.out.println("number1 = 10 -> "+number1); // 10출력
        number1 += 10; // 더하고 대입
        System.out.println("number1 += 10 -> "+number1); // 20 출력
        number1 -= 10; // 빼고 대입
        System.out.println("number1 -= 10 -> "+number1); // 10 출력
        number1 *= 2; // 곱하고 대입
        System.out.println("number1 *= 2 -> "+number1); // 20 출력
        number1 /= 2; // 나누고 대입
        System.out.println("number1 /= 2 -> "+number1); // 10 출력
        number1 %= 3 ; // 나머지 대입
        System.out.println("number1 %= 3 -> "+number1); // 1 출력
         
 
    }
 
}

산술연산

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
package chapter04;
 
public class OpEx2 {
 
    public static void main(String[] args) {
         
//      int a = 5 + 5;
//      System.out.println("5 + 5 = "+a);   // 10출력
//      int b = a - 5;
//      System.out.println("10 - 5 = "+b);  // 5출력
//      int c = b * 2;
//      System.out.println("5 * 2 = "+c);   // 10출력
//      int d = c / 5;
//      System.out.println("10 / 5 = "+d);  // 2출력
//     
//      // 나머지 연산
//      int e = 10 % 3; // 10 나누기 3의 결과값은 몫이 3, 나머지 1
//      System.out.println("10 % 3 = "+e);
//     
         
//      int a = 10;
//      int b = 4;
//     
//      System.out.println("10 / 4 = "+a / b); // 결과값 2
         
//      double a = 10;
//      int b = 4;
//     
//      System.out.println("10 / 4 = "+a / b); // 결과값 2
         
         
        // 결과값이 실수로 연산되게하는 방법
 
        int a = 10;
        int b = 4;
         
        System.out.println("10 / 4 = "+(double)a / b);
         
    }
 
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package chapter04;
 
public class OpEx2_1 {
 
    public static void main(String[] args) {
         
        int a = 5;
         
        System.out.println("+a = " + +a);
        System.out.println("-a = " + -a);
         
        a = -5;
         
        System.out.println("+a = " + +a);
        System.out.println("-a = " + -a);
         
    }
 
}

증감연산

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
package chapter04;
 
public class OpEx3 {
 
    public static void main(String[] args) {
         
//      int a = 10;
//      int b = 10;
//     
//      ++a; // 전위연산
//      b++; // 후위연산
//     
//      System.out.println(a);
//      System.out.println(b);
         
//      int a = 10;
//      int b = ++a;
//     
//      System.out.println("전위연산 결과 : "+b);
//     
//      int x = 10;
//      int y = x++;
//      System.out.println("후위연산 결과 : "+y);
//      System.out.println("x : "+x);
         
        int a = 10;
        a++;
        System.out.println("a++ : " + a);
        int b = 10;
        b += 1;
        System.out.println("b += 1 : " + b);
        int c = 10;
        c = c + 1;
        System.out.println("c = c + 1 : " + c);
         
         
 
    }
 
}

비교연산

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package chapter04;
 
public class OpEx4 {
 
    public static void main(String[] args) {
 
        int a = 10;
        int b = 5;
         
        System.out.println(a > b);   // true
        System.out.println(a >= b);  // true
        System.out.println(a < b);   // false
        System.out.println(a <= b);  // false
        System.out.println(a == b); // false
        System.out.println(a != b); // true
         
        boolean c = a == b; // c 변수에 결과값 대입
        System.out.println("c = "+c);
        boolean d = c == false; // d 변수에 결과값 대입
        System.out.println("d = "+d);
    }
 
}

논리연산

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
package chapter04;
 
public class OpEx5 {
 
    public static void main(String[] args) {
 
        int a = 10;
        int b = 5;
         
        // AND 연산
        System.out.println(a > b && a == 10);    // true
        System.out.println(a > b && a == b); // false
         
        // OR 연산
        System.out.println(a > b || a == b); // true
        System.out.println(a < b || a == b); // false
         
        // XOR 연산
        System.out.println(a > b ^ a == 10); // false
        System.out.println(a > b ^ a == b);      // true
         
        // NOT 연산
        System.out.println(!(a > b));    // false
        System.out.println(!(a < b));    // true
         
         
    }
 
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package chapter04;
 
public class OpEx5_1 {
 
    public static void main(String[] args) {
 
        int a = 10;
        int b = 5;
         
        // 모두 false
        System.out.println(a == b && a > b);
        System.out.println(a == b & a > b);
         
         
         
    }
 
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package chapter04;
 
public class OpEx5_2 {
 
    public static void main(String[] args) {
 
        int a = 10;
        int b = 5;
         
        // & 연산
        System.out.println(a == b & test());
         
        // && 연산
        System.out.println(a == b && test());
    }
     
    public static boolean test() {
        System.out.println("test()메서드 실행됨");
        return true;
    }
 
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package chapter04;
 
public class OpEx5_3 {
 
    public static void main(String[] args) {
 
        int a = 10;
        int b = 0;
         
        // & 연산
        System.out.println(b > 0 && (a / b > 0));
         
    }
 
}

비트연산

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
package chapter04;
 
public class OpEx6 {
 
    public static void main(String[] args) {
         
        System.out.println("2:"+Integer.toBinaryString(2)); // 10진수 2를 2진수로 변환
        System.out.println("3:"+Integer.toBinaryString(3)); // 10진수 3을 2진수로 변환
         
        // 비트 논리연산
        System.out.println("2&3 : "+(2&3));
        System.out.println("2|3 : "+(2|3));
        System.out.println("2^3 : "+(2^3));
        System.out.println("~3 : "+~3);
         
        // 첫자리는 부호를 나타내는 비트를 포함하여 모든 비트를 반대로 변환
        System.out.println("~3을 이진수로 :"+Integer.toBinaryString(~3));
         
        // ~3의 2진수값의 길이
        // 첫자리는 부호를 나타내는 비트 나머지 31자리로 정수자료형을 메모리에 저장
        System.out.println(Integer.toBinaryString(~3).length());
         
        System.out.println(Integer.MAX_VALUE);  // 정수자료형의 최대값 (2의 31승 -1)
        System.out.println(Integer.parseInt("1111111111111111111111111111100",2)
                -Integer.MAX_VALUE-1);
    }
 
}

비트 쉬프트 연산

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package chapter04;
 
public class OpEx6_1 {
 
    public static void main(String[] args) {
         
        // << 연산
        System.out.println(3 << 2);
        System.out.println("3의   이진수 : "+Integer.toBinaryString(3));
        System.out.println("12의 이진수 : "+Integer.toBinaryString(12));
         
        // >> 연산
        System.out.println(8 >> 2);
        System.out.println("8의   이진수 : "+Integer.toBinaryString(8));
        System.out.println("2의 이진수 : "+Integer.toBinaryString(2));
         
        // >>> 연산
        System.out.println(-8 >>> 3);
        System.out.println("-8의   이진수 : "+Integer.toBinaryString(-8));
        System.out.println("-8 >>> 3 : "+Integer.toBinaryString(-8 >>> 3));
    }
 
}

삼항연산

1
2
3
4
5
6
7
8
9
10
11
12
13
package chapter04;
 
public class OpEx7 {
 
    public static void main(String[] args) {
         
        int score = 80;
        String pass = score >= 60 ? "합격 " : "불합격";
        System.out.println(pass);
         
    }
 
}

문자열 연산

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package chapter04;
 
public class OpEx8 {
 
    public static void main(String[] args) {
         
        String name = "홍길동";
        System.out.println("안녕하세요 " + name + " 입니다.");
         
        int height = 180;
        System.out.println("저의 키는 " + height + "cm입니다.");
         
        // String weight = 75.5; // 에러발생 (문자자료형 변수에는 숫자 대입 못함)
        String weight = 75.5 + "";
        System.out.println("제 몸무게는 " + weight + "kg입니다.");
         
        int ageInt = 30;        // 정수
        String ageStr = "30";   // 문자열
         
         
    }
 
}

연산자 우선순위

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package chapter04;
 
public class OpEx9 {
 
    public static void main(String[] args) {
         
        int a = 5;
        int b = 4;
        int c = 3;
         
        // * 연산이 먼저 실행됨
        System.out.println(a + b * c);
         
        // 괄호로 묶어 우선순위를 높여줌
        System.out.println((a + b) * c);
         
    }
 
}