Java

[Effective Java] ์•„์ดํ…œ 29. ์ด์™•์ด๋ฉด ์ œ๋„ค๋ฆญ ํƒ€์ž…์œผ๋กœ ๋งŒ๋“ค๋ผ.

quedevel 2023. 3. 21. 09:47
728x90
๋ฐ˜์‘ํ˜•

๐ŸŽฏ ์•„์ดํ…œ 29. ์ด์™•์ด๋ฉด ์ œ๋„ค๋ฆญ ํƒ€์ž…์œผ๋กœ ๋งŒ๋“ค๋ผ.

์ƒˆ๋กœ์šด ํƒ€์ž…์„ ์„ค๊ณ„ํ•  ๋•Œ๋Š” ํ˜•๋ณ€ํ™˜ ์—†์ด๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋กํ•˜๋ผ. ๊ทธ๋ ‡๊ฒŒ ํ•˜๋ ค๋ฉด ์ œ๋„ค๋ฆญ ํƒ€์ž…์œผ๋กœ ๋งŒ๋“ค์–ด์•ผ ํ•  ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๋‹ค.
๊ธฐ์กด ํƒ€์ž… ์ค‘ ์ œ๋„ค๋ฆญ์ด์—ˆ์–ด์•ผ ํ•˜๋Š” ๊ฒŒ ์žˆ๋‹ค๋ฉด ์ œ๋„ค๋ฆญ ํƒ€์ž…์œผ๋กœ ๋ณ€๊ฒฝํ•˜์ž. ๊ธฐ์กด ํด๋ผ์ด์–ธํŠธ์—๋Š” ์•„๋ฌด ์˜ํ–ฅ์„ ์ฃผ์ง€ ์•Š์œผ๋ฉด์„œ,
์ƒˆ๋กœ์šด ์‚ฌ์šฉ์ž๋ฅผ ํ›จ์”ฌ ํŽธํ•˜๊ฒŒ ํ•ด์ฃผ๋Š” ๊ธธ์ด๋‹ค.


  • Object ๊ธฐ๋ฐ˜ ์Šคํƒ - ์ œ๋„ค๋ฆญ์ด ์ ˆ์‹คํ•œ ๊ฐ•๋ ฅ ํ›„๋ณด!โญ ์ง€๊ธˆ ์ƒํƒœ์—์„œ๋Š” ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์Šคํƒ์—์„œ ๊บผ๋‚ธ ๊ฐ์ฒด๋ฅผ ํ˜•๋ณ€ํ™˜ํ•ด์•ผ ํ•˜๋Š”๋ฐ, ์ด๋•Œ ๋Ÿฐํƒ€์ž„ ์˜ค๋ฅ˜๊ฐ€ ๋‚  ์œ„ํ—˜์ด ์žˆ๋‹ค.
    public class Stack {
      private Object[] elements;
      private int size = 0;
      private static final int DEFAULT_INITIAL_CAPACITY = 16;
    
      public Stack() {
          elements = new Object[DEFAULT_INITIAL_CAPACITY];
      }
    
      public void push(Object e) {
          ensureCapacity();
          elements[size++] = e;
      }
    
      public Object pop() {
          if (size == 0)
              throw new EmptyStackException();
          Object result = elements[--size];
          elements[size] = null;
          return result;
      }
    
      public boolean isEmpty() { 
          return size == 0;
      }
    
      private void ensureCapacity() {
          if (elements.length == size) 
              elements = Arrays.copyOf(elements, 2 * size + 1);
      }
    }
      

์ผ๋ฐ˜ ํด๋ž˜์Šค๋ฅผ ์ œ๋„ค๋ฆญ ํด๋ž˜์Šค๋กœ ๋งŒ๋“œ๋Š” ์ฒซ ๋‹จ๊ณ„๋Š” ํด๋ž˜์Šค ์„ ์–ธ์— ํƒ€์ž… ๋งค๊ฐœ๋ณ€์ˆ˜(E)๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ์ผ์ด๋‹ค.

public Stack() {
    elements = new E[DEFAULT_INITIAL_CAPACITY];
}

โญ ํ•˜์ง€๋งŒ E์™€ ๊ฐ™์€ ์‹ค์ฒดํ™” ๋ถˆ๊ฐ€ ํƒ€์ž…์œผ๋กœ๋Š” ๋ฐฐ์—ด์„ ๋งŒ๋“ค ์ˆ˜ ์—†๋‹ค.


  • ํ•ด๊ฒฐ์ฑ… ์ฒซ ๋ฒˆ์งธ

    ์ œ๋„ค๋ฆญ ๋ฐฐ์—ด ์ƒ์„ฑ์„ ๊ธˆ์ง€ํ•˜๋Š” ์ œ์•ฝ์„ ๋Œ€๋†“๊ณ  ์šฐํšŒํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ Object ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•œ ๋‹ค์Œ ์ œ๋„ค๋ฆญ ๋ฐฐ์—ด๋กœ ํ˜•๋ณ€ํ™˜ ํ•˜์ž!
    // ๋ฐฐ์—ด elements๋Š” push(E)๋กœ ๋„˜์–ด์˜จ E ์ธ์Šคํ„ด์Šค๋งŒ ๋‹ด๋Š”๋‹ค.
    // ๋”ฐ๋ผ์„œ ํƒ€์ž… ์•ˆ์ „์„ฑ์„ ๋ณด์žฅํ•˜์ง€๋งŒ,
    // ์ด ๋ฐฐ์—ด์˜ ๋Ÿฐํƒ€์ž„ ํƒ€์ž…์€ E[]๊ฐ€ ์•„๋‹Œ Object[]๋‹ค!
    @SuppressWarnings("unchecked")
    public Stack() {
      elements = (E[]) new Object[DEFAULT_INITIAL_CAPACITY];
    }
    โญ push ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ๋ฐฐ์—ด์— ์ €์žฅ๋˜๋Š” ์›์†Œ์˜ ํƒ€์ž…์€ ํ•ญ์ƒ E๋‹ค. ๋”ฐ๋ผ์„œ ์ด ๋น„๊ฒ€์‚ฌ ํ˜•๋ณ€ํ™˜์€ ํ™•์‹คํžˆ ์•ˆ์ „ํ•˜๊ธฐ ๋•Œ๋ฌธ์— @SuppressWarnings์œผ๋กœ ๊ฒฝ๊ณ ๋ฅผ ์ˆจ๊ธด๋‹ค.

  • ํ•ด๊ฒฐ์ฑ… ๋‘ ๋ฒˆ์งธ

    elements ํ•„๋“œ์˜ ํƒ€์ž…์„ E[]์—์„œ Object[]๋กœ ๋ฐ”๊พธ๋Š” ๊ฒƒ์ด๋‹ค. ์ด ๋˜ํ•œ push ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด E ํƒ€์ž…๋งŒ ํ—ˆ์šฉํ•˜๋ฏ€๋กœ ์•ˆ์ „ํ•˜๋‹ค.
    // ๋น„๊ฒ€์‚ฌ ๊ฒฝ๊ณ ๋ฅผ ์ ์ ˆํžˆ ์ˆจ๊ธด๋‹ค.
    public E pop() {
      if (size == 0)
          throw new EmptyStackException();
    
      // push์—์„œ E ํƒ€์ž…๋งŒ ํ—ˆ์šฉํ•˜๋ฏ€๋กœ ์ด ํ˜•๋ณ€ํ™˜์€ ์•ˆ์ „ํ•˜๋‹ค.
      @SuppressWarnings("unchecked") E result = (E) elements[--size];
      elements[size] = null; // ๋‹ค ์“ด ์ฐธ์กฐ ํ•ด์ œ
      return result;
    }

์ฒซ ๋ฒˆ์งธ ๋ฐฉ์‹์—๋Š” ํ˜•๋ณ€ํ™˜์„ ๋ฐฐ์—ด ์ƒ์„ฑ ์‹œ ๋‹จ ํ•œ ๋ฒˆ๋งŒ ํ•ด์ฃผ๋ฉด ๋˜์ง€๋งŒ, ๋‘ ๋ฒˆ์งธ ๋ฐฉ์‹์—์„œ๋Š” ๋ฐฐ์—ด์—์„œ ์›์†Œ๋ฅผ ์ฝ์„ ๋•Œ๋งˆ๋‹ค ํ•ด์ค˜์•ผํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ํ˜„์—…์—์„œ๋Š” ์ฒซ ๋ฒˆ์งธ ๋ฐฉ์‹์„ ๋” ์„ ํ˜ธํ•˜๋ฉฐ ์ž์ฃผ ์‚ฌ์šฉํ•œ๋‹ค. ํ•˜์ง€๋งŒ (E๊ฐ€ Object๊ฐ€ ์•„๋‹Œ ํ•œ) ๋ฐฐ์—ด์˜ ๋Ÿฐํƒ€์ž„ ํƒ€์ž…์ด ์ปดํŒŒ์ผํƒ€์ž…๊ณผ ๋‹ฌ๋ผ ํž™ ์˜ค์—ผ์„ ์ผ์œผํ‚จ๋‹ค.


๐Ÿšฉ ํž™ ์˜ค์—ผ(heap pollution)์ด๋ž€? click here.


์ฐธ๊ณ  ์ž๋ฃŒ

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
๋ฐ˜์‘ํ˜•