06-11-2012, 11:52 AM
Forthcoming Java™ Programming Language Features
Forthcoming Java.pdf (Size: 720.39 KB / Downloads: 19)
Generics
• 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
Enhanced for Loop (“foreach”)
• Iterating over collections is a pain
• Often, iterator unused except to get elements
• Iterators are error-prone
─ Iterator variable occurs three times per loop
─ Gives you two opportunities to get it wrong
─ Common cut-and-paste error
• Wouldn’t it be nice if the compiler took care
of the iterator for you?
Current Solution –
Typesafe Enum Pattern
• “Effective Java Programming Language Guide”
• Basic idea - class that exports self-typed
constants and has no public constructor
• Fixes all disadvantages of int pattern
• Other advantages
─ Can add arbitrary methods, fields
─ Can implement interfaces
Metadata (Annotations)
• Many APIs require a fair amount of boilerplate
─ Example: JAX-RPC web service requires
paired interface and implementation
• Wouldn’t it be nice if language let you annotate
code so that tool could generate boilerplate?
• Many APIs require “side files” to be maintained
─ Example: bean has BeanInfo class
• Wouldn’t it be nice if language let you annotate
code so that tools could generate side files?