Java 5 New Features 13 Jun21 New features

  • Slides: 16
Download presentation
Java 5 New Features 13 -Jun-21

Java 5 New Features 13 -Jun-21

New features (Not all) n Generics n n Enhanced for loop n n Provides

New features (Not all) n Generics n n Enhanced for loop n n Provides all the well-known benefits of the Typesafe Enum pattern Static import n n Avoids manual conversion between primitive types (such as int) and wrapper types (such as Integer) Typesafe enums n n Eliminates the drudgery and error-proneness of iterators Autoboxing/unboxing n n Compile-time type safety for collections without casting Lets you avoid qualifying static members with class names Metadata n n 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

Compiler Sugar n Compiler helps you n n n That is, you can key

Compiler Sugar n Compiler helps you n n n 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!

Generics n n A generic is a method that is recompiled with different types

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

Enhanced for loop n Instead of void cancel. All(Collection c) { for (Iterator i

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

Autoboxing n n n Java won’t let you use a primitive value where an

Autoboxing n n n 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: n Map<String, Integer> m = new Tree. Map<String, Integer>(); for (String word : args) { m. put(word, m. get(word) + 1); }

Enumerations n n An enumeration, or “enum, ” is simply a set of constants

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

Advantages of the new enum n They provide compile-time type safety n n They

Advantages of the new enum n They provide compile-time type safety n n They provide a proper name space for the enumerated type n n n int enums are compiled into clients, and you have to recompile clients if you add, remove, or reorder constants. Printed values are informative n n With int enums you have to prefix the constants to get any semblance of a name space. They're robust n n int enums don't provide any type safety at all 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

New features of enum n public enum Coin { PENNY(1), NICKEL(5), DIME(10), QUARTER(25); Coin(int

New features of enum n 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; } }

Another Interesting Example public enum Operation { PLUS { double eval(double x, double y)

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); }

public static void main(String args[]) { double x = Double. parse. Double(args[0]); double y

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

Static import facility import static java. lang. System. out; public class Test { public

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

scanf & printf Scanner sc = new Scanner(System. in); int i = sc. next.

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

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

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

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

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

Reference n n http: //java. sun. com/j 2 se/1. 5. 0/docs/relnotes/features. html (All new

Reference n n http: //java. sun. com/j 2 se/1. 5. 0/docs/relnotes/features. html (All new features, hard) http: //www. cs. indiana. edu/classes/jett/sstamm/