Java 5 New Feature

7,569 views

Published on

Java 5 New Feature from Java2

Published in: Technology, News & Politics
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
7,569
On SlideShare
0
From Embeds
0
Number of Embeds
4,890
Actions
Shares
0
Downloads
25
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Java 5 New Feature

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

×