Java

[Effective Java] ์•„์ดํ…œ 49. ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์œ ํšจํ•œ์ง€ ๊ฒ€์‚ฌํ•˜๋ผ.

quedevel 2023. 3. 23. 10:05
728x90
๋ฐ˜์‘ํ˜•

๐ŸŽฏ ์•„์ดํ…œ 49. ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์œ ํšจํ•œ์ง€ ๊ฒ€์‚ฌํ•˜๋ผ.

"์˜ค๋ฅ˜๋Š” ๊ฐ€๋Šฅํ•œ ํ•œ ๋นจ๋ฆฌ (๋ฐœ์ƒํ•œ ๊ณณ์—์„œ) ์žก์•„์•ผ ํ•œ๋‹ค"

์˜ค๋ฅ˜๋ฅผ ๋ฐœ์ƒํ•œ ์ฆ‰์‹œ ์žก์ง€ ๋ชปํ•˜๋ฉด ํ•ด๋‹น ์˜ค๋ฅ˜๋ฅผ ๊ฐ์ง€ํ•˜๊ธฐ ์–ด๋ ค์›Œ์ง€๊ณ , ๊ฐ์ง€ํ•˜๋”๋ผ๋„ ์˜ค๋ฅ˜์˜ ๋ฐœ์ƒ ์ง€์ ์„ ์ฐพ๊ธฐ ์–ด๋ ค์›Œ ์ง„๋‹ค.


๋งŒ์•ฝ, ๋งค๊ฐœ๋ณ€์ˆ˜ ๊ฒ€์‚ฌ๋ฅผ ์ œ๋Œ€๋กœ ํ•˜์ง€ ๋ชปํ•˜๋ฉด ๋ช‡ ๊ฐ€์ง€ ๋ฌธ์ œ๊ฐ€ ์ƒ๊ธธ ์ˆ˜ ์žˆ๋‹ค.

๋ฉ”์„œ๋“œ๊ฐ€ ์ˆ˜ํ–‰๋˜๋Š” ์ค‘๊ฐ„์— ๋ชจํ˜ธํ•œ ์˜ˆ์™ธ๋ฅผ ๋˜์ง€๋ฉฐ ์‹คํŒจํ•  ์ˆ˜ ์žˆ๋‹ค.

๋งค๊ฐœ๋ณ€์ˆ˜ ๊ฒ€์‚ฌ์— ์‹คํŒจํ•˜๋ฉด ์‹คํŒจ ์›์ž์„ฑ์„ ์–ด๊ธฐ๋Š” ๊ฒฐ๊ณผ๋ฅผ ๋‚ณ์„ ์ˆ˜ ์žˆ๋‹ค.

์‹คํŒจ ์›์ž์„ฑ : ๋ฉ”์„œ๋“œ์—์„œ ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•œ ํ›„์—๋„ ๊ทธ ๊ฐ์ฒด๋Š” ์—ฌ์ „ํžˆ ์œ ํšจํ•œ ์ƒํƒœ

public๊ณผ protected ๋ฉ”์„œ๋“œ๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜ ๊ฐ’์ด ์ž˜๋ชป๋์„ ๋•Œ ๋˜์ง€๋Š” ์˜ˆ์™ธ๋ฅผ ๋ฌธ์„œํ™”ํ•ด์•ผ ํ•œ๋‹ค.

(@throws ์ž๋ฐ”๋… ํƒœ๊ทธ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.)

    /**
     * (ํ˜„์žฌ ๊ฐ’ mod m) ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์ด ๋ฉ”์„œ๋“œ๋Š”
     * ํ•ญ์ƒ ์Œ์ด ์•„๋‹Œ BigInteger๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค๋Š” ์ ์—์„œ remainder ๋ฉ”์„œ๋“œ์™€ ๋‹ค๋ฅด๋‹ค.
     * 
     * @param m ๊ณ„์ˆ˜ (์–‘์ˆ˜์—ฌ์•ผ ํ•œ๋‹ค.)
     * @return ํ˜„์žฌ ๊ฐ’ mod m
     * @throws ArithmeticException m์ด 0๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์œผ๋ฉด ๋ฐœ์ƒํ•œ๋‹ค.
     */
    public BigInteger mod(BigInteger m){
        if (m.signum() <= 0){
            throw new ArithmeticException("๊ณ„์ˆ˜ (m)๋Š” ์–‘์ˆ˜์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค. "+m);
        }
        // ,,, ๊ณ„์‚ฐ ์ˆ˜ํ–‰
    }

์—ฌ๊ธฐ์„œ NullPointException(NPE)์— ๋Œ€ํ•œ ๋‚ด์šฉ์€ ๊ธฐ์ˆ ๋˜์–ด ์žˆ์ง€ ์•Š์ง€๋งŒ,

BigInteger ํด๋ž˜์Šค ์ˆ˜์ค€์—์„œ ๊ธฐ์ˆ ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ์ด ํด๋ž˜์Šค์˜ public ๋ฉ”์„œ๋“œ ์ „์ฒด์— ์ ์šฉ๋˜๊ธฐ์— ๊ฐ ๋ฉ”์„œ๋“œ์— ๊ธฐ์ˆ  ํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค.


java.util.Objects.requireNonNull

์ž๋ฐ” 7์— ์ถ”๊ฐ€๋œ ๋ฉ”์„œ๋“œ๋กœ ์œ ์—ฐํ•˜๊ณ  ์‚ฌ์šฉํ•˜๊ธฐ๋„ ํŽธํ•˜๋‹ˆ, ๋” ์ด์ƒ null ๊ฒ€์‚ฌ๋ฅผ ์ˆ˜๋™์œผ๋กœ ํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค.

public static <T> T requireNonNull(T obj) {
    if (obj == null)
        throw new NullPointerException();
    return obj;
}

public static <T> T requireNonNull(T obj, String message) {
    if (obj == null)
        throw new NullPointerException(message);
    return obj;
}

public์ด ์•„๋‹Œ ๋ฉ”์„œ๋“œ๋ผ๋ฉด ๋‹จ์–ธ๋ฌธ(assert)์„ ์‚ฌ์šฉํ•ด ๋งค๊ฐœ๋ณ€์ˆ˜ ์œ ํšจ์„ฑ์„ ๊ฒ€์ฆํ•  ์ˆ˜ ์žˆ๋‹ค.

public class Item49 {
    private static void solution(int[] arr, int cnt){
        assert arr != null;
        assert cnt > 0;
        System.out.println("---------");
        System.out.println("arr.length = " + arr.length);
        System.out.println("cnt = " + cnt);
        System.out.println("---------");
    }

    public static void main(String[] args) {
        solution(null, -1);
    }
}



๋ฉ”์„œ๋“œ๋‚˜ ์ƒ์„ฑ์ž๋ฅผ ์ž‘์„ฑํ•  ๋•Œ๋ฉด ๊ทธ ๋งค๊ฐœ๋ณ€์ˆ˜๋“ค์— ์–ด๋–ค ์ œ์•ฝ์ด ์žˆ์„์ง€ ์ƒ๊ฐํ•ด์•ผ ํ•œ๋‹ค.

๊ทธ ์ œ์•ฝ๋“ค์„ ๋ฌธ์„œํ™”ํ•˜๊ณ  ๋ฉ”์„œ๋“œ ์ฝ”๋“œ ์‹œ์ž‘ ๋ถ€๋ถ„์—์„œ ๋ช…์‹œ์ ์œผ๋กœ ๊ฒ€์‚ฌํ•ด์•ผํ•œ๋‹ค.

์ฐธ๊ณ  ์ž๋ฃŒ

Joshua Bloch, ใ€ŽEffective Java 3/Eใ€, ๊ฐœ์•ž๋งต์‹œ ์˜ฎ๊น€, ํ”„๋กœ๊ทธ๋ž˜๋ฐ์ธ์‚ฌ์ดํŠธ(2018)
http://www.kyobobook.co.kr/product/detailViewKor.laf?ejkGb=KOR&mallGb=KOR&barcode=9788966262281&orderClick=LEa&Kc=

728x90
๋ฐ˜์‘ํ˜•