cs252org09
TRANSCRIPT
-
8/9/2019 CS252Org09
1/13
19-Jul-10
Java 1.5
-
8/9/2019 CS252Org09
2/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 otherwords, they shift the
responsibility forwriting the boilerplate code from theprogrammer to the compiler.
--Joshua Bloch, senior staffengineer, Sun Microsystems
-
8/9/2019 CS252Org09
3/13
New features--maybe
Generics Compile-time type safety for collectionswithout casting
Enhanced for loop
Eliminates the drudgery and error-proneness of iterators
Autoboxing/unboxing Avoids manual conversion between primitive types (such as int) andwrapper types (such as Integer)
Typesafeenums Provides all thewell-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 stylewhere the programmer says
what should be done and tools emit the code to do it
-
8/9/2019 CS252Org09
4/13
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 words = new ArrayList();
The good news:
Provides compile-time checking to make sureyou are using the correct
type
No casting; instead of
String title = ((String) words.get(i)).toUppercase();
you use
String title = words.get(i).toUppercase();
-
8/9/2019 CS252Org09
5/13
Enhanced for loop
Instead ofvoid 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 c) {for (TimerTask task : c)
task.cancel();}
Not everyone likes this syntax!
-
8/9/2019 CS252Org09
6/13
Autoboxing
Java wont let you use a primitive valuewhere 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 m =
new TreeMap();
for (String word : args) {m.put(word, m.get(word) + 1);
}
-
8/9/2019 CS252Org09
7/13
Enumerations
An enumeration, or enum, is simply a set of constants
to represent various values
Heres 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 Heres the newway of doing it:
enum Season { winter, spring, summer, fall }
-
8/9/2019 CS252Org09
8/13
Advantages of the newenum
They provide compile-time type safety
int enums don't provide any type safety at all
They provide a proper name space for theenumerated 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 Ifyou print an int enum you just see a number.
Because they're objects, you can put them in collections.
Because they'reessentially classes, you can add arbitrary fields
and methods
-
8/9/2019 CS252Org09
9/13
New features ofenum
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; }
}
-
8/9/2019 CS252Org09
10/13
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 sayPhysics.AVOGADROS_NUMBER
-
8/9/2019 CS252Org09
11/13
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 somewhereelse, and insert it It is unclear to me howelse this will be used
I doubt that its just an #insert facility
-
8/9/2019 CS252Org09
12/13
Status
Java 1.5 is due out in late 2003
At this point, it seems highly unlikely that 1.5 will be out by
theend 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!
-
8/9/2019 CS252Org09
13/13
The End