Day01. 자동형변환
public class CastingTest {
public static void main(String[] args) {
byte b = 127;
int i = /*(int)*/b; //자동형변환(모든 값을 저장)
System.out.println("i=" + 1);
double d = i; //자동형변환
System.out.println("d=" + d);
double d2 = 3.14;
int i2 = (int)d2;//명시적형변환(일부값 손실)
System.out.println("i2=" + i2);
int i3 = 128;
byte b3 = (byte)i3;//명시적형변환
System.out.println("b3=" + b3);
char c4 = 'a';
int i4 = c4;//자동형변환(문자 -> 숫자)
System.out.println("c4=" + c4 +" , i4= " + i4);
char c5 = 'A';
int i5 = c5;//자동형변환(문자 -> 숫자)
System.out.println("c5=" + c5 +" , i5= " + i5);
//소문자(97)를 대문자(65)로 변경
System.out.println( (char)(i4 - 32)); //97-32 = 65(A)
/*int i6 = 0;
boolean b6 = (int)i6;*/ //boolean값은 true와 false만 가져갈수있다.
}
}
*클래스명을 Casting Test로 명시한 후, 데이터 타입에 맞게 선언해주었다.
우선 순서는 byte(1) < short(2) < int(4) < long(8) < float(4) < double(8)
이해를 돕자면
byte b = 127;
int i = /*(int)*/b; //자동형변환(모든 값을 저장)
System.out.println("i=" + 1);
double d = i; //자동형변환
System.out.println("d=" + d);
byte b는 127의 수를 갖고 있다 형 변환이된다면 i에 127이 들어가게 된다
하지만 dobule의 형변환순서는 뒤에있기 때문에 i는 127.0이 결과값에 나오게 된다.