J2SE 5 0 - Technical Session
J2SE 5 0 - Technical Session
Session
Topics to be covered.
I. Generics
II. Enhanced for Loop
III. Autoboxing/unboxing
IV. Typesafe Enumerations
V. Static Imports
VI. Metadata
VII. Variable Arguments (Enhancement)
VIII. Enhanced Input
Others
All rights reserved to
[email protected]
I- Generics
Generics, which are also known as
parameterized types
When you get an element from a
collection, you have to cast
Casting is a pain
Casting is unsafe—casts may fail at
runtime
Wouldn’t it be nice if you could tell the
compiler what type a collection holds?
Compiler could put in the casts for you
They’d be guaranteed* to succeed
All rights reserved to
[email protected]
Generics
Filtering a Collection—Today
// Removes 4-letter words from c; elements must be strings
static void expurgate(Collection c) {
for (Iterator i = c.iterator(); i.hasNext(); )
if(((String) i.next()).length() == 4)
i.remove();
}
// Alternative form - a bit prettier?
static void expurgate(Collection c) {
for (Iterator i = c.iterator(); i.hasNext(); ) {
String s = (String) i.next();
if(s.length() == 4)
i.remove();
}
}
All rights reserved to
[email protected]
Generics
int n = reader.nextInt();
All rights reserved to
[email protected]
Others
http://java.sun.com/developer/techni
http://java.sun.com/developer/comm
http://java.sun.com/features/2003/05