Java 5 New Feature

11
Java 5 New Feature 이세우 [email protected] http://blog.xcoda.net Twitter : @excoda

description

Java 5 New Feature from Java2

Transcript of Java 5 New Feature

Page 1: Java 5 New Feature

Java 5 New Feature

이세우

[email protected]

http://blog.xcoda.net

Twitter : @excoda

Page 2: Java 5 New Feature

Agenda

Generics

Enhanced for Loop

Auto-Boxing/Un-Boxing

Typesafe Enums

Variable size arguments

Static Import

Annotation(Metadata)

Formatted Output

Page 3: Java 5 New Feature

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

Page 4: Java 5 New Feature

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

Page 5: Java 5 New Feature

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

Page 6: Java 5 New Feature

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

Page 7: Java 5 New Feature

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

Page 8: Java 5 New Feature

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

Page 9: Java 5 New Feature

Static Import

Avoiding qualifying static members

import java.lang.Math;

….

Math.random();

Math.PI;

Old Style

import java.lang.Math.*;

Random();

PI;

New Style

Page 11: Java 5 New Feature

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