Java 5 New Feature
-
Upload
xcoda -
Category
Technology
-
view
4.324 -
download
1
description
Transcript of Java 5 New Feature
![Page 1: Java 5 New Feature](https://reader030.fdocuments.net/reader030/viewer/2022020217/549e9407b379597b4b8b461a/html5/thumbnails/1.jpg)
Java 5 New Feature
이세우
http://blog.xcoda.net
Twitter : @excoda
![Page 2: Java 5 New Feature](https://reader030.fdocuments.net/reader030/viewer/2022020217/549e9407b379597b4b8b461a/html5/thumbnails/2.jpg)
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](https://reader030.fdocuments.net/reader030/viewer/2022020217/549e9407b379597b4b8b461a/html5/thumbnails/3.jpg)
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](https://reader030.fdocuments.net/reader030/viewer/2022020217/549e9407b379597b4b8b461a/html5/thumbnails/4.jpg)
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](https://reader030.fdocuments.net/reader030/viewer/2022020217/549e9407b379597b4b8b461a/html5/thumbnails/5.jpg)
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](https://reader030.fdocuments.net/reader030/viewer/2022020217/549e9407b379597b4b8b461a/html5/thumbnails/6.jpg)
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](https://reader030.fdocuments.net/reader030/viewer/2022020217/549e9407b379597b4b8b461a/html5/thumbnails/7.jpg)
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](https://reader030.fdocuments.net/reader030/viewer/2022020217/549e9407b379597b4b8b461a/html5/thumbnails/8.jpg)
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](https://reader030.fdocuments.net/reader030/viewer/2022020217/549e9407b379597b4b8b461a/html5/thumbnails/9.jpg)
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 10: Java 5 New Feature](https://reader030.fdocuments.net/reader030/viewer/2022020217/549e9407b379597b4b8b461a/html5/thumbnails/10.jpg)
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
![Page 11: Java 5 New Feature](https://reader030.fdocuments.net/reader030/viewer/2022020217/549e9407b379597b4b8b461a/html5/thumbnails/11.jpg)
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