Generics Written in the Java™ Programming Language with Parameterized Types in Java Platform 5.0 Angelika Langer Trainer/Consultant www.AngelikaLanger.com TS-5627 2005 JavaOne SM Conference | Session TS-5627 1 Goal Give an overview of generics for the Java™ programming language What are generics? Language features of generics What are generics used for? Idioms for use of generics 2005 JavaOne SM Conference | Session TS-5627 | 2
Speaker’s Qualifications • Author of Java Generics FAQ online • www.AngelikaLanger.com/GenericsFAQ/ JavaGenericsFAQ.html • Independent trainer/consultant/author • Teaching C++ and Java for 10+ years 2005 JavaOne SM Conference | Session TS-5627 | 3 Agenda Language Features Usage 2005 JavaOne SM Conference | Session TS-5627 | 4
Non-Generic Collections • No homogeneous collections • Lots of casts required • No compile-time checks • Late error detection at runtime Li nkedLi st l i st = new Li nkedLi st ( ) ; l i st . add( new I nt eger ( 0) ) ; I nt eger i = ( I nt eger ) l i st . get ( 0) ; Fine at Compile-time, St r i ng s = ( St r i ng) l i st . get ( 0) ; but Fails at Runtime Casts Required 2005 JavaOne SM Conference | Session TS-5627 | 5 Generic Collections • Collections are homogeneous • No casts necessary • Early compile-time checks • Based on static type information Li nkedLi st <I nt eger > l i st = new Li nkedLi st <I nt eger > ( ) ; l i st . add( new I nt eger ( 0) ) ; I nt eger i = l i st . get ( 0) ; St r i ng s = l i st . get ( 0) ; Compile-time Error 2005 JavaOne SM Conference | Session TS-5627 | 6
Benefits of Generic Types • Increased expressive power • Improved type safety • Explicit type parameters and implicit type casts 2005 JavaOne SM Conference | Session TS-5627 | 7 Definition of Generic Types i nt er f ace Col l ect i on<A> { publ i c voi d add ( A x) ; publ i c I t er at or <A> i t er at or ( ) ; } cl ass Li nkedLi st <A> i m pl em ent s Col l ect i on<A> { pr ot ect ed cl ass Node { A el t ; Node next = nul l ; Node ( A el t ) { t hi s. el t = el t ; } } . . . } • Type variable = “placeholder” for an unknown type • Similar to a type, but not really a type • Several restrictions • Not allowed in new expressions, cannot be derived from, no class literal 2005 JavaOne SM Conference | Session TS-5627 | 8
Type Parameter Bounds publ i c i nt er f ace Com par abl e<T> { publ i c i nt com par eTo( T ar g) ; } publ i c cl ass Tr eeM ap<K ext ends Com par abl e <K>, V> { pr i vat e st at i c cl ass Ent r y<K, V> { . . . } … pr i vat e Ent r y<K, V> get Ent r y( K key) { … whi l e ( p ! = nul l ) { i nt cm p = k. com par eTo ( p. key) ; … } … } … } • Bounds = supertype of a type variable • Purpose: make available non-static methods of a type variable • Limitations: gives no access to constructors or static methods 2005 JavaOne SM Conference | Session TS-5627 | 9 Using Generic Types • Can use generic types with or without type argument specification • With concrete type arguments • Concrete instantiation • Without type arguments • Raw type • With wildcard arguments • Wildcard instantiation 2005 JavaOne SM Conference | Session TS-5627 | 10
Concrete Instantiation • Type argument is a concrete type voi d pr i nt Di r ect or yNam es( Col l ect i on<Fi l e> f i l es) { f or ( Fi l e f : f i l es) i f ( f . i sDi r ect or y( ) ) Syst em . out . pr i nt l n( f ) ; } • More expressive type information • Enables compile-time type checks Li st <Fi l e> t ar get Di r = new Li nkedLi st <Fi l e>( ) ; ... fill list with Fi l e objects ... pr i nt Di r ect or yNam es( t ar get Di r ) ; 2005 JavaOne SM Conference | Session TS-5627 | 11 Raw Type • No type argument specified voi d pr i nt Di r ect or yNam es( Col l ect i on f i l es) { f or ( I t er at or i t = f i l es. i t er at or ( ) ; i t . hasNext ( ) ; ) { Fi l e f = ( Fi l e) i t . next ( ) ; i f ( f . i sDi r ect or y( ) ) Syst em . out . pr i nt l n( f ) ; } } • Permitted for compatibility reasons • Permits mix of non-generic (legacy) code with generic code Li st <Fi l e> t ar get Di r = new Li nkedLi st <Fi l e>( ) ; ... fill list with Fi l e objects ... pr i nt Di r ect or yNam es( t ar get Di r ) ; 2005 JavaOne SM Conference | Session TS-5627 | 12
Wildcard Instantiation • Type argument is a wildcard voi d pr i nt El em ent s( Col l ect i on<?> c) { f or ( O bj ect e : c) Syst em . out . pr i nt l n( e) ; } • A wildcard stands for a family of types • Bounded and unbounded wildcards supported Col l ect i on <Fi l e> t ar get Di r = new Li nkedLi st <Fi l e>( ) ; ... fill list with Fi l e objects ... pr i nt El em ent s( t ar get Di r ) ; 2005 JavaOne SM Conference | Session TS-5627 | 13 Wildcards • A wildcard denotes a representative from a family of types • Unbounded wildcard ? • All types • Lower-bound wildcard ? extends supertype • All types that are subtypes of supertype • Upper-bound wildcard ? super subtype • All types that are supertypes of subtype 2005 JavaOne SM Conference | Session TS-5627 | 14
Example of a Bounded Wildcard • Consider a method • That draws objects from a class hierarchy of shapes Naïve approach voi d dr awAl l ( Li st <Shape> shapes) { f or ( Shape s : shapes) s. dr aw ( ) ; } • Method cannot draw a list of circles • Because list<circle> is not a subtype of list<shape> Li st <Ci r cl e> ci r cl es = . . . ; dr awAl l ( ci r cl es) ; Incompatible Argument Type 2005 JavaOne SM Conference | Session TS-5627 | 15 Trying to Fix It… • Try a wildcard instantiation Wildcarded version voi d dr awAl l ( Li st <?> shapes) { f or ( Shape s : shapes) s. dr aw ( ) ; Error: ? Does Not Have a Draw Method } • Compiler needs more information about “unknown” type 2005 JavaOne SM Conference | Session TS-5627 | 16
Solution: Upper Bound Wildcard • " ? Ext ends shape " stands for “any subtype of shape ” • Shape is the upper bound of the bounded wildcard • Col l ect i on<? Ext ends shape> stands for “collection of any kind of shapes” • Is the supertype of all collections that contain shapes (or subtypes thereof) voi d dr awAl l ( Li st <? ext ends Shape > shapes) { f or ( Shape s : shapes) s. dr aw ( ) ; } Li st <Ci r cl e> ci r cl es = . . . ; dr awAl l ( ci r cl es) ; fine 2005 JavaOne SM Conference | Session TS-5627 | 17 Generic Methods • Defining a generic method cl ass Ut i l i t i es { publ i c st at i c <A ext ends Com par abl e <A>> A m ax( I t er abl e<A> c) { A r esul t = nul l ; f or ( A a : c) { i f ( r esul t == nul l | | r esul t . com par eTo( a) < 0) r esul t = a; } } } 2005 JavaOne SM Conference | Session TS-5627 | 18
Type Inference • Invoking a generic method • No special invocation syntax • Type arguments are inferred from actual arguments ( ! type inference) publ i c st at i c voi d m ai n ( St r i ng[ ] ar gs) { Li nkedLi st <Byt e> byt eLi st = new Li nkedLi st <Byt e>( ) ; . . . Byt e y = Ut i l i t i es. m ax ( byt eLi st ) ; } 2005 JavaOne SM Conference | Session TS-5627 | 19 Compilation Model • Code specialization • New representation for every instantiation of a generic type or method • e.g., Different code for a list of strings and list of integers • Downside: code bloat • Code sharing • Only one representation of a generic type or method • All concrete instantiations are mapped to this representation • Implicit type checks and type conversions where needed • Downside: no primitive types 2005 JavaOne SM Conference | Session TS-5627 | 20
Type Erasure—Class Definition • Generic type cl ass Li nkedLi st <A> i m pl em ent s Col l ect i on<A> { pr ot ect ed cl ass Node { A el t ; Node next = nul l ; Node ( A el t ) { t hi s. el t = el t ; } } publ i c voi d add ( A el t ) { . . . } . . . } • After type erasure cl ass Li nkedLi st i m pl em ent s Col l ect i on { pr ot ect ed cl ass Node { Obj ect el t ; Node next = nul l ; Node ( Obj ect el t ) { t hi s. el t = el t ; } } publ i c voi d add ( Obj ect el t ) { . . . } . . . } 2005 JavaOne SM Conference | Session TS-5627 | 21 Type Erasure—Usage Context • Generic type f i nal cl ass Test { publ i c st at i c voi d m ai n ( St r i ng[ ] ar gs) { Li nkedLi st <St r i ng> ys = new Li nkedLi st <St r i ng> ( ) ; ys. add( " zer o" ) ; ys. add( " one" ) ; St r i ng y = ys. i t er at or ( ) . next ( ) ; } } • After type erasure f i nal cl ass Test { publ i c st at i c voi d m ai n ( St r i ng[ ] ar gs) { Li nkedLi st ys = new Li nkedLi st ( ) ; ys. add( " zer o" ) ; ys. add( " one" ) ; St r i ng y = ( St r i ng)ys. i t er at or ( ) . next ( ) ; } } Additional Cast 2005 JavaOne SM Conference | Session TS-5627 | 22
Recommend
More recommend