자바 Primitive Type의 conversion에 대한 정리
너부리 큰형님 게시판에 보니 이와 관련된 질문이 보이더군요..
그래서 정리해봤습니다.
중요한 것은 Narrowing Casting인지.. Widening Casting 인지에 대한 것이겠죠..
그에 따른 Truncation Error, Explicit Casting 등.. 기본적으로 알아야 하는 것이 있겠구요..
그리고 연산자에 따른 리턴 타입을 정확히 알고 있어야겠죠..
산술, 증가, 감소, 비트와이즈, 시프트 연산자의 경우는 double < float < long < int 로 결과를 낸다 (shor, char, byte일지라도 int로 리턴)
비교, 등가 (equality), 부울 연산자는 항상 boolean 타입 리턴
대입 연산자는 대입 값과 같은 타입
조건 연산자는 두번째 또는 세번째 값의 타입 (두가지의 타입이 같아야 한다)
그냥 정리해봤습니다. 참고하시길..
package kr.pe.seveny.java.lang.TypeConversion;
/**
* 자바 Primitive Type의 conversion에 대한 정리
*
* @author 세브니
* @date 2003년 3월 17일 월요일
*
*/
public class FirstTypeConversion {
public static void main(String[] args) {
double quotient;
int x = 6;
int y = 10;
// 1
quotient = x / y;
System.out.println(quotient);
// 우리가 원하는 답은 0.6인데.. 위와 같이 하면 0.0이 나온다.
// 그 이유는 primitive type의 conversion의 문제인데..
// x의 타입이 int이고 y의 타입이 int이다.
// (int) x / (int) y 의 타입은 double이 되고..
// / (division) operator에 적용된 operand 중에서 최대 scale은 int 이므로
// /의 결과는 int가 된다. 따라서 값은 0.0이 된다.
// 그 값이 double quotient에 대입되고..
// 결과는 0.0이 된다.
// 2
quotient = (double) (x / y);
System.out.println(quotient);
// 위와 같이 하여도 답은 처음과 같은 0.0이 된다.
// 사실은 1이 내부적으로 2가 된다.
// 0.6을 구하기 위해서는 / 연산자의 최대 scale을
// double에 맞추는 방법이 가장 쉬운 방법이며
// 따라서 다음과 같이 수정하면 쉽게 된다.
quotient = (double) x / y;
System.out.println(quotient);
quotient = x / (double) y;
System.out.println(quotient);
// primitive type에 대한 casting은 다음의 표와 같이 이루어진다.
// 여기서 N은 변환이 이루어질수 없다는 표시이다 (에러 발생)
// C는 narrowing casting을 의미하며 명시적으로 캐스팅을 해야 한다.
// Y는 widening casting을 의미하며
// Y*는 widening casting 이지만, 정수에서 실수형으로 변환됨에 따른
// 유효숫자 표시 방법 차이에 따라 하위 비트 몇자리를 잃을수도 있음을 의미한다.
// 변환은 좌측의 타입에서 위쪽 타입으로의 변환을 의미한다.
// boolean byte short char int long float double
// boolean - N N N N N N N
// byte N - Y C Y Y Y Y
// short N C - C Y Y Y Y
// char N C C - Y Y Y Y
// int N C C C - Y Y* Y
// long N C C C C - Y* Y*
// float N C C C C C - Y
// double N C C C C C C -
// 참고문헌 : Java In A Nutshell 개정 3판 (한빛미디어 2000)
boolean b;
byte byt;
short shor;
char cha;
int in;
long lon;
float floa;
double doubl;
b = true;
byt = b; // Type mismatch: cannot convert from boolean to byte
shor = b; // Type mismatch: cannot convert from boolean to short
cha = b; // Type mismatch: cannot convert from boolean to char
in = b; // Type mismatch: cannot convert from boolean to int
lon = b; // Type mismatch: cannot convert from boolean to long
floa = b; // Type mismatch: cannot convert from boolean to float
doubl = b; // Type mismatch: cannot convert from boolean to double
byt = 1;
b = byt; // Type mismatch: cannot convert from byte to boolean
shor = byt;
cha = byt; // Type mismatch: cannot convert from byte to char
cha = (char) byt; // C이므로 명시적 캐스팅을 해 주어야 한다.
in = byt;
lon = byt;
floa = byt;
doubl = byt;
shor = 1;
b = shor; // Type mismatch: cannot convert from short to boolean
byt = shor; // Type mismatch: cannot convert from short to byte
byt = (byte) shor; // C이므로 명시적 캐스팅을 해 주어야 한다.
cha = shor; // Type mismatch: cannot convert from short to char
cha = (char) shor; // C이므로 명시적 캐스팅을 해 주어야 한다.
in = shor;
lon = shor;
floa = shor;
doubl = shor;
cha = 'a';
b = cha; // cannot convert from char to boolean
byt = cha; // cannot convert from char to byte
byt = (byte) cha; // C이므로 명시적 캐스팅을 해 주어야 한다.
shor = cha; // cannot convert from char to shor
shor = (short) cha; // C이므로 명시적 캐스팅을 해 주어야 한다.
in = cha;
lon = cha;
floa = cha;
doubl = cha;
in = -322158956; // integer의 값이 크므로 narrowing casting에서 truncation error가 발생한다.
b = in; // cannot convert from int to boolean
byt = in; // cannot convert from int to byte
byt = (byte) in; // C이므로 명시적 캐스팅을 해 주어야 한다. truncation error
shor = in; // cannot convert from int shor
shor = (short) in; // C이므로 명시적 캐스팅을 해 주어야 한다. truncation error
cha = in; // cannot convert from int to char
cha = (char) in; // C이므로 명시적 캐스팅을 해 주어야 한다. truncation error
lon = in;
floa = in;
System.out.println(in + " --> " + floa + " (" + (int)floa + ")");
doubl = in;
lon = -2122156879; // long의 값이 크므로 narrowing casting에서 truncation error가 발생한다.
b = lon; // cannot convert from long to boolean
byt = lon; // cannot convert from long to byte
byt = (byte) lon; // C이므로 명시적 캐스팅을 해 주어야 한다.
shor = lon; // cannot convert from long shor
shor = (short) lon; // C이므로 명시적 캐스팅을 해 주어야 한다.
cha = lon; // cannot convert from long to char
cha = (char) lon;
in = lon; // cannot convert from long to int
in = (int)lon;
floa = lon;
System.out.println(lon + " --> " + floa + " (" + (long)floa + ")");
doubl = lon;
// double 변환에 따른 Y*를 찾을려고 했더니 쉽지 않네요 ^^;;
System.out.println(lon + " --> " + doubl + " (" + (long)doubl + ")");
floa = 32000.890f; // float 값이 크므로 narrowing casting에서 truncation error가 발생한다.
b = floa; // cannot convert from float to boolean
byt = floa; // cannot convert from float to byte
byt = (byte) floa; // C이므로 명시적 캐스팅을 해 주어야 한다.
shor = floa; // cannot convert from float shor
shor = (short) floa; // C이므로 명시적 캐스팅을 해 주어야 한다.
cha = floa; // cannot convert from float to char
cha = (char) floa;
in = floa; // cannot convert from float to int
in = (int) floa;
lon = floa; // cannot convert from float to long
lon = (long) floa;
doubl = floa;
doubl = 3200000.12000d; // double 값이 크므로 narrowing casting에서 truncation error가 발생한다.
b = doubl; // cannot convert from double to boolean
byt = doubl; // cannot convert from double to byte
byt = (byte) doubl; // C이므로 명시적 캐스팅을 해 주어야 한다.
shor = doubl; // cannot convert from double shor
shor = (short) doubl; // C이므로 명시적 캐스팅을 해 주어야 한다.
cha = doubl; // cannot convert from double to char
cha = (char) doubl; // C이므로 명시적 캐스팅을 해 주어야 한다.
in = doubl; // cannot convert from double to char
in = (int) doubl; // C이므로 명시적 캐스팅을 해 주어야 한다.
lon = doubl; // cannot convert from double to char
lon = (long) doubl; // C이므로 명시적 캐스팅을 해 주어야 한다.
floa = doubl; // cannot convert from double to char
floa = (float) doubl; // C이므로 명시적 캐스팅을 해 주어야 한다.
}
}