# [Java] ν‘œμ€€ μž…μΆœλ ₯ 클래슀 System에 λŒ€ν•΄
Study Repository

[Java] ν‘œμ€€ μž…μΆœλ ₯ 클래슀 System에 λŒ€ν•΄

by rlaehddnd0422

μžλ°”μ—μ„œλŠ” λͺ¨λ“  것이 객체둜 ν‘œν˜„λ˜λ―€λ‘œ, μž…μΆœλ ₯을 λ‹΄λ‹Ήν•˜λŠ” μˆ˜λ‹¨ λ˜ν•œ λͺ¨λ‘ κ°μ²΄μž…λ‹ˆλ‹€.

Cμ–Έμ–΄μ˜ printf(), scanf(), C++의 std::cin, std::cout 처럼 μžλ°”μ—μ„œλŠ” Systemμ΄λΌλŠ” ν‘œμ€€ μž…μΆœλ ₯ 클래슀λ₯Ό μ •μ˜ν•˜μ—¬ μ œκ³΅ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€.

 

β€» System ν΄λž˜μŠ€λŠ” java.lang νŒ¨ν‚€μ§€μ— ν¬ν•¨λ˜μ–΄ μ œκ³΅ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€.

 

λŒ€ν‘œμ μœΌλ‘œ System.in(ν‘œμ€€ μž…λ ₯), System.out(ν‘œμ€€ 좜λ ₯), System.err(ν‘œμ€€ 좜λ ₯)이 μžˆλŠ”λ° ν•˜λ‚˜μ”© μ‚΄νŽ΄λ΄…μ‹œλ‹€.

System.in 

System.in.read()λŠ” λ°”μ΄νŠΈ λ‹¨μœ„λ‘œ 데이터λ₯Ό μ½λŠ” λ©”μ†Œλ“œμž…λ‹ˆλ‹€. 이 λ©”μ†Œλ“œλŠ” System.inμœΌλ‘œλΆ€ν„° λ°”μ΄νŠΈλ₯Ό 읽고, 읽은 λ°”μ΄νŠΈμ˜ μ •μˆ˜ 값을 λ¦¬ν„΄ν•©λ‹ˆλ‹€. 

public class SystemTest0 {
    
    public static void main(String[] args) throws IOException {
        int k = System.in.read();
        System.out.println("k = " + k);
    }
}

단점 - ν•˜λ‚˜μ˜ 문자만 μž…λ ₯ν•  수 μžˆλ‹€.

 

ν‘œμ€€ μž…λ ₯이긴 ν•˜μ§€λ§Œ λ‹¨μˆœνžˆ λ°”μ΄νŠΈλ₯Ό 읽을 λ•Œλ§Œ μ‚¬μš©ν•  수 있기 λ•Œλ¬Έμ— 잘 μ‚¬μš©ν•˜μ§€λŠ” μ•ŠλŠ” 클래슀

 

System.out, System.err

μžλ°” ν‘œμ€€ 좜λ ₯λ¬Έ 

System.out.println(λ¦¬ν„°λŸ΄ λ˜λŠ” λ³€μˆ˜) : κ΄„ν˜Έ μ•ˆμ˜ λ‚΄μš©μ„ 좜λ ₯ν•˜κ³  κ°œν–‰
System.out.print(λ¦¬ν„°λŸ΄ λ˜λŠ” λ³€μˆ˜) : κ΄„ν˜Έ μ•ˆμ˜ λ‚΄μš©μ„ 좜λ ₯
System.out.printf(ν˜•μ‹λ¬Έμžμ—΄, κ°’) : κ΄„ν˜Έ μ•ˆμ˜ ν˜•μ‹λŒ€λ‘œ 값을 좜λ ₯
public class SystemOutTest {
    private static int k = 1;
    public static void main(String[] args) {
        System.out.print("λ°˜κ°‘μŠ΅λ‹ˆλ‹€.");
        System.out.println("μ•ˆλ…•ν•˜μ„Έμš”.");
        System.out.printf("%d", k);
        System.out.printf("%s", k);
    }
}

System.err은 System.outκ³Ό μ‚¬μš©λ²•μ€ κ°™μ§€λ§Œ μ½˜μ†”μ— λΉ¨κ°„μƒ‰μœΌλ‘œ 좜λ ₯λ©λ‹ˆλ‹€.

λΈ”λ‘œκ·Έμ˜ 정보

Study Repository

rlaehddnd0422

ν™œλ™ν•˜κΈ°