백엔드 과정/자바 예습

[혼공자]Chap 02-3 타입 변환

mim 2021. 12. 14. 12:36
반응형

 

 

타입 변환이란 데이터 타입을 다른 데이터 타입으로 변환하는 것을 말한다.

자동 타입 변환 promotion 은 값의 허용 범위가 작은 타입이 허용 범위가 큰 타입으로 저장될 때 발생한다.

  • 허용범위 크기순 : byte < short < int < long < float < double

// 큰 허용 범위 타입은 작은 허용 범위 타입으로 자동 타입 변환이 될 수 없다.

강제 타입 변환 casting 은 큰 허용 범위타입을 작은 허용 범위 타입으로 강제로 나눠서 저장하는 것을 말한다.

  • exam 1) promotion
    package sec02.exam03;
    
    public class PromotionExample {
    	public static void main(String[] args) {
    		//자동 타입 변환
    		byte byteValue = 10;
    		int intValue = byteValue;
    		System.out.println("intValue:" + intValue);
    		
    		char charValue = '가';
    		intValue = charValue;
    		System.out.println("가의 유니코드는:" + intValue); //char 타입의 경우 int 타입으로 자동 타입 변환되면 유니코드 값이 int 타입에 저장된다.
    
    		intValue = 50;
    		long longValue = intValue;
    		System.out.println("longValue:" + longValue);
    		
    		longValue = 100;
    		float floatValue = longValue ;
    		System.out.println("floatValue:" + floatValue);
    		
    		floatValue = 100.5f;
    		double doubleValue = floatValue;
    		System.out.println("doubleValue:" + doubleValue);
    	}
    }
  • exam2 ) 캐스팅 casting
    package sec02.exam03;
    
    public class CastingExample {
    	public static void main(String[] args) {
    		//강제 타입변환
    		int intValue = 44032;
    		char charValue = (char)intValue;
    		System.out.println(charValue);
    		
    		long longValue = 500;
    		intValue = (int)longValue;
    		System.out.println(intValue);
    		
    		double doubleValue = 3.14;
    		intValue = (int)doubleValue;
    		System.out.println(intValue);
    	}
    }

정수 연산에서의 자동 타입변환

정수 타입 변수가 산술 연산식에서 피연산자로 사용되면 int 타입보다 작은 byte, short 타입의 변수는 int타입으로 자동 타입변환 되어 연산을 수행한다.

  • exam 3) 정수 타입의 연산
    package sec02.exam03;
    
    public class ByteOperahionExample {
    	public static void main(String[] args) {
    		byte result1 = 10 + 20;
    		System.out.println(result1);
    		
    		byte x = 10;
    		byte y = 20; //처음부터 int 타입으로 선언하는 것이 실행 성능을 향상시킨다.
    		int result2 = x + y ; // int 타입으로 변환되어 연산결과를 저장
    		System.out.println(result2);
    	}
    }
    정수 연산식에서 모든 변수가 int 타입으로 변환되는 것은 아니다. 두 피연산자 중 허용 범위가 큰 타입으로 변환되어 연산을 수행한다.

실수 연산에서의 자동 타입 변환

exam 4) 실수 타입의 연산

package sec02.exam03;

public class OperationPromotionExample {
	public static void main(String[] args) {
		byte byteValue1 = 10;
		byte byteValue2 = 20;
		//byte byteValue3 = byteValue1 + byteValue2; <-컴파일 에러
		int intValue1 = byteValue1 + byteValue2;
		System.out.println(intValue1);

		char charValue1 = 'a';
		char charValue2 = 1;
		//char charValue3 =charValue1 + charValue2; <-컴파일 에러
		int intValue2 = charValue1+ charValue2;
		System.out.println("유니코드=" + intValue2);
		System.out.println("출력문자=" + (char)intValue2);
		
		int intValue3 = 10;
		int intValue4 = intValue3/4;
		System.out.println(intValue4);
		
		int intValue5 = 10;
		//int intValue = 10/4.0 ; <-컴파일 에러
		double doubleValue = intValue5 /4.0;
		System.out.println(doubleValue);
		
		int x =1;
		int y =2;
		double result = (double) x/y; // 실수값 0.5를 출력하기 위해서는 double 타입으로 변환해야 한다.
		System.out.println(result);
	}
}

30

유니코드=98

출력문자=b

2

2.5

0.5

 

연산에서의 문자열 자동 타입변환

  • exam 5) 문자열 결합 연산
    package sec02.exam03;
    
    public class StringConcatExample {
    	public static void main(String[] args) {
    		//숫자 연산
    		int value = 10 + 2 + 8 ;
    		System.out.println(value);
    		
    		//문자열 결합
    		String str1 = 10 + 2 + "8" ;
    		System.out.println("str1:" + str1);
    		
    		String str2 = 10 + "2" + 8 ;
    		System.out.println("str2:" + str2);
    		
    		String str3 = "10" + 2 + 8 ;
    		System.out.println("str3:" + str3);
    		
    		String str4 = "10" + (2+8);
    		System.out.println("str4:" + str4);
    	}
    }

문자열을 기본타입으로 강제 타입변환

문자열과 + 연산을 하면 다른 피연산자도 문자열로 변환되어 문자열 결합이 일어난다.

  • exam 6) 기본타입과 문자열 간의 변환문자열을 기본타입으로 변경
  • String str = "a"; int value = Integer.paresInt (str) ;
  • 기본타입을 문자열로 변경 String str = String.valueOf(기본타입값);
package sec02.exam03;

public class PrimitiveAndStringConversionExample {

	public static void main(String[] args) {
		int value1 =Integer.parseInt("10"); //문자열을 기본타입으로 변경
		double value2 = Double.parseDouble("3.14");
		boolean value3 = Boolean.parseBoolean("true");
		
		System.out.println("value1:" + value1);
		System.out.println("value2:" + value2);
		System.out.println("value3:" + value3);
		
		String str1 = String.valueOf(10); //기본타입을 문자열로 변경
		String str2 = String.valueOf(3.14);
		String str3 = String.valueOf(true);
		
		System.out.println("str1:"+str1);
		System.out.println("str2:"+str2);
		System.out.println("str3:"+str3);
	}
}
 

value1:10 value2:3.14 value3:true str1:10 str2:3.14 str3:true

 

반응형