Taming Tiger!! (1/3) - 진화된 JavaSE 5편

annotation에 대해서 공부하다가. javaworld에 java 1.5에 대한 기사가 있어서 간단히 정리해본다. (http://www.javaworld.com/javaworld/jw-04-2004/jw-0426-tiger1.html)

An Introduction to Java 1.5

아래의 것을 Eclipse에서 제대로 동작 시키려면 Project Properties에서 Java Compiler를 5.0이상으로 세팅해야한다.

1. compile-time type safety checking

   Compile Time에 Class Casting에 대한 예외처리를 할 수 있다. 예전에는 Runtime시에 ClassCastException이 발생했었다. 이제는 compile-time type safety checking이 가능!!

2. Autoboxing & unboxing

   Collection에 객체를 넣을때 미리 그 안의 객체를 선언시에 정의한다. (진짜 편하고 좋더라.)

3. 달라진 For문(hehanced for)과 파라미터

-------------------------------------------------

public int sum(Integer[] numbers)
 {
    int mysum = 0;  
    for(int i: numbers)
       mysum += i;
       return mysum;
 }

-------------------------------------------------

 public int sum2(Integer... numbers)
 {
       int mysum = 0;   
       for(int i: numbers)
          mysum += i;
       return mysum;
 }

-------------------------------------------------

4. C언어의 printf style지원

 public void printf()
 {
 //  Using System.out.println and System.out.printf
  int x = 5;
  int y = 6;
  int sum = x + y;
 //  Pre 1.5 print statement
  System.out.println(x + " + " + y + " = " + sum);
 //  Using 1.5 printf
  System.out.printf("%d + %d = %d\n", x, y, sum);
 }

5. C언어의 enum지원

public enum Color
{
   Red, White, Blue;
}


// usage

Color myColor = Color.Red;

for(Color c: Color.values())
{
   System.out.println(c);
}

--------------------------------------------------------

이렇게 확장도 가능하네..

public enum Color
{
    Red("Red"), White("White"), Blue("Blue")
    {
        public boolean isInUSFlag()
       {
            return false;
        }
    };

 private String m_strName;
 Color(String s)
 {
     m_strName = s;
 }

 public String toString()
 {
     return m_strName;
 }

 public boolean isInUSFlag()
 {
     return true;
 }
}

6. static imports

java.lang.Math 하위에 있는 모든 메소드를 사용한 클래스 자신의 것처럼 쓰고 싶을때..

그런데 이것은 메소드의 출처가 불분명해 진다는 문제가 좀 있겠다. 사용을 자제해야할듯.

import static java.lang.Math.*;

public class StaticImport
{
     public static void main(String[] args)
     {
          double x = 10.12;
          double y = ceil(x);
      }
}

이 글과 관련있는 글을 자동검색한 결과입니다 [?]

by Jipsin | 2006/12/18 17:56 | Java/Essential | 트랙백 | 덧글(1)

트랙백 주소 : http://jipsin08.egloos.com/tb/697071
☞ 내 이글루에 이 글과 관련된 글 쓰기 (트랙백 보내기) [도움말]
Commented by Naomi at 2007/04/06 01:44
hello

:         :

:

비공개 덧글

<< 이전 페이지다음 페이지 >>