new features in jdk 1.5 can these new and complex features simplify java development?...
TRANSCRIPT
![Page 1: New features in JDK 1.5 Can these new and complex features simplify Java development? dave@marquam.org](https://reader036.vdocument.in/reader036/viewer/2022070306/5519bf055503467a578b4add/html5/thumbnails/1.jpg)
New features in JDK 1.5
Can these new and complex features simplify Java
development?
![Page 2: New features in JDK 1.5 Can these new and complex features simplify Java development? dave@marquam.org](https://reader036.vdocument.in/reader036/viewer/2022070306/5519bf055503467a578b4add/html5/thumbnails/2.jpg)
Overview
• Generic Types• Auto-boxing/unboxing• Enhanced For Loop• Enumerations• Variable Arguments• Static Import• Meta-data
• Formatted I/O• Concurrency Utilities• Management Utilities• Class Data Sharing• Loads of other
improvements
![Page 3: New features in JDK 1.5 Can these new and complex features simplify Java development? dave@marquam.org](https://reader036.vdocument.in/reader036/viewer/2022070306/5519bf055503467a578b4add/html5/thumbnails/3.jpg)
Reasoning
"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
![Page 4: New features in JDK 1.5 Can these new and complex features simplify Java development? dave@marquam.org](https://reader036.vdocument.in/reader036/viewer/2022070306/5519bf055503467a578b4add/html5/thumbnails/4.jpg)
Complementary Features
• Generics improve enhanced for loop
• Generics and auto-boxing/unboxing allow Enumerated types
• Variable args improve API usability
• Variable args allow formatted I/O
![Page 5: New features in JDK 1.5 Can these new and complex features simplify Java development? dave@marquam.org](https://reader036.vdocument.in/reader036/viewer/2022070306/5519bf055503467a578b4add/html5/thumbnails/5.jpg)
New features enhance existing API• Generics enhance compile time type
checking
• Generics improve reflection
• Generics allow return type overriding
• Enum improves readability and organization of constants
• Static import improves utility functions
• Auto-boxing improves readability
![Page 6: New features in JDK 1.5 Can these new and complex features simplify Java development? dave@marquam.org](https://reader036.vdocument.in/reader036/viewer/2022070306/5519bf055503467a578b4add/html5/thumbnails/6.jpg)
Generic Types
interface Collection <E> {
boolean add(E o);
boolean addAll(Collection<? extends E> c);
Iterator<E> iterator();
<T> T[] toArray(T[] a);
…
}
![Page 7: New features in JDK 1.5 Can these new and complex features simplify Java development? dave@marquam.org](https://reader036.vdocument.in/reader036/viewer/2022070306/5519bf055503467a578b4add/html5/thumbnails/7.jpg)
Generic Interface (or Class)interface Collection <E> …
Collection<Integer> integers;
E is the type parameter for the generic type
Collection is the raw type after type erasure
Integer is the actual type parameter used when declaring an instance of the generic type
Cannot use primitive types but auto-boxing covers over the difference
![Page 8: New features in JDK 1.5 Can these new and complex features simplify Java development? dave@marquam.org](https://reader036.vdocument.in/reader036/viewer/2022070306/5519bf055503467a578b4add/html5/thumbnails/8.jpg)
Using the Type Parameter boolean add(E o);
Where ever E is used, the compiler will check the type against the actual type parameter. At runtime E will be Object.
![Page 9: New features in JDK 1.5 Can these new and complex features simplify Java development? dave@marquam.org](https://reader036.vdocument.in/reader036/viewer/2022070306/5519bf055503467a578b4add/html5/thumbnails/9.jpg)
Wildcard Parametersboolean addAll(Collection<? extends E> c);
? represents an unknown type
List<?> list = new ArrayList<String>();
Not legal:
List<Object> list = new ArrayList<String>();
Would allow list.add( new Object() );
![Page 10: New features in JDK 1.5 Can these new and complex features simplify Java development? dave@marquam.org](https://reader036.vdocument.in/reader036/viewer/2022070306/5519bf055503467a578b4add/html5/thumbnails/10.jpg)
Bounded Type Parametersboolean addAll(Collection<? extends E> c);
Constrains possible actual types
An actual type must be same as E or extend E
Can also constrain to implement multiple interfaces
![Page 11: New features in JDK 1.5 Can these new and complex features simplify Java development? dave@marquam.org](https://reader036.vdocument.in/reader036/viewer/2022070306/5519bf055503467a578b4add/html5/thumbnails/11.jpg)
Parameterized Methods<T> T[] toArray(T[] a);
Infers the actual type from the actual parameter passed to the method.
![Page 12: New features in JDK 1.5 Can these new and complex features simplify Java development? dave@marquam.org](https://reader036.vdocument.in/reader036/viewer/2022070306/5519bf055503467a578b4add/html5/thumbnails/12.jpg)
Type Erasure
• Insures backwards compatibility
• Avoids bloat ala C++ templates
• Type information still available through reflection
![Page 13: New features in JDK 1.5 Can these new and complex features simplify Java development? dave@marquam.org](https://reader036.vdocument.in/reader036/viewer/2022070306/5519bf055503467a578b4add/html5/thumbnails/13.jpg)
Small Examplepublic static void main ( String... args ) {
Collection<String> strs = new ArrayList<String>();
for ( String s : args )
strs.add( s );
Collection<?> readonly = new ArrayList<String> ( strs );
//readonly.add( "some string" ); Not allowed
Collection<String> copy = new ArrayList<String> (strs);
String[] strAry = strs.toArray( new String[] {} );
}
![Page 14: New features in JDK 1.5 Can these new and complex features simplify Java development? dave@marquam.org](https://reader036.vdocument.in/reader036/viewer/2022070306/5519bf055503467a578b4add/html5/thumbnails/14.jpg)
Auto-boxing/unboxing
List<Integer> ints = new ArrayList<Integer>();
ints.add( 1 ); // auto-boxing to new Integer( 1 )
ints.add( 2 );
ints.add( 3 );
for ( int num : ints ) // Auto-unboxing using obj.intValue()
System.out.println( num );
![Page 15: New features in JDK 1.5 Can these new and complex features simplify Java development? dave@marquam.org](https://reader036.vdocument.in/reader036/viewer/2022070306/5519bf055503467a578b4add/html5/thumbnails/15.jpg)
Enhanced for loop (foreach)
• Improves readability
• Reduces boilerplate code
• Works with both arrays and objects that expose an iterator
![Page 16: New features in JDK 1.5 Can these new and complex features simplify Java development? dave@marquam.org](https://reader036.vdocument.in/reader036/viewer/2022070306/5519bf055503467a578b4add/html5/thumbnails/16.jpg)
Enumerated Types• Collects related constants into own
namespace
• Codifies the type-safe enum idiom
• Can have constructors, fields and methods
• Can subclass
• Works well with switch statements
• Adds enum keyword to language. May require source changes before compiling with 1.5 compiler. Existing classes should still work.
![Page 17: New features in JDK 1.5 Can these new and complex features simplify Java development? dave@marquam.org](https://reader036.vdocument.in/reader036/viewer/2022070306/5519bf055503467a578b4add/html5/thumbnails/17.jpg)
Enum exampleenum Suit {clubs, diamonds, hearts, spades}
enum Rank {deuce, three, four, five, six, seven,
eight, nine, ten, jack, queen, king, ace}
List<Card> deck = new ArrayList<Card>();
for (Suit suit : Suit.VALUES)
for (Rank rank : Rank.VALUES)
deck.add(new Card(suit, rank));
Collections.shuffle(deck);
![Page 18: New features in JDK 1.5 Can these new and complex features simplify Java development? dave@marquam.org](https://reader036.vdocument.in/reader036/viewer/2022070306/5519bf055503467a578b4add/html5/thumbnails/18.jpg)
Switch with EnumColor getColor( Suit suit ) {
switch( suit ) {
case clubs:
case spades: return Color.Black;
case diamonds:
case hearts: return Color.Red;
default: throw new AssertionError( suit + “ not valid value” );
}
}
![Page 19: New features in JDK 1.5 Can these new and complex features simplify Java development? dave@marquam.org](https://reader036.vdocument.in/reader036/viewer/2022070306/5519bf055503467a578b4add/html5/thumbnails/19.jpg)
Variable ArgumentsString format( String format, Object... args );
format(“{2} {1}!”, “World”, “Hello”);
• Nicer way to call a method that takes an array of some type
• Class…means Class[] inside of the method
• Use the enhanced for loop to iterate
![Page 20: New features in JDK 1.5 Can these new and complex features simplify Java development? dave@marquam.org](https://reader036.vdocument.in/reader036/viewer/2022070306/5519bf055503467a578b4add/html5/thumbnails/20.jpg)
Static Importimport static java.lang.math.*;
…
int themax = max( 123123, 23475 );
• Reduces typing
• Alleviates need for implementing a Constant Interface
• Compiler error if names clash
![Page 21: New features in JDK 1.5 Can these new and complex features simplify Java development? dave@marquam.org](https://reader036.vdocument.in/reader036/viewer/2022070306/5519bf055503467a578b4add/html5/thumbnails/21.jpg)
Meta-data• Available at compile-time or runtime with
reflection
• Similar concept to XDoclet tags or .Net attributes
• @deprecated and @overrides are standard
![Page 22: New features in JDK 1.5 Can these new and complex features simplify Java development? dave@marquam.org](https://reader036.vdocument.in/reader036/viewer/2022070306/5519bf055503467a578b4add/html5/thumbnails/22.jpg)
References
Java Generics Tutorialhttp://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf
J2SE 1.5 in a Nutshell http://java.sun.com/developer/technicalArticles/releases/j2se15
A Conversation with Joshua Bloch http://java.sun.com/features/2003/05/bloch_qa.html
What's new in J2SE 1.5http://wiki.schubart.net/wiki/wiki.phtml?title=What's_new_in_J2SE_1.5
David Flanagan Web loghttp://www.davidflanagan.com/