Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java 5 New Features 1-May-19.

Similar presentations


Presentation on theme: "Java 5 New Features 1-May-19."— Presentation transcript:

1 Java 5 New Features 1-May-19

2 New features (Not all) Generics Enhanced for loop Autoboxing/unboxing
Compile-time type safety for collections without casting Enhanced for loop Eliminates the drudgery and error-proneness of iterators Autoboxing/unboxing Avoids manual conversion between primitive types (such as int) and wrapper types (such as Integer) Typesafe enums Provides all the well-known benefits of the Typesafe Enum pattern Static import Lets you avoid qualifying static members with class names Metadata Tools to generate boilerplate code from annotations in the source code Leads to a "declarative" programming style where the programmer says what should be done and tools emit the code to do it

3 Compiler Sugar Compiler helps you
That is, you can key in less words But it does not actually extend the power of the language itself! Many new features are provided by compiler sugars, but not all!

4 Generics A generic is a method that is recompiled with different types as the need arises The bad news: Instead of saying: List words = new ArrayList(); You'll have to say: List<String> words = new ArrayList<String>(); The good news: Provides compile-time checking to make sure you are using the correct type No casting; instead of String title = ((String) words.get(i)).toUppercase(); you use String title = words.get(i).toUppercase();

5 Enhanced for loop Instead of void cancelAll(Collection c) { for (Iterator i = c.iterator(); i.hasNext(); ) { TimerTask tt = (TimerTask) i.next(); tt.cancel(); } } You will be able to use: void cancelAll(Collection c) { for (Object o : c) ((TimerTask)o).cancel(); } Or: void cancelAll(Collection<TimerTask> c) { for (TimerTask task : c) task.cancel(); } Not everyone likes this syntax!

6 Autoboxing Java won’t let you use a primitive value where an object is required--you need a “wrapper” Similarly, you can’t use an object where a primitive is required--you need to “unwrap” it Java 1.5 makes this automatic: Map<String, Integer> m = new TreeMap<String, Integer>(); for (String word : args) { m.put(word, m.get(word) + 1); }

7 Enumerations An enumeration, or “enum,” is simply a set of constants to represent various values Here’s the old way of doing it public final int SPRING = 0; public final int SUMMER = 1; public final int FALL = 2; public final int WINTER = 3; This is a nuisance, and is error prone as well Here’s the new way of doing it: enum Season { winter, spring, summer, fall }

8 Advantages of the new enum
They provide compile-time type safety int enums don't provide any type safety at all They provide a proper name space for the enumerated type With int enums you have to prefix the constants to get any semblance of a name space. They're robust int enums are compiled into clients, and you have to recompile clients if you add, remove, or reorder constants. Printed values are informative If you print an int enum you just see a number. Because they're objects, you can put them in collections. Because they're essentially classes, you can add arbitrary fields and methods

9 New features of enum public enum Coin { PENNY(1), NICKEL(5), DIME(10), QUARTER(25); Coin(int value) { this.value = value; } private final int value; public int value() { return value; } }

10 Another Interesting Example
public enum Operation { PLUS { double eval(double x, double y) { return x + y; } }, MINUS { double eval(double x, double y) { return x - y; } }, TIMES { double eval(double x, double y) { return x * y; } }, DIVIDE { double eval(double x, double y) { return x / y; } }; // Do arithmetic op represented by this constant abstract double eval(double x, double y); }

11 public static void main(String args[]) { double x = Double
public static void main(String args[]) { double x = Double.parseDouble(args[0]); double y = Double.parseDouble(args[1]); for (Operation op : Operation.values()) System.out.printf("%f %s %f = %f%n", x, op, y, op.eval(x, y)); } $ java Operation PLUS = MINUS = TIMES = DIVIDE = 怎麼改成印+-*/

12 Static import facility
import static java.lang.System.out; public class Test { public static void main(String...args) { out.println("hello world"); } You no longer have to say System.out.println

13 scanf & printf Scanner sc = new Scanner(System.in); int i = sc.nextInt(); String name = sc.next(); System.out.printf("[%s]我今年%d歲", "popcorny", 18); String str = String.format("[%s]屁啦~", "moliwang");

14 Variable length parameters
public static void main(String[] args) public static void main(String… args)

15 Metadata @Override To guarantee you “override” a method. That is, typo is not possible

16 Reference (All new features, hard)


Download ppt "Java 5 New Features 1-May-19."

Similar presentations


Ads by Google