mallin 2025. 7. 19. 01:53

β˜•οΈ  κΉ€μ˜ν•œμ˜ μ‹€μ „ μžλ°” - 쀑급 1편 μ„ λ“£κ³  μž‘μ„±ν•˜λŠ” λ³΅μŠ΅ λΈ”λ‘œκ·Έ μž…λ‹ˆλ‹€.

 

0. λ°°κ²½

μžλ°”λ₯Ό 처음 배울 λ•Œ int, double κ³Ό 같은 κΈ°λ³Έν˜•μ„ λ¨Όμ € λ°°μš΄λ‹€

ν•˜μ§€λ§Œ κΈ°λ³Έν˜•μ—λŠ” λͺ…ν™•ν•œ ν•œκ³„κ°€ μ‘΄μž¬ν•œλ‹€

그러면 이런 ν•œκ³„λ₯Ό μ–΄λ–»κ²Œ ν•΄κ²°ν•΄μ•Όν• κΉŒ !!!?


1. κΈ°λ³Έν˜•μ˜ ν•œκ³„

κΈ°λ³Έν˜•μ€ 크게 λ‘κ°€μ§€μ˜ ν•œκ³„κ°€ μ‘΄μž¬ν•œλ‹€.

 

  1. 객체가 μ•„λ‹˜ πŸ™…‍♀️ 
    • κΈ°λ³Έν˜• λ°μ΄ν„°λŠ” 객체가 μ•„λ‹ˆλ‹€
    • μžλ°”λŠ” 객체지ν–₯ ν”„λ‘œκ·Έλž˜λ° 언어인데, 객체 μ§€ν–₯ ν”„λ‘œκ·Έλž˜λ°μ˜ μž₯점을 살릴 수 μ—†λ‹€
  2. 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 클래슀

클래슀의 정보 (메타데이터) λ₯Ό λ‹€λ£¨λŠ”λ° μ‚¬μš©ν•œλ‹€

즉, μ‹€ν–‰ 쀑인 μžλ°” μ• ν”Œλ¦¬μΌ€μ΄μ…˜ λ‚΄μ—μ„œ ν•„μš”ν•œ 클래슀의 속성와 λ©”μ†Œλ“œμ— λŒ€ν•œ 정보λ₯Ό μ‘°νšŒν•˜κ³  μ‘°μž‘ν•  수 μžˆλ‹€

 

μ£Όμš” κΈ°λŠ₯

  1. νƒ€μž… 정보 μ–»κΈ° : 클래슀의 이름, 슈퍼클래슀, μΈν„°νŽ˜μ΄μŠ€, μ ‘κ·Ό μ œν•œμž λ“±κ³Ό 같은 정보λ₯Ό 쑰회
  2. λ¦¬ν”Œλ ‰μ…˜ : ν΄λž˜μŠ€μ— μ •μ˜λœ λ©”μ†Œλ“œ, ν•„λ“œ, μƒμ„±μž 등을 μ‘°νšŒν•˜κ³  이듀을 톡해 객체 μΈμŠ€ν„΄μŠ€λ₯Ό μƒμ„±ν•˜κ±°λ‚˜ λ©”μ†Œλ“œλ₯Ό ν˜ΈμΆœν•˜λŠ” λ“±μ˜ μž‘μ—…
    -> 이건 좔후에 λ°°μ›Œλ„ λœλ‹€ !!
  3. 동적 λ‘œλ”©κ³Ό 생성 : Class.forName() λ©”μ„œλ“œλ₯Ό μ‚¬μš©ν•˜μ—¬ 클래슀λ₯Ό λ™μ μœΌλ‘œ λ‘œλ“œν•˜κ³ , newInstance() λ©”μ„œλ“œλ₯Ό 톡해 μƒˆλ‘œμš΄ μΈμŠ€ν„΄μŠ€λ₯Ό 생성
  4. μ• λ…Έν…Œμ΄μ…˜ 처리 : ν΄λž˜μŠ€μ— 적용된 μ• λ…Έν…Œμ΄μ…˜μ„ μ‘°νšŒν•˜κ³  μ²˜λ¦¬ν•˜λŠ” κΈ°λŠ₯을 제곡

 

클래슀 쑰회

// 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 μ‹œμŠ€ν…œ λ ˆλ²¨μ—μ„œ μ΅œμ ν™”λœ λ©”λͺ¨λ¦¬ 볡사 연산을 μ‚¬μš©