자바의정석 3장 연산자 요약

서현우·2022년 6월 18일
0

자바의정석

목록 보기
15/22

Chapter 03 연산자(Operator)

연산자의 종류

산술, 비교, 논리(&&가 ||보다 우선순위가 높다), 대입

산술 변환

연산 수행 직전에 발생하는 피연산자의 자동 형변환

  • 두 피연산자의 타입을 같게 일치(보다 큰 타입으로)
  • 피연산자의 타입이 int보다 작은 타입이면 int로 변환
    (byte, short, char)

증감 연산자

증가 연산자(++)
감소 연산자(--)
전위형, 후위형

x = x++ - ++x; //x는 -2

char 알파벳, 숫자 출력

public class Ex14 {
    public static void main(String[] args) {
        //'a'~'z', 'A'~'Z', '0'~'9'를 출력
        //a~z는 26개
        char c = 'a'; //문자형 알파벳과 숫자를 저장할 변수
        for (int i = 0; i < 26; i++) {
            System.out.print(c++);
        }
        System.out.println();
        c = 'A';
        for (int i = 0; i < 26; i++) {
            System.out.print(c++);
        }
        System.out.println();
        c = '0';
        for (int i = 0; i < 10; i++) {
            System.out.print(c++);
        }
    }
}

char 알파벳 소문자를 대문자로 변경

public class Ex15 {
    public static void main(String[] args) {
        //문자형 소문자 a를 대문자 A로 바꿈
        //소문자에서 정수형 32를 빼면 대문자가 된다
        char lowerCase = 'a';
        char upperCase = (char)(lowerCase - 32);
        System.out.println("upperCase = " + upperCase);
    }
}
public class Ex17 {
    public static void main(String[] args) {
        //Math.round()를 쓰지않고 소수점 3째자리까지 반올림해서 출력
        double pi = 3.141592;
        double shortPi = (int)(pi*1000+0.5)/1000.0;
        System.out.println("shortPi = " + shortPi);

        //Math.round()를 쓰고 소수점 3째자리까지 반올림해서 출력
        double shortPi2 = Math.round(pi*1000)/1000.0;
        System.out.println("shortPi2 = " + shortPi2);
    }
}

%의 피연산자에 음수가 있는 경우

public class Ex20 {
    public static void main(String[] args) {
        //나머지 연산자의 피연사자 중에 음수가 있을 때의 계산
        //%는 나누는 수로 음수도 허용하지만 부호는 무시됨.
        //따라서 %의 결과는 나누는 수로 양수, 음수 동일.
        //결과값의 부호는 나눠지는 수의 부호로 결정됨.
        System.out.println(-10%8); //-2
        System.out.println(10%-8); //2
        System.out.println(-10%-8); //-2

    }
}

비교연산자의 연산과 자동 형변환

실수형은 오차를 주의.
&&, ||이 같이 있으면 우선순위가 &&가 더 높다.

public class Ex21 {
    public static void main(String[] args) {
        //비교연산자의 연산
        //비교연산자도 이항연산자이므로 연산을 수행하기 직전
        //자동형변환을 통해 두 피연산자의 타입을 같게(큰범위의 타입으로) 맞춘다.
		//주의! 실수형은 오차 주의!
        System.out.printf("10==10.0f = %b%n", 10 == 10.0f); //true
        System.out.printf("'0'==0 = %b%n", '0' == 0); //false
        System.out.printf("'A'==65 = %b%n", 'A' == 65); //true
        System.out.printf("'A'>'B' = %b%n", 'A' > 'B'); //false
        System.out.printf("'A'+1!='B' = %b%n", 'A' + 1 != 'B'); //false
    }
}

equalsIgnoreCase()는 대소문자 구분X

Scanner

nextInt(), next(), nextLine()

삼항연산자 예제

public class Ex32 {
    public static void main(String[] args) {
        //정수를 절대값으로 바꾸고, 원래 정수의 부호를 문자로 앞에 붙임.
        int x, y, z;
        int absX, absY, absZ;
        char signX, signY, signZ;

        x=10;
        y=-5;
        z=0;

        //x, y, z를 양수로 만듬
        //삼항연산자
        absX = x >= 0 ? x : -x;
        absY = y >= 0 ? y : -y;
        absZ = z >= 0 ? z : -z;

        //양수면 '+', 음수면 '-', 0은 ' '를 앞에 붙임
        //삼항연산자 중첩
        signX = x > 0 ? '+' : (x < 0 ? '-' : ' ');
        signY = y > 0 ? '+' : (y < 0 ? '-' : ' ');
        signZ = z > 0 ? '+' : (z < 0 ? '-' : ' ');

        System.out.println("signX+absX = " + signX + absX);
        System.out.println("signY+absY = " + signY + absY);
        System.out.println("signZ+absZ = " + signZ + absZ);
    }
}

복합 대입 연산자

i += 3; //i = i + 3;
i *= 10 + j; // i = i * (10 + j);
profile
안녕하세요!!

0개의 댓글