π© Javaμμ Primitive λ°μ΄ν° νμ μ
- μ μνμ (byte, short, int, long)
- μ€μνμ (float, double)
- λ Όλ¦¬νμ (boolean)
- λ¬Έμνμ (char)
μ΄ 4κ°μ§ νμ μΌλ‘ ꡬμ±λμμ΅λλ€.
μ΄μ λΉμ·νκ² Wrapper νμ μ΄λΌκ³ κ° Primitive λ³ μ°Έμ‘°νμ μ΄ λ°λ‘ μ‘΄μ¬ν©λλ€.
π© Wrapper νμ
- μ μνμ (Byte, Short, Integer, Long)
- μ€μνμ (Float, Double)
- λ Όλ¦¬νμ (Boolean)
- λ¬Έμνμ (Character)
π© Boxing κ³Ό Unboxing μλ―Έμ μ¬μ© μ΄μ
μμ Primitive νμ κ³Ό Wrapper νμ μ μλ‘ μλ€κ°λ€(Casting) ν λ μ¬μ©νλ€.
- Boxing : Primitive -> Wrapper
- Unboxing : Wrapper -> Primitive
μλ°μμλ κΈ°λ³Έν νμ (Primitive)λ κ°μ μ μ₯νκΈ° λλ¬Έμ Collectionμ λ΄μ μ μλ€.
μ΄λ₯Ό ν΄κ²°νκΈ° μν΄ κΈ°λ³Έν νμ (Primitive Type)μ Boxingν΄μ μ°Έμ‘° νμ (Reference Type)μΌλ‘ λ³νν΄μ Collectionμ λ΄μ μ μκ² λλ€.
π© Boxing κ³Ό Unboxing λ°©λ²
int x = 3;
Integer μ μκ°μ· = Integer.valueOf(x);// new Integer(x); Boxing
int x1 = μ μκ°μ·.intValue();// Unboxing
String y = "hello";// νμμ΄ κ°μ·μ μ
κ³ μλ λ¬Έμμ΄!
double d1 = 3.9;
Double λλΈκ°μ· = Double.valueOf(d1);// new Double(d1);
double d2 = λλΈκ°μ·.doubleValue();// Unboxing
// ---μ μλ₯Ό λ¬Έμμ΄λ‘ λ³ν & λ¬Έμμ΄μ λλΈλ‘ λ³ν.
int n1 = 10;
String s1 = String.valueOf(n1);
String s2 = "30.2";
double n2 = Double.parseDouble(s2);
μλ μμ€μ½λ μλ³Έμ λ³Ό μ μλ€.
π π
https://beastit.tistory.com/34
컬λ μ μ κ΄ν΄ κΆκΈνλ€λ©΄ μλ λ§ν¬λ₯Ό μ°Έμ‘°νλ©΄ λλ€.
https://beastit.tistory.com/87
'IT > JAVA' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[Java] μμ λ΄μ© λ©λͺ¨ (0) | 2023.08.30 |
---|---|
[Java] μ λ€λ¦(Generic) (1) | 2023.07.09 |
[Java] 컬λ μ (Collection) ? (1) | 2023.07.09 |
[Java] ν¨μ μ€λ²λ‘λ(overload) (1) | 2023.07.04 |
[Java]ꡬ쑰μ μΈ νλ‘κ·Έλλ°(λ©μλ) (2) | 2023.06.28 |