New features in JDK 1.5 - PowerPoint PPT Presentation

About This Presentation
Title:

New features in JDK 1.5

Description:

In other words, they shift the responsibility for writing the ... case hearts: return Color.Red; default: throw new AssertionError( suit ' not valid value' ... – PowerPoint PPT presentation

Number of Views:118
Avg rating:3.0/5.0
Slides: 23
Provided by: indy2
Category:
Tags: jdk | features | hearts | new | red

less

Transcript and Presenter's Notes

Title: New features in JDK 1.5


1
New features in JDK 1.5
  • Can these new and complex features simplify Java
    development?

dave_at_marquam.org
2
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

3
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

4
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

5
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

6
Generic Types
  • interface Collection ltEgt
  • boolean add(E o)
  • boolean addAll(Collectionlt? extends Egt c)
  • IteratorltEgt iterator()
  • ltTgt T toArray(T a)

7
Generic Interface (or Class)
  • interface Collection ltEgt
  • CollectionltIntegergt 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

8
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.

9
Wildcard Parameters
  • boolean addAll(Collectionlt? extends Egt c)
  • ? represents an unknown type
  • Listlt?gt list new ArrayListltStringgt()
  • Not legal
  • ListltObjectgt list new ArrayListltStringgt()
  • Would allow list.add( new Object() )

10
Bounded Type Parameters
  • boolean addAll(Collectionlt? extends Egt c)
  • Constrains possible actual types
  • An actual type must be same as E or extend E
  • Can also constrain to implement multiple
    interfaces

11
Parameterized Methods
  • ltTgt T toArray(T a)
  • Infers the actual type from the actual parameter
    passed to the method.

12
Type Erasure
  • Insures backwards compatibility
  • Avoids bloat ala C templates
  • Type information still available through
    reflection

13
Small Example
  • public static void main ( String... args )
  • CollectionltStringgt strs new
    ArrayListltStringgt()
  • for ( String s args )
  • strs.add( s )
  • Collectionlt?gt readonly new ArrayListltStringgt
    ( strs )
  • //readonly.add( "some string" ) Not allowed
  • CollectionltStringgt copy new ArrayListltStringgt
    (strs)
  • String strAry strs.toArray( new String
    )

14
Auto-boxing/unboxing
  • ListltIntegergt ints new ArrayListltIntegergt()
  • 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 )

15
Enhanced for loop (foreach)
  • Improves readability
  • Reduces boilerplate code
  • Works with both arrays and objects that expose an
    iterator

16
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.

17
Enum example
  • enum Suit clubs, diamonds, hearts, spades
  • enum Rank deuce, three, four, five, six, seven,
  • eight, nine, ten, jack,
    queen, king, ace
  • ListltCardgt deck new ArrayListltCardgt()
  • for (Suit suit Suit.VALUES)
  • for (Rank rank Rank.VALUES)
  • deck.add(new Card(suit, rank))
  • Collections.shuffle(deck)

18
Switch with Enum
  • Color 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 )

19
Variable Arguments
  • String format( String format, Object... args )
  • format(2 1!, World, Hello)
  • Nicer way to call a method that takes an array of
    some type
  • Classmeans Class inside of the method
  • Use the enhanced for loop to iterate

20
Static Import
  • import static java.lang.math.
  • int themax max( 123123, 23475 )
  • Reduces typing
  • Alleviates need for implementing a Constant
    Interface
  • Compiler error if names clash

21
Meta-data
  • Available at compile-time or runtime with
    reflection
  • Similar concept to XDoclet tags or .Net
    attributes
  • _at_deprecated and _at_overrides are standard

22
References
  • Java Generics Tutorial
  • http//java.sun.com/j2se/1.5/pdf/generics-tutorial
    .pdf
  • J2SE 1.5 in a Nutshell
  • http//java.sun.com/developer/technicalArticles/re
    leases/j2se15
  • A Conversation with Joshua Bloch
  • http//java.sun.com/features/2003/05/bloch_qa.html
  • What's new in J2SE 1.5
  • http//wiki.schubart.net/wiki/wiki.phtml?titleWha
    t's_new_in_J2SE_1.5
  • David Flanagan Web log
  • http//www.davidflanagan.com/
Write a Comment
User Comments (0)
About PowerShow.com