캐스팅

TIL·2022년 12월 9일
0

Java (최은빈)

목록 보기
4/27
package me.day03.casting;

public class DataTypeCastingExample {
    public static void main(String[] args) {

        //// type casting (명시적 형변환) ////
        // 2byte (0 ~ 2^16-1 (문자 이므로 0부터 시작)) -> 1byte (-128 ~ 127)
        // 00000000 00000000 -> 00000000
        char ch = 'A';
//        byte b = ch; // 에러
        byte b = (byte) ch;

        // 데이터 손실 방지
        int i = 128;
        if ((i < Byte.MIN_VALUE) || (i > Byte.MAX_VALUE)) { // -128 ~ 127 (bite의 유효범위)
            System.out.println("byte 타입으로 변환할 수 없음");
            System.out.println("값 다시 확인 바람");
        } else {
            byte i2 = (byte) i; // byte(1) <- int(4) 이지만 유효범위 내부이므로 데이터 손상X, 명시적 형변환 ok
            System.out.println(i2);
        }

        // 정밀도 손실 방지
        int num1 = 123456780;
        int num2 = 123456780;

        float num3 = num2; // 4 -> 8
        num2 = (int) num3; // 8 -> 4
        System.out.println(num3); // 1.23456784E8
        System.out.println(num2); // 123456784

        int result = num1 - num2;
        System.out.println(result); // -4 // 데이터가 손상되었다


        //// promotion (자동 형변환) ////
        // int(4) <- byte(1)
        byte byteValue = 10;
        int intValue = byteValue;
//        char charValue = byteValue; // 컴파일 에러
        System.out.println(intValue); // 00001010 -> 00000000 00000000 00000000 00001010

        // long(8) <- int(4)
        intValue = 500;
        long longValue = intValue;
        System.out.println(longValue);

        // double(8) <- int(4)
        double doubleValue = intValue;
        System.out.println(doubleValue); // 500.0

        // int(4) <- char(2)
        char charValue = '가';
        intValue = charValue;
        System.out.println(intValue); // 유니코드 값(숫자) 저장됨

        // double(8) <- int(4)
        intValue = 200;
        double doubleValue1 = intValue;
        System.out.println(doubleValue1);


        //// 연산식의 타입 변환 ////
        // 연산은 기본적으로 같은 타입의 피연산자 (operand) 간에 수행

        // 서로 다른 타입의 피연산자가 있을 경우 두 피연산자 중 크기가 큰 타입으로 자동 변환된 후 연산 수행
        // 문자열 + 문자 도 프로모션 된 것
        System.out.println("ch = " + ch); // soutv

        // 자바는 숫자를 더 큰 문자열로 자동 변환 (파이썬은 컴파일에러)
        //"a" + 1 == "a" + "1" == "a1" // 1 -> "1" 로 자동 변환

        // 크기가 4byte 보다 작은 타입은 int로 변환된 후 연산 수행 (자바는 정수 연산 int 기본)
        // [byte, char, short, int] 연산자 [byte, char, short, int] = int
        // 정수형 -> 이진수 -> int 에 저장 (JVM)

        // byte + byte = int -> long, double
        byte byteValue1 = 10;
        byte byteValue2 = 20;
//        byte byteValue3 = byteValue1 + byteValue2; // 컴파일 에러
        int intValue1 = byteValue1 + byteValue2;
        long longValue1 = byteValue1 + byteValue2; // int -> long, double 프로모션
        System.out.println(intValue1);
        System.out.println(longValue1);

        // char + char = int
        char charValue1 = 'A'; // 아스키코드 값을 저장
        char charValue2 = 1;
//        char charValue3 = charValue1 + charValue2; // 컴파일 에러
        int intValue2 = charValue1 + charValue2;
        System.out.println(intValue2); // 66
        System.out.println((char)intValue2); // B

        // 정수와 정수의 연산은 정수 (나눗셈의 결과는 몫)
        int intValue3 = 10;
        int intValue4 = intValue3 / 4; // 2.5 아닌 2 (몫)
        System.out.println(intValue4);

        int intValue5 = 10;
        // int intValue6 = 10 / 4.0 // 컴파일 에러
        double doubleValue2 = intValue5 / 4.0; // 10 -> 10.0 (더 큰 데이터 타입으로 자동 변환)
        System.out.println(doubleValue2); // 2.5 (int에 저장할 수 없다)

        // long + [long 이하의 자료형] = long
        // double - [double 이하의 자료형] = double
    }
}

0개의 댓글

관련 채용 정보