0% found this document useful (0 votes)
33 views

46 Java 1.5

The document summarizes new features in Java 1.5 including generics, enhanced for loops, autoboxing/unboxing, typesafe enums, static import, and metadata. Generics allow compile-time type safety for collections without casting. Enhanced for loops simplify iterating over collections. Autoboxing/unboxing automatically converts between primitive types and their corresponding wrapper classes. Typesafe enums provide compile-time type safety and other advantages over traditional integer-based enums. Static import allows importing static members without class qualification. Metadata uses annotations to generate boilerplate code.

Uploaded by

Anjani Kumar Jha
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

46 Java 1.5

The document summarizes new features in Java 1.5 including generics, enhanced for loops, autoboxing/unboxing, typesafe enums, static import, and metadata. Generics allow compile-time type safety for collections without casting. Enhanced for loops simplify iterating over collections. Autoboxing/unboxing automatically converts between primitive types and their corresponding wrapper classes. Typesafe enums provide compile-time type safety and other advantages over traditional integer-based enums. Static import allows importing static members without class qualification. Metadata uses annotations to generate boilerplate code.

Uploaded by

Anjani Kumar Jha
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 13

Java 1.

14-Mar-13

Reason for changes

The new language features all have one thing in common: they take some common idiom and provide linguistic support for it. In other words, they shift the responsibility for writing the boilerplate code from the programmer to the compiler.
--Joshua Bloch, senior staff engineer, Sun Microsystems

New features--maybe

Generics

Compile-time type safety for collections without casting Eliminates the drudgery and error-proneness of iterators Avoids manual conversion between primitive types (such as int) and wrapper types (such as Integer) Provides all the well-known benefits of the Typesafe Enum pattern

Enhanced for loop

Autoboxing/unboxing

Typesafe enums

Static import

Lets you avoid qualifying static members with class names


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

Metadata

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

The good news:

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(); } void cancelAll(Collection<TimerTask> c) { for (TimerTask task : c) task.cancel(); }

Or:

Not everyone likes this syntax!

Autoboxing

Java wont let you use a primitive value where an object is required--you need a wrapper Similarly, you cant 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); }

Enumerations

An enumeration, or enum, is simply a set of constants to represent various values Heres the old way of doing it

public public public public

final final final final

int int int int

SPRING = 0; SUMMER = 1; FALL = 2; WINTER = 3;

This is a nuisance, and is error prone as well Heres the new way of doing it:

enum Season { winter, spring, summer, fall }

Advantages of the new enum

They provide compile-time type safety

int enums don't provide any type safety at all With int enums you have to prefix the constants to get any semblance of a name space. int enums are compiled into clients, and you have to recompile clients if you add, remove, or reorder constants. If you print an int enum you just see a number.

They provide a proper name space for the enumerated type

They're robust

Printed values are informative

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

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


}

Static import facility

import static org.iso.Physics.*; class Guacamole { public static void main(String[] args) { double molecules = AVOGADROS_NUMBER * moles; ... } } You no longer have to say Physics.AVOGADROS_NUMBER

Metadata

Boilerplate is code that is inserted over and over again, into many different programs The @ symbol is used to tell the compiler to fetch the code from somewhere else, and insert it It is unclear to me how else this will be used

I doubt that its just an #insert facility

Status

Java 1.5 is due out in late 2003


At this point, it seems highly unlikely that 1.5 will be out by the end of 2003 My description is from an ancient article--May 2003 It isnt easy to find out much more than this Lets hope for the best!

The End

You might also like