JAVA

큰 변수 데이터 타입으로 자동형변환 정리

연93 2022. 9. 5. 14:40
byte byteValue1 = 10;
		byte byteValue2 = 20;
		
		//byte변수끼리의 합의 결과를 int로 해석하도록 되어있다.
		//byte byteValue3 = 10 + 20;
		
		int intValue1 = byteValue1 + byteValue2;
		System.out.println(intValue1);
		
		//아스키코드 < 유니코드
		// 0~65535범위
		char charValue1 = 'A'; // 내부적으로 숫자로 저장된다. 아스키코드표 65 소문자 a 92
		char charValue2 = 1;
		int intValue2 = charValue1 + charValue2;
		System.out.println("유니코드값: " + intValue2);
		System.out.println("출력문자=:" + (char) charValue2);
		
		/*
		 변수로 사용시 (int형 아래 데이터 타입끼리 연산시에는 결과가 int형으로 해석된다)
		 byte변수 + byte변수 -> int
		 char변수 + char변수 -> int	 
		 byte변수 + short변수 -> int
		 
		 
		 int + long -> long + long = long
		 int + float -> float + float = float
		 float + double -> double + double = doule
		 
		 byte + int + long -> long + long + long
		 
		 
		 */
		
		//int이하 데이터타입 끼리의 연산 결과는 int
		// 정수의 기본형 int사용 실수는 double형사용
		byte b = 10;
		short s = 20;
		short s2 = b + s; //변환해줘야한다
		
		// 정수끼리의 연산결과는 정수
		int intValue3 = 10;
		int intValue4 = intValue3 / 4;
		System.out.println(intValue4);
		
		// 다른데이터타입끼리 연산시 가장 큰데이터 타입으로 형변환이 발생된다.
		// 연산시에도 타입이 일치되는 작업이 이루어져야 한다.
		int intValue5 = 10;
		//int intValue6 = 10 / 4.0;
		double doubleValue = 10 / 4.0;
		System.out.println(doubleValue);
		
		/*
		 10 / 4.0 -> 10.0 / 4.04
		 10.0 / 4.0
		 */