JAVA๊ฐ ๊ฐ์ง๋ ์ฌ๋ฌ ํ์ ๋ํด ์์๋ณด์
CAST์ PROMOTION
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | package pack; public class Test1 { public static void main(String[] ar){ System.out.println("์ฑ๊ณต"); System.out.println("๋จ์ถํค๋ฅผ ์ฐ์ตํ์"); System.out.println(); System.out.print("์๋ฐ"); System.out.print("๋ณ์"); System.out.println("์ฐ์ต"); //ํ ์ค ์ฃผ์ System.out.println("ํ ์ค");//์ฌ๊ธฐ์๋ถํฐ ํ์ ๋๊น์ง ์ฃผ์ /* ์ฌ๋ฌ์ค ์ฃผ์ */ //์์๋ฅผ ๊ธฐ์ตํ๊ณ ์ ๊ธฐ์ต์ฅ์๋ฅผ ํ๋ณดํ๊ณ ๊ธฐ์ต์ฅ์์ ์ด๋ฆ์ ๋ถ์ฌ: ๋ณ์ //๊ธฐ๋ณธํ bcbsilfd - ์์๋ฅผ ๊ธฐ์ตํ๋ ์๋ฃํ boolean/int/float๋ฅผ ๋ง์ด ์ด๋ค. //String : ๋ฌธ์์ด ๊ธฐ์ต์ฅ์ : ๊ธฐ๋ณธํ์ฒ๋ผ ์ฌ์ฉํ๋, ์ฌ์ค์ ๊ฐ์ฒด ๋ณ์ //์ฐธ์กฐํ : ๊ฐ์ฒด ๋ณ์ byte var1; //๋ณ์๋ฅผ ์ฌ์ฉํ๋ ๋ฒ <<๋ณ์ ์ ์ธ ํ์(type) : ํ ๋ณ์๋ช
, 1byte>> //Double์ (-128 ~ 127๊น์ง ๊ธฐ์ต) var1 = 10; var1 = 5; System.out.println(var1); //์ด method๋ฅผ ํตํด ์ถ๋ ฅํ๋ค. short var2 = 10000; // 32767์ด์์ด ๋์ด๊ฐ๋ฉด ์๋ฌ 2bytes ๊ธฐ์ต์ฅ์ 2^15 System.out.println(var2); int var3 = 2147483647; //2147483647๊น์ง 4bytes ๊ธฐ์ต์ฅ์ 2^31 System.out.println(var3); long var4 = 2147483648L; /* 8bytes ํ๋ณด - 2^63 ์ผ๋ฐ์ ์ธ ์ซ์๋ ๋ชจ๋ int (interger : ์ ์)ํ์ด๊ณ 2147483647 ์ด์์ ์๋ ์ซ์๋ก ๋ณด์ง ์๊ธฐ ๋๋ฌธ์ ์๋ฌ๊ฐ ๋๋ค.๊ทธ๋์ ๋์ L์ ์ฐ๋ฉด int๊ฐ ์๋๊ณ longํ ๊ฐ์ด ๋๋ค.*/ System.out.println(var4); long var5 = 10L; System.out.println(var5); //promotion : ์๋ ํ๋ณํ, cast : ๊ฐ์ ํ๋ณํ byte b1 = (byte)128; //๊ฐ์ ์ ์ผ๋ก 1๋ฐ์ดํธ์ ์ ์ฅ๊ณต๊ฐ์ ์ ์ฅ๊ณต๊ฐ ์ด์์ ๊ฐ์ ์ฃผ๋ฉด ๊ฒฐ๊ณผ ๊ฐ์ด ์ด์ํ๊ฒ ๋์จ๋ค (-128) byte b2 = (byte)12;//์์น์ ์ผ๋ก๋ ์ด๊ฒ ๋ง๋ค ๊ทธ๋ฌ๋ ์ค์ ๋ก๋ (byte)๋ฅผ ํ๋ก๊ทธ๋จ์์ ์๋์ผ๋ก promoteํด์ค๋ค. ๊ทธ๋์ ์ํด๋ ๋๋ค. byte b3 = b2; System.out.println(b1); System.out.println(b2); System.out.println(b3); short s1 = 10; // = (short)10; 2byte๊ฐ 4bytes์ ๋ค์ด๊ฐ๋๊ฒ์ promotion์ ๊ฐ๋
int i1 = s1; System.out.println(i1); short s2 = (short)i1; // 4bytes์ ๊ฐ์ด 2bytes ๊ฐ์ด ๋ค์ด๊ฐ ์ ์์ง๋ง ๊ฐ์ ๋ก ๋ฃ์ด์ฃผ๋ ๊ฒ์ด casting ๊ฐ๋
. (์๋ชป๋ ๊ฐ์ด ๋์ฌ์ ์์ง๋ง ๋์ด๊ฐ) System.out.println(s1); System.out.println("์ค์์ฒ๋ฆฌ-------"); double d1 = 10.0; d1 = 5; //์ค์ํ์ ์ ์ํ ์์ ๋กญ๊ฒ ๋ค์ด๊ฐ ์ ์์ (promotion) System.out.println(d1); d1 = 56.7; float f1 = 3.5F;//๊ธฐ๋ณธ์ ์ผ๋ก ์ ์๋ int ์ค์๋ double, 3.5๋ฅผ float๋ก ์ ์ฅ ์์ผ๋ผ f1 = (float)3.5; //doubleํ์ float ๊ธฐ์ต์ฅ์์ ๊ฐ์ ๋ก ๊ธฐ์ต์์ผ๋ผ System.out.println(f1); int i2 = (int)3.5; //๋ฒ๋ฆผ - ๊ฒฐ๊ณผ๊ฐ์ด 3์ผ๋ก ๋์ด System.out.println(i2); //์ฐ์ฐ ์ ํฐ ํ์
์ผ๋ก ์๋ ๋ณํ๋จ //double result = 4.5 * 10; //int 10์ด ๋ ํฐ double 4.5์ ๊ฐ์ด double๋ก ๋ณํ์ด ๋์ด double์ ๊ฐ์ ๊ณ์ฐ ์ํ //int result = (int)4.5 * 10; double dd = 5.5; int result = (int)4.5 * (int)dd; //๊ฒฐ๊ณผ๋ 4.5=> 4 * 5.5=> 5 ๊ทธ๋์ 20์ด ๊ฒฐ๊ณผ๋ก ๋์จ๋ค. System.out.println("result : " + result); System.out.println(); boolean bu1 = true; bu1 = false; System.out.println(bu1); System.out.println(); char c1 = 'a'; // c1 = 'abc' ๋ฌธ์์ด์ ๋ชป๋ค์ด๊ฐ // c1 = "a;" ๋ฌธ์์ด๋ง ํฐ๋ฐ์ดํ์ ๋ค์ด๊ฐ ์ ์์. ๋ฌธ์๋ ''๋ง ์ฌ์ฉ๊ฐ๋ฅ System.out.println(c1 + " " + (int)c1 + " " + (char)97); //a๋ ์์คํค ์ฝ๋ ๊ฐ์ผ๋ก 97์์ผ๋ก ๊ฒฐ๊ณผ๋ a 97๋ก ๋์ด //์์คํค ์ฝ๋ a = 97, A = 65, Enter = 13+10 ์์คํค ์ฝ๋๋ฅผ ๋ฐฐ์๋ผ System.out.println("๋ฌธ์์ด ์ฒ๋ฆฌ : String ๊ฐ์ฒด - ์ฐธ์กฐํ"); String irum = "ํ๊ธธ๋"; System.out.println(irum);//์ด๊ฒ์ ์ ์ ๋๋ ๊ธฐ๋ณธํ์ด ์๋... "์ฐธ์กฐํ" ๊ธฐ์ต์ฅ์ ํํ์ด๋ค. //JAVA์์ ๋๋ฌธ์๋ก ์์ํ๋ ๊ฒ์ Class ๋ฐ์ ์์.... } } | cs |
๋ฐ์ํ
'๐ป ํ๋ก๊ทธ๋๋ฐ > JAVA' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๋๋ค ํํ์(Lambda Expression) (0) | 2016.06.23 |
---|---|
2-2 ์ฐ์ ์ฐ์ฐ, ์นํ์ฐ์ฐ, ๋์ , ์ฆ๊ฐ, ๋ถํธ (0) | 2016.06.22 |
2-10 Scanner ์ฌ์ฉ๋ฒ๊ณผ ๋ฐ๋ณต For (0) | 2016.06.22 |
1-2 ๊ฐ์ฒด์์ฑ (0) | 2016.06.22 |
1-1 ์์คํ ์ฝ์์ ์ถ๋ ฅํด๋ณด๊ธฐ (0) | 2016.06.22 |