*자동 타입 변환
말 그대로 자동으로 타입 변환이 일어나는것. 값의 허용 범위가 작은 타입이 허용 범위가 큰 타입으로 저장될때 발생
*기본 타입 허용 범위 크기순
byte < short < int < long < float < double
자동 타입 변환 <- 큰 허용 범위 타입 = 작은 허용 범위 타입ex)
byte byteValue = 10; int intValue = byteValue; // int가 byte보다 허용 범위가 더 크므로 자동 타입 변환됨
*정수 타입이 실수 타입으로 저장될 경우엔 무조건 자동 타입 변환이 일어난다.
실수 타입은 정수 타입보다 허용 범위가 크기 때문.
ex)
long longValue = 5000000000L;
float floatValue = longValue; // 5.0E9f로 저장됨
double doubleValue = longValue; // 5.0E9로 저장됨
*char 타입의 경우 int 타입으로 자동 타입 변환되면 유니코드 값이 int 타입에 저장됨
ex)
char charValue = 'A'; int intValue = charValue; // 65가 저장됨*예외적으로 char 타입보다 허용 범위가 작은 byte 타입은 char 타입으로 자동 타입 변환될수없다.
char 타입의 허용 범위는 음수를 포함하지않는데 byte 타입은 음수를 포함하기 때문.
ex)byte byteValue = 65; char charValue = byteValue; // 컴파일 에러
*강제 타입 변환
큰 타입을 작은 타입으로 강제로 나눠서 저장하는것. 캐스팅 연산자 괄호() 사용, 괄호 안에 들어가는 타입은 나누는 단위
강제 타입 변환 <- 작은 허용 범위 타입 = (작은 허용 범위 타입)큰 허용 범위 타입ex 1)
int intValue = 10; byte byteValue = (byte) intValue; // 강제 타입 변환ex 2)
int intValue = 65; char charValue = (char) intValue; charValue 출력 : Aex 3)
double doubleValue = 3.14; int intValue = (int) doubleValue; // intValue는 정수 부분인 3만 저장
*정수 연산에서의 자동 타입 변환
정수 타입 변수가 산술 연산식에서 피연산자로 사용되면 int 타입보다 작은 byte, short 타입의 변수는 int 타입으로 자동 타입 변환되어 연산을 수행
int result = byte / char / short / int 연산자(+,-,*,/,%) bte / char / short / int
ex 1) byte 타입 변수가 피연산자로 사용된 경우byte x = 10; byte y = 20; byte result = x + y; // 컴파일 에러 int result = x + y; -> byte 변수 x, y가 피연산자로 사용되면 int 타입으로 변환되어 연산되므로 연산 결과를 int 변수에 저장해야함ex 2) int 타입 변수가 피연산자로 사용된 경우
int x = 10; int y = 20; int result = x + y;ex 3) 정수 리터럴 10과 20을 덧셈 연산해서 결과를 byte 변수 result에 저장하는 코드
byte result = 10 + 20; -> 이 경우에는 피연산자가 변수가 아니므로 int 타입으로 변환을 하지 않는다
*정수 연산식에서 모든 변수가 int 타입으로 변환되는것은 아니고 두 피연산자 중 허용 범위가 큰 타입으로 변환되어 연산을 수행한다.
만약 int 타입보다 허용 범위가 더 큰 long 타입이 피연산자로 사용되면 다른 피연산자는 무조건 long 타입으로 변환하고 연산을 수행한다. 따라서 연산 결과를 long 타입 변수에 저장해야한다.
> long result = long 타입 연산자(+,-,*,/,%) byte / char / short / int
*실수 연산에서의 자동 타입 변환
실수 타입 변수가 산술 연산식에서 피연산자로 사용될 경우 두 피연산자가 동일한 타입이라면 해당 타입으로 연산되지만 피연산자 중 하나가 double 타입이라면 다른 피연산자도 double 타입으로 자동 타입 변환되어 연산을 수행한다. 따라서 연산 결과는 double 타입이 된다.
double result = 1.2f + 3.4; -> float 타입인 1.2f가 double 타입인 1.2로 자동 타입 변환 되어 연산된다.int 타입과 double 타입을 연산해도 동일하다.
int intValue = 10; double doubleValue = 5.5; double result = intValue + doubleValue; // result에 15.5가 저장됨 -> intValue가 double 타입으로 변환되어 연산 수행된다.만약 꼭 int 타입으로 연산을 해야한다면 double 타입을 int 타입으로 강제 변환하고 연산을 수행해야한다.
int intValue = 10; double doubleValue = 5.5; int result = intValue + (int) doubleValue; // result에 15가 저장됨
*자바에서는 소문자 f 또는 대문자 F가 없는 실수 리터럴을 double 타입으로 해석하므로 연산 결과는 double 타입 변수에 저장해야한다.
double result = 1.5 + 2.3;만약 float 타입 변수에 저장하면 컴파일 에러가 발생한다.
float result = 1.5 + 2.3; // 컴파일 에러 발생float 타입에 저장하고싶다면 실수 리터럴 뒤에 소문자 f나 대문자 F를 붙여 컴파일러가 float 타입임을 알도록 해야한다.
float result = 1.5f + 2.3f;
*기타
int x = 1; int y = 2; double result = x / y; syso(result);-> result는 0.5가 아니라 0.0 출력. 그 이유는 자바에서 정수 연산의 결과는 정수가 되기 때문. x / y의 연산 결과는 0.5가 아니라 0이 되고, 0을 double 타입 변수 result에 저장하므로 0.0이 됨
0.5가 되기 위해서는 x / y를 정수 연산이 아니라 실수 연산으로 변경해야함. x와 y 둘중 하나 또는 둘 모두를 double 타입으로 변환해야한다. 정수 타입을 실수 타입으로 변환하는 방법은 아래와 같다.float floatValue = (float) 정수; double doubleValue = (double) 정수;ex 1)
int x = 1; int y = 2; double result = (double) x / y;ex 2)
int x = 1; int y - 2; double result = x / (double) y;ex 3)
int x = 1; int y = 2; double result = (double) x / (double) y;
*+연산에서 문자열 자동 타입 변환
피연산자가 모두 숫자일 경우엔 덧셈 연산 수행, 피연산자 중 하나가 문자열일 경우에는 나머지 피연산자도 문자열로 자동 변환되어 문자열 결합 연산을 수행
ex)int value = 3 + 7; -> int value = 10; String str = "3" + 7; -> String str = "3" + "7"; -> String str = "37" String str = 3 + "7"; -> String str = "3" + :7"; -> String str = "37";int value = 1 + 2 + 3; -> int value = 3 + 3; -> int value = 6; String str = 1 + 2 + "3"; -> String str = 3 + "3"; -> String str = "33"; String str = 1 + "2" + 3; -> String str = "12" + 3; -> String str = "123"; String str = "1" + 2 + 3; -> String str = "12" + 3; -> String str = "123";만약 특정 부분을 우선 연산하려면 해당 부분을 괄호 ( )로 감싸주면 된다.
String str = "1" + (2 + 3); -> String str = "1" + 5; -> String str = "15";
*문자열을 기본 타입으로 강제 타입 변환
1) String -> byte
String str = "10"; byte value = Byte.parseByte(str);2) String -> short
String str = "200"; short value = Short.parseShort(str);3) String -> int
String str = "300000"; int value = Integer.parseInt(str);4) String -> long
String str = "40000000000"; long value = Long.parseLong(str);5) String -> float
String str = "12.345"; float value = Float.parseFloat(str);6) String -> double
String str = "12.345"; double value = Double.parseDouble(str);7) String -> boolean
String str = "true"; boolean value = Boolean.parseBoolean(str);만약 문자열이 숫자가 아닌 알파벳이나 특수문자, 한글 등을 포함하고 있을 경우 숫자 타입으로 변환을 시도하면 숫자 형식 예외(NumberFormatException) 발생.
String str = "1a"; int value = Integer.parseInt(str); // NumberFormatException 예외 발생반대로 기본 타입(byte, short, char, int, long, float, double, boolean)의 값을 문자열로 변경하는 것은 String.valueOf( ) 메서드 쓰면 됨.
String str = String.valueOf(기본타입값); String str = String.valueOf(3) -> 문자열 "3"