Java 5 New Feature
이세우
http://blog.xcoda.net
Twitter : @excoda
Agenda
Generics
Enhanced for Loop
Auto-Boxing/Un-Boxing
Typesafe Enums
Variable size arguments
Static Import
Annotation(Metadata)
Formatted Output
Generics
Adding Compile Time Type-Safe
Elimination Drudgery Class Casting
List list = new ArrayList();
….
String s = (String) list.get(0);
Old Style
List<String> list = new ArrayList<String>();
….
String s = list.get(0)
New Style
Enhanced for Loop
Elimination Prone Bounds Errors
String arr = new String[]{“hello”, “world”};
for(int i = 0; i < arr.length; i++){
System.out.println(arr[i]);
}
Old Style
String arr = new String[]{“hello”, “world”};
for( String s : arr){
System.out.println(s);
}
New Style
Auto-Boxing/Un-Boxing
Auto Conversion Between Primitives and Wrappers
int i = 42;
Integer itg = new Integer(i);
int j = itg.intValue();
Old Style
Integer itg = 42;
int i = itg;
New Style
Auto-Boxing with Generics and For-Each
Auto Conversion Between Primitives and Wrappers
List list = new ArrayList();
…
int sum = 0;
Iterator iter = list.iterator();
while(iter.hasNext()){
Integer itgr = (Integer) iter.next();
sum += itger.intValue();
}
Old Style
List list = new ArrayList();
…
int sum = 0;
for(int i : list){
sum+=i;
}
New Style
Typesafe Enums
Safer alternative to constants
public class Dialog{
public static final int SHORT = 0;
public static final int LONG = 1;
public static void show(int duration){
if(duration == SHORT)…
else(duration == LONG) ….
}
}
…
…
Dialog.show(Dialog.LONG);
Dialog.show(3); // Problem
Old Style
public class Dialog{
public enum Duration{LONG, SHORT};
public static void show(Duration e){
if(e== Duration.SHORT)…
else(e == Duration.LONG) ….
}
}
…
…
Dialog.show(Duration.LONG);
Dialog.show(3); // Compile Error
New Style
Varargs
Variable size argument lists for methods
void orderCar(int modelNo, List options){ }
List options = new ArrayList();
options.add(“navigation”);
options.add(“smart key”);
options.add(“hi pass”);
odrderCar(Benz.E_CLASS, options);
Old Style
void orderCar(int modelNo, String … options){ }
odrderCar(Benz.E_CLASS, “navigation”, “smart key”, “hi pass”);
New Style
Static Import
Avoiding qualifying static members
import java.lang.Math;
….
Math.random();
Math.PI;
Old Style
import java.lang.Math.*;
…
Random();
PI;
New Style
Annotation (Metadata)
Attach extra information about code Compiler Check
Code Analysis
@Override
Public boolean equals(Object obj)……
@Override
@Deprecated
Public doSomething()….
@Deprecated
http://www.ibm.com/developerworks/kr/library/j-annotate1/index.html
http://www.ibm.com/developerworks/kr/library/j-annotate2.html
@SupressWarning ({ “unchecked”, “fallthrough”})
@SupressWarnings
Formatted Output
Printf(), String.format()
Sysem.out.printf(“Hello %s of %s%n”, “World”, “Java”);
String s = String.format(“The meaning of the %s is %d”, “universe”, 42);
Examples
Format Description Format Description Format Description
%b Boolean data %c Charactor %d Decimal
%e Big Deciaml %f Float-point %x Hex-Decimal
%o As Octal %s String %n New Line
%t Date/time %% Percent
Top Related