Thu, 21st Oct 2021
package sec03.exam01_promotion;
public class PromotionExample {
public static void main(String[] args) {
byte byteValue = 10;
int intValue = byteValue;
System.out.println(intValue);
char charValue = '가'; //char = 2byte
//int intValue = charValue //오류 발생 : intValue는 위에 이미 선언되었으므로 duplicated 변수
intValue = charValue; //int = 4byte (2byte -> 4byte 묵시적 형 변환)
System.out.println("가의 유니코드 = " + intValue); //가의 유니코드 = 44032
intValue = 500; //int = 4byte
long longValue = intValue; //long = 8byte (4byte -> 8byte 묵시적 형 변환)
System.out.println(longValue); //500
intValue = 200; //int = 4byte
double doubleValue = intValue; //double = 8byte (4byte -> 8byte 묵시적 형 변환)
System.out.println(doubleValue); //200.0
}
}
package sec03.exam02_casting;
public class CastingExample {
public static void main(String[] args) {
int intValue = 44032; //int = 4byte
//char charValue = intValue; //char = 2byte, int = 4yte (4byte -> 2byte 묵시적 형 변환 불가)
char charValue = (char) intValue; //Type Casting : 명시적 형 변환
System.out.println(charValue); //가
long longValue = 500; //long = 8byte
//intValue = longValue; //int = 4byte, long = 8byte (8byte -> 4byte 묵시적 형 변환 불가)
intValue = (int) longValue; //casting
System.out.println(intValue); //500
double doubleValue = 3.44; //double = 8byte
//intValue = doubleValue; //int(정수) = 4byte, double(실수) = 8byte (8byte(실수) -> 4byte(정수) 묵시적 형 변환 불가)
intValue = (int) doubleValue; //casting
System.out.println(intValue); //3 : 실수->정수 변환이므로 소수점 아래 값은 출력되지 않음
double doubleValue2 = intValue; //정수(int = 4byte)->실수(double = 8byte) : 묵시적 형 변환 가능
System.out.println(doubleValue2); //3.0
}
}
package sec03.exam02_casting;
public class CheckValueBeforeCasting {
public static void main(String[] args) {
//int 타입의 intValue를 byte 타입으로 casting 전에, casting이 가능 여부를 확인해보는 예제
int intValue = 120; //int = 4byte
if((intValue < Byte.MIN_VALUE) || (intValue > Byte.MAX_VALUE)) {
//|| : or 연산자
//Byte.MIN_VALUE : byte의 최소값 : -128
//Byte.MAX_VALUE : byte의 최대값 : 127
//intValue가 byte의 최소값보다 작거나 최대값보다 크다면 casting 불가
System.out.println("byte 타입으로 변환할 수 없습니다.");
System.out.println("값을 다시 확인해주세요.");
} else {
byte byteValue = (byte) intValue; //casting
System.out.println(byteValue); //120
}
}
}
package sec03.exam03_accuracy;
public class FromIntToDouble {
public static void main(String[] args) {
int num1 = 123456780; //비교를 위한 기준값
int num2 = 123456780; //조작할 값
double num3 = num2;
//int : 4byte(32 bit) -> double(실수)의 가수부 : 52 bit
//덜 정밀 -> 더 정밀 : 손실없이 변환됨
System.out.println(num3); //1.2345678E8
num2 = (int) num3;
System.out.println(num2); //123456780 : 손실없이 변환됨
int result = num1 - num2;
System.out.println(result);//0
}
}
package sec03.exam03_accuracy;
public class FromIntToFloat {
public static void main(String[] args) {
int num1 = 123456780; //비교를 위한 기준값
int num2 = 123456780; //조작할 값
float num3 = num2; //int(정수) : 4byte(32 bit) -> float(실수)의 가수부 : 23bit
System.out.println(num3); //1.23456784E8
num2 = (int) num3;
System.out.println(num2); //123456784 : float의 23bit로 num2를 표현 불가 -> 근사치로 변환됨 -> 변환 시 손실 발생
int result = num2 - num1;
System.out.println(result); //4 : 근사치로 변환되어 0이 아님
}
}
package sec03.exam04_operation;
public class OperationPromotionExample {
public static void main(String[] args) {
byte byteValue1 = 10;
byte byteValue2 = 20;
//byte byteValue3 = byteValue1 + byteValue2; //플러스 연산시 int 타입으로 자동변환되어 결과값을 byte 타입으로 불러올 수 없음
int intValue1 = byteValue1 + byteValue2;
System.out.println(intValue1); //30
char charValue1 = 'A'; //아스키코드값 : 65
char charValue2 = 1;
//char charValue3 = charValue1 + charValue2; //플러스 연산시 int 타입으로 자동변환되어 결과값을 char 타입으로 불러올 수 없음
int intValue2 = charValue1 + charValue2;
System.out.println("유니코드= " + intValue2); //66
System.out.println("출력문자= " + (char) intValue2); //B
int intValue3 = 10;
int intValue4 = intValue3 / 4;
System.out.println(intValue4); //2 (int 타입)
System.out.println((double) intValue4); //2.0 (double 타입)
int intValue5 = 10;
//int intValue6 = intValue5 / 4.0; //에러 발생 : 4.0이 double 값이므로 int 타입 변수에 대입될 수 없음
double doubleValue = intValue5 / 4.0;
System.out.println(doubleValue); //2.5
}
}