μλ° λνΌ, Class μ΄μ 리
βοΈ κΉμνμ μ€μ μλ° - μ€κΈ 1νΈ μ λ£κ³ μμ±νλ λ³΅μ΅ λΈλ‘κ·Έ μ λλ€.
0. λ°°κ²½
μλ°λ₯Ό μ²μ λ°°μΈ λ int, double κ³Ό κ°μ κΈ°λ³Ένμ λ¨Όμ λ°°μ΄λ€
νμ§λ§ κΈ°λ³Ένμλ λͺ νν νκ³κ° μ‘΄μ¬νλ€
κ·Έλ¬λ©΄ μ΄λ° νκ³λ₯Ό μ΄λ»κ² ν΄κ²°ν΄μΌν κΉ !!!?
1. κΈ°λ³Ένμ νκ³
κΈ°λ³Ένμ ν¬κ² λκ°μ§μ νκ³κ° μ‘΄μ¬νλ€.
- κ°μ²΄κ° μλ π
βοΈ
- κΈ°λ³Έν λ°μ΄ν°λ κ°μ²΄κ° μλλ€
- μλ°λ κ°μ²΄μ§ν₯ νλ‘κ·Έλλ° μΈμ΄μΈλ°, κ°μ²΄ μ§ν₯ νλ‘κ·Έλλ°μ μ₯μ μ μ΄λ¦΄ μ μλ€
- null κ°μ κ°μ§μ§ λͺ»ν¨ π
βοΈ
- κΈ°λ³Έν λ°μ΄ν° νμ μ null κ°μ κ°μ§ μ μλ€
- λ°μ΄ν°κ° μμμ΄λΌλ μνλ₯Ό νννλ €λ©΄ int μ κ²½μ° -1 μ΄λ 0 λ±μ μ¬μ©ν΄μΌ νλ€
μ΄λ¬ν νκ³λ₯Ό λ²μ΄λκΈ° μν΄μ μ§μ ν΄λμ€λ₯Ό λ§λ€μ΄λ³΄μ !!!
2. μ§μ κΈ°λ³Έν ν΄λμ€ λ§λ€κΈ°
λ¨μνκ² κΈ°λ³Έν ν΄λμ€λ₯Ό λ§λ€λ €λ©΄ κΈ°λ³Ένμ class λ‘ κ°μΈμ£Όλ©΄ λλ€
public class MyInteger {
private final int value;
public MyInteger(int value) {
this.value = value;
}
public int getValue() {
return value;
}
public int compareTo(int value, int target) {
if (value < target) {
return -1;
} else if (value > target) {
return 1;
} else {
return 0;
}
}
}
int value λ₯Ό private final λ‘ λ§λ€κ³ λ©μλλ₯Ό ν΅ν΄μ
- getValue() : value μ‘°ν λ©μλ
- compareTo() : value λΉκ΅ λ©μλ
λ κ°μ κΈ°λ₯μ μΆκ°νλ€
μ΄λ₯Ό ν΅ν΄μ intλ₯Ό κ°μ²΄λ‘ λ€λ£° μ μκ³ , null κ°μ ν΅ν΄ μμμ΄λΌλ μνλ₯Ό νμν μ μκ² λμλ€ !!
3. μλ° λνΌ ν΄λμ€
μ΄λ κ² νλμ© λ§λ€ νμ μμ΄ μλ°μμλ λνΌ ν΄λμ€λ₯Ό μ 곡νλ€
λνΌ ν΄λμ€λ κ°λ¨νκ² κΈ°λ³Ένμ κ°μ²΄ λ²μ μ΄λΌκ³ μκ°νλ©΄ λλ€ !!
| κΈ°λ³Έν | λνΌ ν΄λμ€ |
| byte | Byte |
| short | Short |
| int | Integer |
| long | Long |
| float | Float |
| double | Double |
| char | Character |
| boolean | Boolean |
κ°κ°μ κΈ°λ³Ένμ λνΌ ν΄λμ€μ λ§€νλλ€
λνΌ ν΄λμ€ μμ±
Integer newInteger = new Integer(10);
Integer integerObj = Integer.valueOf(10);
Long longObj = Long.valueOf(100);
Double doubleObj = Double.valueOf(10.5);
- λνΌ ν΄λμ€λ κ°μ²΄μ΄κΈ° λλ¬Έμ new ν€μλλ‘ μμ±ν μ μλ€
- νμ§λ§, vaueOf λ°©λ²μ΄ λ κΆμ λλλ° μμ£Ό μ¬μ©νλ κ°μ μΊμ±νκΈ° λλ¬Έμ΄λ€
- λν λνΌ ν΄λμ€μ κ°μ λΆλ³μ΄λ€
λνΌ ν΄λμ€ κ° μ½κΈ°
int intValue = integerObj.intValue();
long longValue = longObj.longValue();
- κΈ°λ³ΈνValue() μ νμμΌλ‘ λνΌ ν΄λμ€ κ°μ μ½μ μ μλ€
λνΌ ν΄λμ€ λΉκ΅
Integer newInteger = new Integer(10);
Integer integerObj = Integer.valueOf(10);
System.out.println("λΉκ΅");
System.out.println("==: " + (newInteger == integerObj));
System.out.println("equlas: "+(newInteger.equals(integerObj)));
- == μΌλ‘ λΉκ΅νμ λ false, equals λ‘ λΉκ΅νμ λ true
- newInteger μ integerObj λ λ€λ₯Έ μ°Έμ‘°κ°μ κ°μ§κ³ μκΈ° λλ¬Έμ == λ‘ λΉκ΅νλ©΄ false κ° λμ€κ³
- value κ° 10μΌλ‘ λμΌνκΈ° λλ¬Έμ equals λ‘ λΉκ΅νμ λ true κ° λμ¨λ€
Integer newInteger = Integer.valueOf(10);
Integer integerObj = Integer.valueOf(10);
System.out.println("λΉκ΅");
System.out.println("==: " + (newInteger == integerObj));
System.out.println("equlas: "+(newInteger.equals(integerObj)));
- == μΌλ‘ λΉκ΅νμ λ true, equals λ‘ λΉκ΅νμ λ true
- Integer.valueOf λ‘ λ§λλ κ²½μ° μΊμ±λ κ°μ μ¬μ©νκΈ° λλ¬Έμ == λ‘ λΉκ΅νμ λλ true κ° λμ¨λ€
4. λ°μ± & μΈλ°μ±
λ°μ± (Boxing)
- κΈ°λ³Έν → λνΌν΄λμ€
- λ°μ€μ 물건μ λ£μ κ² κ°λ€κ³ ν΄μ λ°μ±μ΄λΌκ³ νλ€
- new Integer() μ μΆν μμ λ μμ μ΄κΈ° λλ¬Έμ Integer.valueOf() λ₯Ό μ¬μ©ν΄μΌ νλ€
- Integer.valueOf() μλ μ±λ₯ μ΅μ ν κΈ°λ₯μ΄ μλ€. κ°λ°μκ° μΌλ°μ μΌλ‘ μ¬μ©νλ -128 ~ 127 λ²μμ Integer ν΄λμ€λ₯Ό 미리 μμ±νλ€.
μΈλ°μ± (Unboxing)
- λνΌ ν΄λμ€ → κΈ°λ³Έν
- λ°μ€μ λ€μ΄μλ 물건μ κΊΌλ΄λ κ²κ°λ€κ³ ν΄μ μΈλ°μ±μ΄λΌκ³ νλ€
μ€ν λ°μ± / μΈλ°μ± (Autoboxing / AutoUnboxing)
- μ»΄νμΌλ¬κ° κ°λ°μ λμ valueOf, xxxValue() λ±μ μ½λλ₯Ό μΆκ°ν΄μ€λ€
- μ€ν λ°μ± / μΈλ°μ±μΌλ‘ κΈ°λ³Ένκ³Ό λνΌνμ μλ‘ νΈνκ² λ³νν μ μλ€
Integer boxedValue = value;
Integer boxedValue = Integer.valueOf(value);
int unboxedValue = boxedValue;
int unboxedValue = boxedValue.intValue();
5. Class ν΄λμ€
ν΄λμ€μ μ 보 (λ©νλ°μ΄ν°) λ₯Ό λ€λ£¨λλ° μ¬μ©νλ€
μ¦, μ€ν μ€μΈ μλ° μ ν리μΌμ΄μ λ΄μμ νμν ν΄λμ€μ μμ±μ λ©μλμ λν μ 보λ₯Ό μ‘°ννκ³ μ‘°μν μ μλ€
μ£Όμ κΈ°λ₯
- νμ μ 보 μ»κΈ° : ν΄λμ€μ μ΄λ¦, μνΌν΄λμ€, μΈν°νμ΄μ€, μ κ·Ό μ νμ λ±κ³Ό κ°μ μ 보λ₯Ό μ‘°ν
- 리νλ μ
: ν΄λμ€μ μ μλ λ©μλ, νλ, μμ±μ λ±μ μ‘°ννκ³ μ΄λ€μ ν΅ν΄ κ°μ²΄ μΈμ€ν΄μ€λ₯Ό μμ±νκ±°λ λ©μλλ₯Ό νΈμΆνλ λ±μ μμ
-> μ΄κ±΄ μΆνμ λ°°μλ λλ€ !! - λμ λ‘λ©κ³Ό μμ± : Class.forName() λ©μλλ₯Ό μ¬μ©νμ¬ ν΄λμ€λ₯Ό λμ μΌλ‘ λ‘λνκ³ , newInstance() λ©μλλ₯Ό ν΅ν΄ μλ‘μ΄ μΈμ€ν΄μ€λ₯Ό μμ±
- μ λ Έν μ΄μ μ²λ¦¬ : ν΄λμ€μ μ μ©λ μ λ Έν μ΄μ μ μ‘°ννκ³ μ²λ¦¬νλ κΈ°λ₯μ μ 곡
ν΄λμ€ μ‘°ν
// 1. ν΄λμ€μμ μ‘°ν
Class clazz = String.class;
// 2. μΈμ€ν΄μ€μμ μ‘°ν
Class clazz1 = new String().getClass();
// 3. λ¬Έμμ΄λ‘ μ‘°ν
Class clazz2 = Class.forName("java.lang.String");
Class ν΄λμ€μ μ£Όμ κΈ°λ₯
- getDeclaredFields() : ν΄λμ€μ λͺ¨λ νλ μ‘°ν
- getDeclaredMethods() : ν΄λμ€μ λͺ¨λ λ©μλ μ‘°ν
- getSuperclass() : ν΄λμ€μ λΆλͺ¨ ν΄λμ€ μ‘°ν
- getInterface() : ν΄λμ€μ μΈν°νμ΄μ€λ€ μ‘°ν
6. System ν΄λμ€
μμ€ν κ³Ό κ΄λ ¨λ κΈ°λ³Έ κΈ°λ₯λ€μ μ 곡νλ€
| μ 곡 λ©μλ | μ€λͺ |
| System.currentTimeMillis() | νμ¬ μκ°μ λ°λ¦¬μ΄ λ¨μλ‘ μ 곡 |
| System.nanoTime | νμ¬ μκ°μ λλ Έμ΄ λ¨μλ‘ μ 곡νλ€ |
| System.getenv() | OSμμ μ€μ ν νκ²½ λ³μμ κ° μ‘°ν |
| System.getProerties() | νμ¬ μμ€ν μμ± |
| System.getProperty(String key) | νΉμ μμ± |
| System.exit(int status) | νλ‘κ·Έλ¨μ μ’ λ£νκ³ , OSμ νλ‘κ·Έλ¨ μ’ λ£μ μν μ½λλ₯Ό μ λ¬ |
| System.arraycopy | μμ€ν λ 벨μμ μ΅μ νλ λ©λͺ¨λ¦¬ λ³΅μ¬ μ°μ°μ μ¬μ© |