java. Java6 의 특징에 대한 예제 코드

java8 도 나온 시점에 java6 이라니 싶지만
예전에 정리해놓은 코드가 있어서 올려놓습니다.

public enum GENDER{ M, F };

public static void main( String[] args ){
 //1.Generic
 ArrayList< String > strList = new ArrayList< String >();
 strList.add( "ABC" );
 strList.add( "DEF" );
 for( int i = 0; i < strList.size(); i++ ){
  String str = strList.get( i );
  System.out.println( str );//ABC, DEF
 }//for
 
 //2. AutoBoxing, AutoUnboxing 
 Integer i1 = 3;
 int i2 = new Integer( 4 );
 System.out.println( i1 + i2 );//7
 
 //3. Loop syntax
 String[] arrStr = new String[]{ "a", "b" };
 for( String str : arrStr ){
  System.out.println( str );//a, b
 }//for
 
 //4. enum type
 GENDER g = GENDER.F;
 System.out.println( g );//F
 printGender( g );
 
 //5. Dynamic arguments
 printArgs( 1, 2, 3 );
 printArgs( 1, 4 );
 printArgs( new Integer( 3 ), 125 );
 
 //6. Formatting
 System.out.printf( "%7.3g", Math.PI );//   3.14
 System.out.println();
 
 //7. Scripting language support
 ScriptEngineManager sem = new ScriptEngineManager();
 ScriptEngine se = sem.getEngineByName( "javascript" );
 try{
  se.eval( "var a = 'hello';print( a );" );//hello
 }catch( Exception ex ){
  ex.printStackTrace();
 }//try
}//method

public static void printGender( GENDER g ){
 if( g == GENDER.F ){
  System.out.println( "WOMAN" );
 }else{
  System.out.println( "MAN" );
 }//if
}//method

public static void printArgs( Integer... intg ){
 for( int i = 0; i < intg.length; i++ ){
  System.out.println( intg[ i ] );
 }//for
}//method

다른글 읽기