Java์ ์ ํ ์บ์คํ ์ด๋ ๋ฌด์์ ๋๊น?
๋ฐ์ดํฐ ์ ํ์ ์ํํ ์ ์๋ ์์ ๊ณผ ํจ๊ป ์ ์ฅํ ์ ์๋ ๊ฐ์ ์ ํ์ ์ง์ ํ๋ ๋ฏธ๋ฆฌ ์ ์๋ ๊ฐ ์งํฉ์ ๋๋ค.ํ์ ์บ์คํ ์ด ํ์ํ ์ด์ ๋ ๋ฌด์์ ๋๊น?
Java์๋ ๋ฉ๋ชจ๋ฆฌ์ ๋ค๋ฅธ ๊ณต๊ฐ์ด ํ์ํ ๋ค์ํ ๊ธฐ๋ณธ ๋ฐ์ดํฐ ์ ํ์ด ์์ต๋๋ค. ์ด๋ก ์ธํด ํ ๋ฐ์ดํฐ ์ ํ์ ๊ฐ์ ๋ค๋ฅธ ๋ฐ์ดํฐ ์ ํ์ ํ ๋นํ๋ ๋์ ํธํ์ฑ ๋ฌธ์ ๊ฐ ๋ฐ์ํ ์ ์์ต๋๋ค. ๋ฐ์ดํฐ ์ ํ์ด ์ด๋ฏธ ํธํ๋๋ ๊ฒฝ์ฐ ์ปดํ์ผ๋ฌ๊ฐ ์๋์ผ๋ก ์ ํ ๋ณํ์ ์ํํฉ๋๋ค. ๋ฐ๋ผ์ ํ์ ์บ์คํ ์ ํ๋ก๊ทธ๋จ์์ ๋ค์ํ ๋ฐ์ดํฐ ์ ํ์ ์ฒ๋ฆฌํ ๋ ์ฃผ์ ํธํ์ฑ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํฉ๋๋ค.Java ์ ํ ์บ์คํ ์ ์ ํ
Java์๋ ๋ ๊ฐ์ง ์ ํ์ ์ ํ ์บ์คํ ์ด ์์ต๋๋ค.- ํ๋ ์ ํ ์บ์คํ โ ์์์ ๋๋ ์๋ ์ ํ ์บ์คํ ์ด๋ผ๊ณ ๋ ํจ
- Narrowing Type Casting - ๋ช ์์ ๋๋ ์๋ ์ ํ ์บ์คํ ์ด๋ผ๊ณ ๋ ํจ
ํํญํ ์ฃผ์กฐ
ํ์ฅํ ํ๋ณํ(Widening typecasting)์ ์ด๋ฆ์์ ์ ์ ์๋ฏ์ด ์์ ๋ฐ์ดํฐ ์ ํ์ ๋ ํฐ ๋ฐ์ดํฐ ์ ํ์ผ๋ก ํ์ฅํ๋ ๊ฒ์ ์๋ฏธํฉ๋๋ค. ์์ ์ ํ์ ํฐ ์ ํ์ผ๋ก ๋ณํํ๋ ค๋ ๊ฒฝ์ฐ ์ด๋ฌํ ์ ํ ๋ณํ์ ์ํํฉ๋๋ค. ๋ฐ์ดํฐ ์ ํ์ ์๋ก ํธํ๋์ด์ผ ํฉ๋๋ค. ์ซ์์์ char ๋๋ ๋ถ์ธ ์ ํ์ผ๋ก์ ์์์ ๋ณํ์ ์์ต๋๋ค. Java์์๋ char ๋ฐ boolean ์ ํ์ด ํธํ๋์ง ์์ต๋๋ค.
๋ฐ์ดํธ -> ์งง์ -> ๋ฌธ์ -> int -> ๊ธด -> ๋ถ๋ ์์์ -> ๋๋ธ
์ด๋ฌํ ์ ํ์ ์บ์คํ
์ ์ ๋ณด ์์ค ์์ด ์ปดํ์ผ๋ฌ์ ์ํด ์๋์ผ๋ก ์ํ๋ฉ๋๋ค. ํ๋ก๊ทธ๋๋จธ์ ์ธ๋ถ ํธ๋ฆฌ๊ฑฐ๊ฐ ํ์ํ์ง ์์ต๋๋ค.
์
//Automatic type conversion
public class WideningExample {
public static void main(String[] args) {
int i = 100;
System.out.println("int value: " + i);
// int to long type
long l = i;
System.out.println("int to long value: " + l);
// long to float type
float f = l;
System.out.println("int to float value: " + f);
byte b = 1;
System.out.println("byte value: " + b);
// byte to int type
i = b;
System.out.println("byte to int value: " + i);
char c = 'a';
System.out.println("char value: " + c);
// char to int type
i = c;
// prints the ASCII value of the given character
// ASCII value of 'a' = 97
System.out.println("char to int value: " + i);
}
}
์ฐ์ถ
int ๊ฐ: 100 int์์ long ๊ฐ: 100 int์์ float ๊ฐ: 100.0 byte ๊ฐ: 1 byte์์ int ๊ฐ: 1 char ๊ฐ: char์์ int ๊ฐ: 97
์ค๋ช
์์ ์ฝ๋์์๋ ์ปดํ์ผ๋ฌ๊ฐ ์๋์ผ๋ก ์ํํ๋ ํ์ฅํ ํ๋ณํ์ ๋ณด์ฌ์ฃผ์์ต๋๋ค. ์ฐ์ int , byte ๋ฐ char ์ ๊ฐ์ ํ ๋นํ์ต๋๋ค . ๊ทธ๋ฐ ๋ค์ int ๊ฐ์ long ๋ฐ float ์ ํ ๋นํ๋๋ฐ ๋ ๋ค int ๋ณด๋ค ํฝ๋๋ค . ๋ํ byte ๋ฐ char ๊ฐ์ int ์ ํ ๋นํ์ต๋๋ค . byte ๋ฐ char ๋ ๋ชจ๋ int ๋ณด๋ค ์์ ๋ฐ์ดํฐ ์ ํ์ด๋ฏ๋ก ์ด๋ฌํ ๋ณํ์ ์์์ ์ด์์ต๋๋ค.ํ์ํ ์ฃผ์กฐ
์ด๋ฆ์์ ์ ์ ์๋ฏ์ด Narrowing typecasting์ ๋ ํฐ ๋ฐ์ดํฐ ์ ํ์ ๋ ์์ ๋ฐ์ดํฐ ์ ํ์ผ๋ก ์ขํ๋ ๊ฒ์ ์๋ฏธํฉ๋๋ค. ์ฐ๋ฆฌ๋ ํฐ ์ ํ์ ์์ ์ ํ์ผ๋ก ๋ณํํ๋ ค๊ณ ํ ๋ ์ด ์ ํ ๋ณํ์ ์ํํฉ๋๋ค.
double -> float -> long -> int -> char -> short -> ๋ฐ์ดํธ
์ด๋ฌํ ์ ํ์ ์บ์คํ
์ ๊ฒฝ์ฐ ์์ฒด ๋ณํ์ ์ง์ ํ์ฌ Java์ ๊ธฐ๋ณธ ๋ณํ์ ์ฌ์ ์ํฉ๋๋ค. ์ด๋ฅผ ๋ฌ์ฑํ๊ธฐ ์ํด ๋์ ๋ฐ์ดํฐ ์ ํ ์์ ์ ํ ๋ณํ์ด ํ์ํ ๋ณ์ ๋๋ ๊ฐ์ ๊ดํธ '()' ์์ ์์ฑํฉ๋๋ค. ๊ทธ๋ฌ๋ ์ด๋ฌํ ์ ํ์ ์ฃผ์กฐ๋ก ์ธํด ์ ๋ฐ๋๊ฐ ์์ค๋ ์ ์์ต๋๋ค.
์
//Manual Type Conversion
public class NarrowingExample {
public static void main(String[] arg) {
// double data type
double d = 97.04;
// Print statements
System.out.println("double value: " + d);
// Narrowing type casting from double to long
// implicitly writing the target data type in () followed by initial data type
long l = (long) d;
// fractional part lost - loss of precision
System.out.println("long value: " + l);
// Narrowing type casting from double to int
// implicitly writing the target data type in () followed by initial data type
int i = (int) l;
// fractional part lost - loss of precision
System.out.println("int value: " + i);
// Narrowing type casting from double to int
// implicitly writing the target data type in () followed by initial data type
char c = (char) i;
// displaying character corresponding to the ASCII value of 100
System.out.println("char value: " + c);
}
}
์ฐ์ถ
double ๊ฐ: 97.04 long ๊ฐ: 97 int ๊ฐ: 97 char ๊ฐ: a
GO TO FULL VERSION