IBM Korea Skip to main content
       IBM Ȩ    |  Á¦Ç° & ¼­ºñ½º  |  °í°´Áö¿ø & ´Ù¿î·Îµå  |  È¸¿ø°¡ÀÔ  

IBM developerWorks > ÀÚ¹Ù
developerWorks

Magic with Merlin : J2SE 1.5
TigerÀÇ ¹Ì·¡¿Í ¾ð¾î Â÷¿øÀÇ º¯È­

Level: Introductory

John Zukowski
President, JZ Ventures, Inc.
2003³â 12¿ù 19ÀÏ

Column iconJ2SE 1.5 (Tiger)´Â ¾ÆÁ÷ °ø½ÄÀûÀÎ µ¥ºß´Â ÇÏÁö ¾Ê¾ÒÁö¸¸ ÇÁ·ÎÅäŸÀÔÀ» ´Ù¿î·Îµå¸¦ ÅëÇØ Á¢±ÙÇÒ ¼ö ÀÖ´Ù. "parameterized" ŸÀÔ, enumeration, º¯¼ö ÀÎÀÚ ¸®½ºÆ®, autoboxing, Çâ»óµÈ for ·çÇÁ, Á¤Àû ¹ÝÀÔ µî ¾ð¾î Â÷¿øÀÇ ¹ßÀüÀÌ J2SE 1.5 ¹öÀü¿¡¼­ ´«¿¡ ¶è´Ù.

2004³â »ó¹Ý±â¿¡ Ãâ½Ã ¿¹Á¤ÀÎ J2SE 1.5 (Tiger)¿¡´Â ÀÚ¹Ù Ç÷§Æû¿¡ ´ëÇÑ ¾ð¾î Â÷¿øÀÇ ¸¹Àº º¯È­µéÀÌ Æ÷ÇԵǾî ÀÖ´Ù. strictfp °°Àº »õ·Î¿î Å°¿öµå¿Í Ç¥¸íÀÇ Ãß°¡°¡ ÀÖÀ» °ÍÀ̶ó´Â ±â´ë·Î ÀÚ¹Ù ÇÁ·Î±×·¡¹Ö ¾ð¾î ÀÚü´Â 1.1 ´ç½Ã ³»ºÎ Ŭ·¡½ºÀÇ Ãß°¡ ÀÌÈÄ º¯È­°¡ ¾ø¾ú´Ù. ÀÌ ¹öÀüÀº ¸¹Àº Áß¿äÇÑ º¯È­°¡ ÀÖÀ» °ÍÀ̶ó´Â ±â´ë¸¦ ¹Þ°í ÀÖ´Ù. "parameterized" µ¥ÀÌÅÍ À¯Çü(generics), ½Ã½ºÅÛÀÇ Áö¿øÀ» ¹Þ´Â enumeration, autoboxing, for ·çÇÁÀÇ Çâ»ó, Á¤Àû ¹ÝÀÔ µîÀ» Áö¿øÇÑ´Ù.

ÇÁ·ÎÅäŸÀÔ ¼³Ä¡Çϱâ
Sun Developer NetworkÀº generics Ãß°¡¸¦ Áö¿øÇÏ´Â ÇÁ·ÎÅäŸÀÔ ¹öÀü 2.2¸¦ Æ÷ÇÔÇÏ°í ÀÖ´Ù. ÀÌ ÇÁ·ÎÅäŸÀÔÀº ¾Õ¼­ ¾ð±ÞÇÑ ¾ð¾î ±¸Á¶Ã¼¸¦ Áö¿øÇϱ⠶§¹®¿¡ 1.5 ¹öÀüÀ» »ç¿ëÇÏ¿© ½ÇÇèÇÒ ¼ö ÀÖ´Ù.

Sun Developer Network(Âü°íÀÚ·á)À» ¹æ¹®ÇÏ¿© generics ÇÁ·ÎÅäŸÀÔÀ» ´Ù¿î·Îµå ÈÄ ¾ðÆÐÅ·ÇÏ¿© ȯ°æ º¯¼ö°¡ ¼³Á¤µÇ¾ú´ÂÁö¸¦ È®ÀÎÇÑ´Ù. ´Ù¿î·Îµå ÇÒ ¶§, Java Archive Files (JARs)Àº Ç¥ÁØ ½Ã½ºÅÛ Å¬·¡½º¸¦ »õ·Î¿î ¹öÀüÀ¸·Î ±³Ã¼ÇØ¾ß ÇÑ´Ù. À̸¦ °£°úÇϸé ÇÁ·ÎÅäŸÀÔ ÆÐÅ°Áö¸¦ »ç¿ëÇÒ ¼ö ¾ø´Ù.

¼³Á¤ÇØ¾ß ÇÒ µÎ °³ÀÇ È¯°æ º¯¼ö´Â JSR14DISTR(adding_generics-2_2-ea.zipÀ» ¾ðÆÐÅ·ÇÏ´Â Àå¼Ò)¿Í J2SE14(J2SDK 1.4.2°¡ ¼³Ä¡µÇ´Â °÷)ÀÌ´Ù. Listing 1Àº Microsoft Windows Ç÷§Æû¿¡ ¼³Á¤ÇÏ´Â ¸ð½ÀÀÌ´Ù:

Listing 1. ȯ°æ º¯¼ö ¼³Á¤

  set JSR14DISTR=c:\adding_generics-2_2-ea
  set J2SE14=c:\j2sdk1.4.2

¼³Á¤ÀÌ Á¤È®È÷ µÇ¾ú´ÂÁö¸¦ º¸·Á¸é ÇÁ·ÎÅäŸÀÔ¿¡ µû¶ó¿À´Â ÇÏÀ§ µð·ºÅ丮 examples¿¡¼­ Test.java ¼Ò½º ¿¹Á¦¸¦ º»´Ù. ÇÏÀ§ µð·ºÅ丮 scripts¿¡ ÀÖ´Â javac ½ºÅ©¸³Æ®·Î À̸¦ ÄÄÆÄÀÏÇÑ´Ù. ÄÄÆÄÀÏÀÌ Á¤È®È÷ µÇ¸é ȯ°æÀº ¿Ã¹Ù¸£°Ô ¼³Á¤µÈ °ÍÀÌ´Ù. ±×·¸Áö ¾Ê´Ù¸é ȯ°æ º¯¼ö°¡ Á¤È®È÷ ¼³Á¤µÇ¾ú´ÂÁö¸¦ È®ÀÎÇÑ´Ù.

generics »ç¿ëÇϱâ
¾ÆÁ÷±îÁö´Â »ý¼ÒÇÑ ¸î °¡Áö ¿ë¾î¸¦ »ç¿ëÇß´Ù. generics¿Í parameterized types(Á¾Á¾ ÅÛÇø´À̶ó°í ĪÇÔ)ÀÌ ±×°ÍÀÌ´Ù. generics Áö¿øÀ¸·Î º¸´Ù ¸í¹éÇÑ ÄÄÆÄÀÏ ½Ã°£ üũ¿Í ´ú ¸íÈ®ÇÑ Ä³½ºÆÃ(casting)°ú ÇÔ²² À¯Çü ÀÚÀ¯·Î¿î ŸÀÔÀÇ Ä÷º¼ÇÀ» °¡Áú ¼ö ÀÖ´Ù. generics´Â ¸¹Àº ÀåÁ¡µéÀÌ ÀÖÁö¸¸ ÇÑ °¡Áö Áß¿äÇÑ ÇýÅÃÀº String ŸÀÔÀÇ °´Ã¼ÀÇ List¸¦ ¿äûÇÒ ¼ö ÀÖ´Ù. Integer¸¦ List¿¡ ³õÀ¸¸é ÀÌ´Â ÄÄÆÄÀÏ ½Ã ÀÌ ½Ãµµ¸¦ °ÅºÎÇÒ °ÍÀÌ´Ù. ¶ÇÇÑ ¸®½ºÆ®¿¡¼­ ¿¤¸®¸ÕÆ®¸¦ º¸³»¸é À̸¦ ´Ù½Ã StringÀ¸·Î ij½ºÆÃ(cast) ÇÒ ÇÊ¿ä°¡ ¾ø´Ù. ÀÚµ¿À¸·Î ¼öÇàµÇ±â ¶§¹®ÀÌ´Ù. ÀÌ ±â´ÉÀÇ Á¤È®ÇÑ ½ÅÅýº´Â ¹Ù²ð ¼ö ÀÖÁö¸¸ °¡Àå ´Ü¼øÇÑ °æ¿ì Ä÷º¼Ç ¿ë ¼±¾ð ÁÖÀ§¿¡ <type>À» Ãß°¡ÇÏ¸é µÈ´Ù.

Listing 2´Â main() ¸Þ¼Òµå·Î Àü´ÞµÈ ¸í·ÉÇà ÀÎÀÚ ¾î·¹À̸¦ String °´Ã¼ÀÇ List·Î º¯È¯ÇÏ´Â ¹æ¹ýÀÌ´Ù:

Listing 2. generics ½ÃµµÇϱâ

  import java.util.*;

  public class StringList {
    public static void main (String args[]) {
      // Create a List of type String
      List<String> listOne = Arrays.asList(args);
      // Make editable copy of List of type String
      List<String> listTwo = new ArrayList<String>(listOne);
      // Print contents
      System.out.println(listTwo);
      // Fetch element
      String head = listTwo.get(0);
      // Print element
      System.out.println(head);
      // Add item to list
      listTwo.add(head);
      // Print contents
      System.out.println(listTwo);
      // Can't do item of type List.
      // Won't compile. 
      /* listTwo.add(listOne); */
    }
  }  

Ưº°ÇÑ javac ½ºÅ©¸³Æ®·Î ÇÁ·Î±×·¥À» ÄÄÆÄÀÏ ÇÑ ÈÄ¿¡, Ưº°ÇÑ java ½ºÅ©¸³Æ®·Î À̸¦ ½ÇÇàÇÑ´Ù. ¸í·ÉÇà ÀÎÀÚ ¼¼Æ®·Î Àü´ÞÇÏ¿© String°´Ã¼·Î¼­ List¿¡ ¹èÄ¡ÇÑ´Ù´Â °ÍÀ» º¸¿©ÁØ´Ù. ±×¸®°í ÇϳªÀÇ ¾ÆÀÌÅÛÀÌ ¸®½ºÆ® ¹Û¿¡¼­ °Ë»öµÇ°í µÚ¿¡¼­ Ãß°¡µÇ´Â °Íµµ º¸¿©ÁØ´Ù. main() ¸Þ¼ÒµåÀÇ ¸¶Áö¸· ¶óÀÎÀº ÄÄÆÄÀÏ ÇÒ ¼ö ¾ø±â ¶§¹®¿¡ ÁÖ¼®ÀÌ ´Þ·È´Ù. listTwo°¡ String À¯Çü Áß Çϳª¶ó°í ¼±¾ðµÇ¾ú±â ¶§¹®¿¡, ÄÄÆÄÀÏ·¯´Â List À¯ÇüÀ» ¿©±â¿¡ Ãß°¡ÇÒ ¼ö ¾ø´Â °ÍÀÌ´Ù.

½Ã½ºÅÛ Áö¿øÀ» ¹Þ´Â enumeration
enumerationÀº °ü·Ã »ó¼ö °ªµéÀ» Á¤ÀÇÇÒ ¼ö ÀÖµµ·Ï ÇÏ´Â ¾ð¾î ±¸Á¶Ã¼ÀÌ´Ù. ÀÌ °ªÀº »ó¼ö µé Áß Çϳª¸¦ ³ªÅ¸³»°íÀÚ ÇÒ¶§ »ç¿ëµÈ´Ù. .

enumerationÀ» ÇÒ ¼ö ÀÖÁö¸¸ Á¤È®ÇÏ°Ô µÇÁö ¾Ê´Â´Ù¸é ¹®Á¦°¡ »ý±ä´Ù. Á¤È®È÷ ÇÏ·Á¸é °£´ÜÇÑ Å½ºÅ©¿¡µµ ¸¹Àº Äڵ尡 ÇÊ¿äÇÏ´Ù. (Âü°íÀÚ·á)

JSR 201Àº »õ·Î¿î enum Å°¿öµå¸¦ Ãß°¡Çß´Ù. enumeration À̸§°ú °ýÈ£¾ÈÀ¸·Î µé¾î°¡¾ßÇÒ °¡´ÉÇÑ °ªµéÀÌ ±×°ÍÀÌ´Ù. (enum Color {red, green, blue}). Listing 3Àº enumÀÇ »ç¿ë ¹æ¹ýÀÌ´Ù:

Listing 3. enumeration

  public class EnumTest {
    public static void main(String args[]) {
      enum Color {red, green, blue};

      // Get collection of values
      System.out.println(Color.VALUES);

      // Check if collection type is java.util.List
      System.out.println(Color.VALUES instanceof java.util.List);

      // Create variable of type
      Color aColor = Color.green;

      // Use enumeration in switch
      switch(aColor) {
        case Color.red:
          System.out.println("Got red.");
          break;
        case Color.green:
          System.out.println("Got green.");
          break;
        case Color.blue:
          System.out.println("Got blue.");
          break;
      }
    }
  }

º¯¼ö ÀÎÀÚ ¸®½ºÆ®
TigerÀÇ ¶Ç ´Ù¸¥ »õ·Î¿î ±â´ÉÀº º¯¼ö ÀÎÀÚ ¸®½ºÆ® Áö¿øÀÌ´Ù. ÀÌÀü¿¡´Â ÀÎÀÚÀÇ º¯¼ö ¼ýÀÚÀÇ Áö¿øÀÌ ÇÊ¿äÇÏ¸é °´Ã¼ÀÇ ¾î·¹ÀÌ (Object[ ])·Î Àü´ÞÇÏ°í ±×·± ´ÙÀ½ ÀÌ°ÍÀÇ ±æÀ̸¦ ¿äûÇß´Ù. ÀÌÁ¦´Â ÀÎÀÚ ¸®½ºÆ®¿¡ ...¸¦ ÁöÁ¤ÇÔÀ¸·Î¼­ °°Àº °ÍÀ» ¾ò°Ô µÈ´Ù. Listing 4´Â ¸Þ¼Òµå ¼±¾ð°ú È£Ãâ ³»¿ëÀÌ´Ù.

Listing 4. º¯¼ö ÀÎÀÚ ¸®½ºÆ®

  public static void printf(String fmt, Object... args) {
    // ...
  }
  printf("Hello, %\n", first);
  printf("Hello, % %\n", first, last);
  printf("Hello, % (%) %\n", first, middle, last);

args[0] °°ÀÌ ¾î·¹ÀÌ ¾×¼¼½º¸¦ ÅëÇØ args¿¡ ¾×¼¼½ºÇÑ´Ù. º¯¼ö ÀÎÀÚ¸¦ »ç¿ëÇÏ´ø ÀÌÀü ¹æ½Ä°ú ´Ù¸¦ °ÍÀÌ ¾ø´Ù.

autoboxing ´Ù·ç±â
AutoboxingÀº ÇÁ¸®¸ÓƼºê À¯Çü¿¡¼­ Object¿¡ »óÀÀÇÏ´Â °ÍÀ¸·Î ¶Ç´Â ±× ¹Ý´ë·Î ÀÚµ¿º¯È¯ÀÌ´Ù. ¿¹¸¦ µé¾î ¸Þ¼Òµå È£Ãâ¿¡ Integer°¡ ÇÊ¿äÇÏ¿© int·Î È£ÃâÇÑ´Ù¸é ÄÄÆÄÀÏ·¯´Â ÀûÀýÇÑ À¯ÇüÀÌ ÀÌ ¸Þ¼Òµå·Î Àü´ÞµÉ ¼ö ÀÖµµ·Ï ÇÊ¿äÇÑ º¯È¯À» ¼öÇàÇÑ´Ù:

Listing 5. autoboxing

  public class BoxingTest {
    public static void main(String args[]) {
      test1(1);
      test2(new Integer(2));
    }
    private static void test1(Integer i) {
      System.out.println("test1: " + i);
    }
    private static void test2(int i) {
      System.out.println("test2: " + i);
    }
  }

Çâ»óµÈ for ·çÇÁ
generics ¿Ü¿¡µµ TigerÀÇ °¡Àå Àç¹ÌÀÖ´Â ±â´ÉÀº for ·çÇÁÀÇ Çâ»óÀÌ´Ù. »çÀü »çÈÄ Á¶°ÇÀ» ÁÖ±âÀ§ÇØ for ·çÇÁ ±¸Á¶Ã¼ ³»¿¡ µÎ °³ÀÇ ¼¼¹ÌÄÝ·ÐÀÌ ´õÀÌ»ó ÇÊ¿äÇÏÁö ¾Ê´Ù. ´ë½Å ÄÝ·ÐÀ» »ç¿ëÇÏ¿© ¾î·¹À̳ª Collection¿¡ °¢ ¿¤¸®¸ÕÆ®¸¦ À§ÇØ ·çÇÎ ÇÒ for-each ±¸Á¶Ã¼¸¦ °®°ÔµÈ´Ù:

Listing 6. for ·çÇÁ

  public class ForEach {
    enum Color { red, green, blue };

    public static void printf(String fmt, Object... args) {
      int i = 0;
      for (char c : fmt.toCharArray()) {
        if (c == '%') {
          System.out.print(args[i++]);
        } else if (c == '\n') {
          System.out.println();
        } else {
          System.out.print(c);
        }
      }
    }
    public static void main(String args[]) {
      printf("Addition: % plus % equals %\n", 1, 1, 2);
      printf("Colors are %\n", Color.VALUES);
      for (Color aColor : Color.VALUES) {
        switch(aColor) {
          case Color.red:
            System.out.println("Got red.");
            break;
          case Color.green:
            System.out.println("Got green.");
            break;
          case Color.blue:
            System.out.println("Got blue.");
            break;
        }
      }
    }
  }

Á¤Àû ¹ÝÀÔ
import Å°¿öµå µÚ¿¡ static ´Ü¾î¸¦ Ãß°¡ÇÔÀ¸·Î¼­ ¹ÝÀÔÀ» Á¤Àû ¹ÝÀÔÀ¸·Î º¯°æÇÑ´Ù. ¹ÝÀÔÀÌ Á¤ÀûÀÌ¸é ¸ðµç Á¢±Ù °¡´ÉÇÑ ¸Þ¼Òµå¿Í º¯¼öµéÀº Á¢µÎ»ç¸¦ ºÙÀÌÁö ¾Ê°í »ç¿ëµÉ ¼ö ÀÖ´Ù:

Listing 7. static imports

  import static java.awt.Color.*;

  public class ImportTest {
    public static void main(String args[]) {
      System.out.println(RED);
    }
  }

Ŭ·¡½º¿ë »ó¼ö¸¦ Á¤ÀÇÇϱâ À§ÇØ ¸¸µå´Â ÀÎÅÍÆäÀ̽ºÀÇ ºÒÇÊ¿äÇÑ ±¸ÇöÀ» ÇÇÇÒ ¼ö ÀÖ´Ù´Â °ÍÀÌ ¶Ç ´Ù¸¥ ÀÌÀ¯ÀÌ´Ù.

Âü°íÀÚ·á

¸ñ Â÷:
ÇÁ·ÎÅäŸÀÔ ¼³Ä¡Çϱâ
generics »ç¿ëÇϱâ
½Ã½ºÅÛ Áö¿øÀ» ¹Þ´Â enumeration
º¯¼ö ÀÎÀÚ ¸®½ºÆ®
autoboxing ´Ù·ç±â
Çâ»óµÈ for ·çÇÁ
Á¤Àû ¹ÝÀÔ
Âü°í ÀÚ·á
ÇÊÀÚ ¼Ò°³
±â»ç¿¡ ´ëÇÑ Æò°¡
°ü·Ã dW ¸µÅ©:
Catching more errors at compile time with Generic Java
Java language essentials
Diagnosing Java code: The case for static types
Subscribe to the developerWorks newsletter
US ¿ø¹® Àбâ
ÇÊÀÚ¼Ò°³
John Zukowski´Â JZ Ventures, Inc.¿¡¼­ ÀÚ¹Ù Àü·« ÄÁ¼³ÆÃÀ» ´ã´çÇÏ°í ÀÖ´Ù. ÃֽŠÀú¼­·Î´Â Mastering Java 2, J2SE 1.4 (Sybex, April 2002)¿Í Learn Java with JBuilder 6 (Apress, March 2002)ÀÌ ÀÖ´Ù.
ÀÌ ±â»ç¿¡ ´ëÇÏ¿© ¾î¶»°Ô »ý°¢ÇϽʴϱî?

Á¤¸» ÁÁ´Ù (5) ÁÁ´Ù (4) ±×Àú±×·¸´Ù (3) ¼öÁ¤º¸¿ÏÀÌ ÇÊ¿äÇÏ´Ù(2) ÇüÆí¾ø´Ù (1)

  È¸»ç¼Ò°³  |  °³ÀÎÁ¤º¸ º¸È£Á¤Ã¥  |  ¹ý·ü  |  ¹®ÀÇ