SlideShare a Scribd company logo
1 of 140
Download to read offline
January 21, 2009                                               Generic Classes in Java




                          Department of Computer Science




                                            Java Generics
                                                E. Steegmans
                                                 K.U.Leuven




                         Overview

                                      concepts
                          Basic


                                     erasure
                          Type


                              Wild cards
                          



                              Inheritance
                          



                              Generic methods
                          




Eric Steegmans - K.U.Leuven                                                         1
January 21, 2009                                                                        Generic Classes in Java




                         Non-Generic Classes
                              No formal arguments involved in definitions of
                          

                              classes and interfaces
                                   Object is often used as the most general type for
                               


                                   variables, method arguments and return types
                                    -  Typical example: Container classes in Java API
                                   Common practice before Java 1.5
                               




                         Non-Generic Classes: Usage

                          List employees = new ArrayList();

                          Person albert = …;
                          Parrot filip = …;
                          employees.add(albert);
                          employees.add(filip);
                                   // No compile-time checks nor runtime checks.


                          Person laurent = (Person) employees.get(1);
                                   // Virtual machine throws ClassCastException.




Eric Steegmans - K.U.Leuven                                                                                  2
January 21, 2009                                                                   Generic Classes in Java




                         Generics
                              Parameterized definitions of classes and
                          

                              interfaces
                                   Only reference types as formal arguments
                               


                                    -  No other types such as integers, …
                                   Convention: single letter arguments such as E
                               

                                   (element), T (type), K (key), …




                         Generic Classes
                          public class ArrayList
                              extends AbstractList
                              implements List, Cloneable, Serializable
                          {
                            public Arraylist();
                            public boolean add(Object elem);
                            public Object get(int index);
                            public boolean remove(Object o);
                            public boolean contains(Object o);
                            public Object[] toArray();
                            public Iterator iterator();
                            public List subList(int from, int to);
                            public boolean addAll(Collection c);
                            public boolean containsAll(Collection c);
                            public Class getClass();
                            public Object[] toArray(Object[] a);
                            …
                          }




Eric Steegmans - K.U.Leuven                                                                             3
January 21, 2009                                                     Generic Classes in Java




                         Generic Classes
                          public class ArrayList<E>


                          {
                              public Arraylist();
                              public boolean add( E     elem);
                              public   E    get(int index);




                              …
                          }




                         Generic Classes
                          public class ArrayList<E>


                          {
                              public   Arraylist();
                              public   boolean add(E elem);
                              public   E get(int index);
                              public   boolean remove(Object o);
                              public   boolean contains(Object o);
                              public   Object[] toArray();




                              …
                          }




Eric Steegmans - K.U.Leuven                                                               4
January 21, 2009                                                                Generic Classes in Java




                         Generic Classes
                          public class ArrayList<E>
                              extends AbstractList<E>
                              implements List<E>, Cloneable, Serializable
                          {
                            public Arraylist();
                            public boolean add(E elem);
                            public E get(int index);
                            public boolean remove(Object o);
                            public boolean contains(Object o);
                            public Object[] toArray();
                            public Iterator<E> iterator();
                            public List<E> subList(int from, int to);




                              …
                          }




                         Generic Classes
                          public class ArrayList<E>
                              extends AbstractList<E>
                              implements List<E>, Cloneable, Serializable
                          {
                            public Arraylist();
                            public boolean add(E elem);
                            public E get(int index);
                            public boolean remove(Object o);
                            public boolean contains(Object o);
                            public Object[] toArray();
                            public Iterator<E> iterator();
                            public List<E> subList(int from, int to);
                            public boolean addAll(Collection<? extends E> c);
                            public boolean containsAll(Collection<?> c);
                            public Class<? extends Object> getClass();

                              …
                          }




Eric Steegmans - K.U.Leuven                                                                          5
January 21, 2009                                                                Generic Classes in Java




                         Generic Classes
                          public class ArrayList<E>
                              extends AbstractList<E>
                              implements List<E>, Cloneable, Serializable
                          {
                            public Arraylist();
                            public boolean add(E elem);
                            public E get(int index);
                            public boolean remove(Object o);
                            public boolean contains(Object o);
                            public Object[] toArray();
                            public Iterator<E> iterator();
                            public List<E> subList(int from, int to);
                            public boolean addAll(Collection<? extends E> c);
                            public boolean containsAll(Collection<?> c);
                            public Class<? extends Object> getClass();
                            public <T> T[] toArray(T[] a);
                            …
                          }




                         Generics: Usage

                          List<Person> employees =
                              new ArrayList<Person>();

                          Person albert = …;
                          Parrot filip = …;
                          employees.add(albert);
                          employees.add(filip);
                              // Compile-time checks.


                          Person laurent = employees.get(1);
                              // No type cast needed.




Eric Steegmans - K.U.Leuven                                                                          6
January 21, 2009                                           Generic Classes in Java




                         Generics: Advantages
                              Correctness 
                          

                                   Compile-time checks 
                               



                              Complexity 
                          

                                   Type casts 
                               



                              Readability 
                          

                                   Static information 
                               




                         Overview

                                          concepts
                          Basic



                                          erasure
                          Type


                                        cards
                          Wild


                              Inheritance
                          



                              Generic methods
                          




Eric Steegmans - K.U.Leuven                                                     7
January 21, 2009                                                                                            Generic Classes in Java




                         Type Erasure
                              The Java compiler removes all information related
                          

                              to generic types
                                   The Java Virtual Machine only knows of “raw types”
                               


                                    -  All instances of all instantiations of a generic class (interface)
                                       belong to the same class (interface)
                              Advantage: binary compatibility
                          

                                   The introduction of generics does not require any
                               

                                   changes to the Java Virtual Machine
                                    -  All Java applications can still run on the same virtual machine




                         Type Erasure: Consequences
                          public class GenericClass<T> {

                              private T[] elems = new T[12];
                              // At the time an array is created, the
                              // type of its elements must be supplied.




                          }




Eric Steegmans - K.U.Leuven                                                                                                      8
January 21, 2009                                                             Generic Classes in Java




                         Type Erasure: Consequences
                          public class GenericClass<T> {

                              private T[] elems = new T[12];

                              public void someMethod(Object elem) {

                                  if (elem instanceof T);
                                  // No runtime type information (RTTI)
                                  // available concerning generic args.



                              }

                          }




                         Type Erasure: Consequences
                          public class GenericClass<T> {

                              private T[] elems = new T[12];

                              public void someMethod(Object elem) {

                                  if (elem instanceof T);

                                  new T();
                                  // No information concerning constructor
                                  // of generic argument.

                              }

                          }




Eric Steegmans - K.U.Leuven                                                                       9
January 21, 2009                                                                                  Generic Classes in Java




                         Type Erasure: Consequences
                          public class GenericClass<T> {

                              private T[] elems = new T[12];

                              public void someMethod(Object elem) {

                                   if (elem instanceof T);

                                   new T();

                                   T theElement = (T) elem;
                                   // Unchecked cast from Object to T.
                              }

                          }




                         Type Erasure: Conclusion
                              Java 1.4: Run-time type safety
                          

                                   Java programs may include attempts to assign values of
                               

                                   improper type
                                    -  Most attempts are already caught at compile-time
                                   During the execution of a program, a variable can never
                               

                                   store a value of the wrong type
                                    -  The virtual machine checks the correctness of type casts




Eric Steegmans - K.U.Leuven                                                                                           10
January 21, 2009                                                                                               Generic Classes in Java




                         Type Erasure: Conclusion
                              Java 1.5: No runtime type safety for generic types
                          

                                   Without proper care
                               


                                    -  Instantiations of generic classes may store elements of the
                                       wrong type
                                        -  It is not impossible for a list of persons to store other objects such
                                           as bank accounts, books and trees
                                    -  Instantiations of generic classes may return objects of a type
                                       that differs from the return type
                                   With proper care
                               


                                    -  A generic class can make it impossible for clients to store
                                       elements of the wrong type
                                    -  Methods of a generic class always return objects of the type
                                       they promise




                         Overview

                              Basic concepts
                          



                                          erasure
                          Type



                                          cards
                          Wild


                          Inheritance


                              Generic methods
                          




Eric Steegmans - K.U.Leuven                                                                                                        11
January 21, 2009                                                                                  Generic Classes in Java




                         Polymorphism in Java 1.4
                              If S is a subtype of T, an object of type S can be
                          

                              assigned to a variable of type T
                                   Objects of type Woman and objects of type Man can be
                               


                                   assigned to variables of type Person
                              Can we assign an array of type S[] to a variable
                          
                              of type T[]




                         Polymorphism in Java 1.5
                              Can we assign an object of type G<S> to a
                          

                              variable of type G<T>?
                                   Assuming G is a generic class or interface, and S is a
                               


                                   true subtype of T
                                    -  Can we assign an object of type List<Woman> to a variable of
                                       type List<Person>?




Eric Steegmans - K.U.Leuven                                                                                           12
January 21, 2009                                                                                        Generic Classes in Java




                         Unbounded Wild Cards
                              A variable of type G<?> is a supertype for any
                          

                              instantiation of the generic class or interface G
                                   A variable of type List<?> can be assigned objects of
                               


                                   type List<Person>, List<Integer>, ...
                              The object referenced by a variable of type G<?>
                          
                              is more or less read-only
                                   Methods of G<E> with arguments of type E cannot be
                               


                                   invoked against variables of type G<?>
                                    -  Invocations of methods without arguments of type E may still
                                       cause state changes
                                   Variables of type G<?> act as if the class or interface is
                               

                                   instantiated with Object
                                    -  Invocations of methods declared to return an object of type E,
                                       return an object of type Object




                         Bounded Wild Cards
                              A variable of type G<? extends X> is a super-
                          

                              type for instantiations with type X or subtype of X
                                   A variable of type List<? extends Person> can be
                               


                                   assigned objects of type List<Man>, List<Woman>, ..
                                   This type of wild card specifies an upper bound for
                               

                                   actual types at the time of instantiation
                              The object referenced by a variable of type
                          
                              G<? extends X> is to some extent read-only
                                   Methods of G<E> with arguments of type E cannot be
                               

                                   invoked against variables of type G<? extends X>
                                   Variables of type G<? extends X> act as if the class
                               


                                   or interface is instantiated with X




Eric Steegmans - K.U.Leuven                                                                                                 13
January 21, 2009                                                                                  Generic Classes in Java




                         Bounded Wild Cards
                              A variable of type G<? super X> is a supertype
                          

                              for any instantiation with type X or supertype of X
                                   A variable of type List<? super Man> can be
                               


                                   assigned objects of type List<Man>, List<Person>
                                   This type of wild card specifies a lower bound for actual
                               

                                   types at the time of instantiation
                              The object referenced by a variable of type
                          
                              G<? super X> is to some extent write-only
                                   Methods of G<E> with actual argument of type E can
                               

                                   only be invoked with actual arguments of type X
                                   Invocations of methods declared to return an object of
                               


                                   type E, return an object of type Object




                         Raw Types
                              A variable of type G is a supertype for any
                          

                              instantiation of the generic class or interface G
                                   A variable of type List can be assigned objects of type
                               


                                   List<Account>, List<Person>
                              Be careful in approaching generic instantiations by
                          
                              means of raw types!
                                   Methods of G<E> with actual argument of type E can be
                               


                                   invoked against variables of type G
                                    -  The Java compiler only issues warnings for possible type
                                       mismatches
                                   Invocations of methods declared to return an object of
                               

                                   type E, return an object of type Object




Eric Steegmans - K.U.Leuven                                                                                           14
January 21, 2009                                                                          Generic Classes in Java




                         Generic Arguments: Bounds
                              A generic class or interface G<T extends C>
                          

                              can only be instantiated with C or a subtype of C
                                   All methods of class or interface C can be invoked
                               


                                   against variables of type T
                              Multiple bounds on generic arguments are
                          
                              possible as in G<T extends X & Y>




                         Wild Cards: Conclusion
                              Wild cards are needed in polymorphic
                          

                              assignments only because of type erasure
                                   Other languages such as C++, C# and Eiffel do not
                               


                                   need wild cards in variable declarations
                                   Approaching generic instantiations with raw types is
                               

                                   asking for (big) problems
                              Wild cards are useful in specifying restrictions on
                          
                              generic arguments




Eric Steegmans - K.U.Leuven                                                                                   15
January 21, 2009                                                                                    Generic Classes in Java




                         Overview
                              Basic concepts
                          




                              Type erasure
                          



                                         cards
                          Wild



                          Inheritance


                                                methods
                          Generic




                         Generic Subclasses
                              A generic class may extend a single class and
                          

                              may implement several interfaces
                                   Superclasses and superinterfaces may be instantiations
                               


                                   of generic classes, resp. generic interfaces
                                    -  Typically, formal arguments of the generic subclass are used to
                                       instantiate generic superclasses and superinterfaces
                              A generic interface may extend several interfaces
                          




Eric Steegmans - K.U.Leuven                                                                                             16
January 21, 2009                                                                           Generic Classes in Java




                         Polymorphism
                              If S<E> inherits from T<E>, can we assign an
                          

                              object of type S<C> to a variable of type T<C>?
                                   Can an object of type SortedSet<Integer> be
                               


                                   assigned to a variable of type Set<Integer>?




                         Frameworks
                              Generics (can) considerably improve the quality of
                          

                              frameworks
                                   Unfortunately they may also considerably increase the
                               


                                   complexity of frameworks
                                   Wild cards are not powerful enough to express all
                               

                                   restrictions




Eric Steegmans - K.U.Leuven                                                                                    17
January 21, 2009                                         Generic Classes in Java




                         The Composite Pattern




                         Overview
                              Basic concepts
                          




                              Type erasure
                          




                              Wild cards
                          



                          Inheritance



                                               methods
                          Generic




Eric Steegmans - K.U.Leuven                                                  18
January 21, 2009                                                                                  Generic Classes in Java




                         Generic Methods: Definition
                              A generic method is parameterized in one or more
                          

                              types
                                   As for generic classes, generic methods can only be
                               


                                   parameterized in reference types
                                    -  The generic parameters precede the return type of the method
                              Generic arguments can express dependencies
                          

                              among types of ordinary formal arguments
                                   Bounded wildcards in both directions are used for that
                               


                                   purpose
                              The actual types of generic methods are derived
                          
                              from their actual arguments
                                   The compiler rejects invocations if no matching type is
                               

                                   available




Eric Steegmans - K.U.Leuven                                                                                           19
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics
BeJug.Org   Java Generics

More Related Content

What's hot

Statics in java | Constructors | Exceptions in Java | String in java| class 3
Statics in java | Constructors | Exceptions in Java | String in java| class 3Statics in java | Constructors | Exceptions in Java | String in java| class 3
Statics in java | Constructors | Exceptions in Java | String in java| class 3Sagar Verma
 
First fare 2011 frc-java-introduction
First fare 2011 frc-java-introductionFirst fare 2011 frc-java-introduction
First fare 2011 frc-java-introductionOregon FIRST Robotics
 
Xm Lquickref
Xm LquickrefXm Lquickref
Xm LquickrefLiquidHub
 
Xml Syntax Quick Reference
Xml Syntax Quick ReferenceXml Syntax Quick Reference
Xml Syntax Quick ReferenceLiquidHub
 
Querying UML Class Diagrams - FoSSaCS 2012
Querying UML Class Diagrams - FoSSaCS 2012Querying UML Class Diagrams - FoSSaCS 2012
Querying UML Class Diagrams - FoSSaCS 2012Giorgio Orsi
 
Java Beans Unit 4(Part 1)
Java Beans Unit 4(Part 1)Java Beans Unit 4(Part 1)
Java Beans Unit 4(Part 1)SURBHI SAROHA
 
Java: Objects and Object References
Java: Objects and Object ReferencesJava: Objects and Object References
Java: Objects and Object ReferencesTareq Hasan
 
Introduction to class in java
Introduction to class in javaIntroduction to class in java
Introduction to class in javakamal kotecha
 
The Ruby Object Model by Rafael Magana
The Ruby Object Model by Rafael MaganaThe Ruby Object Model by Rafael Magana
The Ruby Object Model by Rafael MaganaRafael Magana
 
Sdtl manual
Sdtl manualSdtl manual
Sdtl manualqaz8989
 

What's hot (20)

core java
core javacore java
core java
 
10slide
10slide10slide
10slide
 
Ruby object model
Ruby object modelRuby object model
Ruby object model
 
Cascon2011_5_rules+owl
Cascon2011_5_rules+owlCascon2011_5_rules+owl
Cascon2011_5_rules+owl
 
Statics in java | Constructors | Exceptions in Java | String in java| class 3
Statics in java | Constructors | Exceptions in Java | String in java| class 3Statics in java | Constructors | Exceptions in Java | String in java| class 3
Statics in java | Constructors | Exceptions in Java | String in java| class 3
 
First fare 2011 frc-java-introduction
First fare 2011 frc-java-introductionFirst fare 2011 frc-java-introduction
First fare 2011 frc-java-introduction
 
Xm Lquickref
Xm LquickrefXm Lquickref
Xm Lquickref
 
Xml Syntax Quick Reference
Xml Syntax Quick ReferenceXml Syntax Quick Reference
Xml Syntax Quick Reference
 
11slide
11slide11slide
11slide
 
Querying UML Class Diagrams - FoSSaCS 2012
Querying UML Class Diagrams - FoSSaCS 2012Querying UML Class Diagrams - FoSSaCS 2012
Querying UML Class Diagrams - FoSSaCS 2012
 
Java beans
Java beansJava beans
Java beans
 
Class introduction in java
Class introduction in javaClass introduction in java
Class introduction in java
 
Core java complete notes - Contact at +91-814-614-5674
Core java complete notes - Contact at +91-814-614-5674Core java complete notes - Contact at +91-814-614-5674
Core java complete notes - Contact at +91-814-614-5674
 
Java Beans Unit 4(Part 1)
Java Beans Unit 4(Part 1)Java Beans Unit 4(Part 1)
Java Beans Unit 4(Part 1)
 
Java: Objects and Object References
Java: Objects and Object ReferencesJava: Objects and Object References
Java: Objects and Object References
 
Computer Programming 2
Computer Programming 2 Computer Programming 2
Computer Programming 2
 
Introduction to class in java
Introduction to class in javaIntroduction to class in java
Introduction to class in java
 
The Ruby Object Model by Rafael Magana
The Ruby Object Model by Rafael MaganaThe Ruby Object Model by Rafael Magana
The Ruby Object Model by Rafael Magana
 
java training faridabad
java training faridabadjava training faridabad
java training faridabad
 
Sdtl manual
Sdtl manualSdtl manual
Sdtl manual
 

Similar to BeJug.Org Java Generics

oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaoops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaCPD INDIA
 
Java Serialization
Java SerializationJava Serialization
Java Serializationjeslie
 
Corejava Training in Bangalore Tutorial
Corejava Training in Bangalore TutorialCorejava Training in Bangalore Tutorial
Corejava Training in Bangalore Tutorialrajkamaltibacademy
 
Java Course 5: Enums, Generics, Assertions
Java Course 5: Enums, Generics, AssertionsJava Course 5: Enums, Generics, Assertions
Java Course 5: Enums, Generics, AssertionsAnton Keks
 
Week9 Intro to classes and objects in Java
Week9 Intro to classes and objects in JavaWeek9 Intro to classes and objects in Java
Week9 Intro to classes and objects in Javakjkleindorfer
 
Java class 3
Java class 3Java class 3
Java class 3Edureka!
 
Java Generics
Java GenericsJava Generics
Java Genericsjeslie
 

Similar to BeJug.Org Java Generics (8)

Java OO Revisited
Java OO RevisitedJava OO Revisited
Java OO Revisited
 
oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaoops concept in java | object oriented programming in java
oops concept in java | object oriented programming in java
 
Java Serialization
Java SerializationJava Serialization
Java Serialization
 
Corejava Training in Bangalore Tutorial
Corejava Training in Bangalore TutorialCorejava Training in Bangalore Tutorial
Corejava Training in Bangalore Tutorial
 
Java Course 5: Enums, Generics, Assertions
Java Course 5: Enums, Generics, AssertionsJava Course 5: Enums, Generics, Assertions
Java Course 5: Enums, Generics, Assertions
 
Week9 Intro to classes and objects in Java
Week9 Intro to classes and objects in JavaWeek9 Intro to classes and objects in Java
Week9 Intro to classes and objects in Java
 
Java class 3
Java class 3Java class 3
Java class 3
 
Java Generics
Java GenericsJava Generics
Java Generics
 

More from Stephan Janssen

Devoxx speaker training (June 27th, 2018)
Devoxx speaker training (June 27th, 2018)Devoxx speaker training (June 27th, 2018)
Devoxx speaker training (June 27th, 2018)Stephan Janssen
 
The new DeVoxxEd websites with JHipster, Angular & Kubernetes
The new DeVoxxEd websites with JHipster, Angular & KubernetesThe new DeVoxxEd websites with JHipster, Angular & Kubernetes
The new DeVoxxEd websites with JHipster, Angular & KubernetesStephan Janssen
 
The new Voxxed websites with JHipster, Angular and GitLab
The new Voxxed websites  with JHipster, Angular and GitLabThe new Voxxed websites  with JHipster, Angular and GitLab
The new Voxxed websites with JHipster, Angular and GitLabStephan Janssen
 
Devoxx2011 timesheet day3-4-5
Devoxx2011 timesheet day3-4-5Devoxx2011 timesheet day3-4-5
Devoxx2011 timesheet day3-4-5Stephan Janssen
 
Devoxx2011 timesheet day1-2
Devoxx2011 timesheet day1-2Devoxx2011 timesheet day1-2
Devoxx2011 timesheet day1-2Stephan Janssen
 

More from Stephan Janssen (17)

Devoxx speaker training (June 27th, 2018)
Devoxx speaker training (June 27th, 2018)Devoxx speaker training (June 27th, 2018)
Devoxx speaker training (June 27th, 2018)
 
The new DeVoxxEd websites with JHipster, Angular & Kubernetes
The new DeVoxxEd websites with JHipster, Angular & KubernetesThe new DeVoxxEd websites with JHipster, Angular & Kubernetes
The new DeVoxxEd websites with JHipster, Angular & Kubernetes
 
The new Voxxed websites with JHipster, Angular and GitLab
The new Voxxed websites  with JHipster, Angular and GitLabThe new Voxxed websites  with JHipster, Angular and GitLab
The new Voxxed websites with JHipster, Angular and GitLab
 
Java, what's next?
Java, what's next?Java, what's next?
Java, what's next?
 
Programming 4 kids
Programming 4 kidsProgramming 4 kids
Programming 4 kids
 
Devoxx2011 timesheet day3-4-5
Devoxx2011 timesheet day3-4-5Devoxx2011 timesheet day3-4-5
Devoxx2011 timesheet day3-4-5
 
Devoxx2011 timesheet day1-2
Devoxx2011 timesheet day1-2Devoxx2011 timesheet day1-2
Devoxx2011 timesheet day1-2
 
EJB 3.1 by Bert Ertman
EJB 3.1 by Bert ErtmanEJB 3.1 by Bert Ertman
EJB 3.1 by Bert Ertman
 
BeJUG JAX-RS Event
BeJUG JAX-RS EventBeJUG JAX-RS Event
BeJUG JAX-RS Event
 
Whats New In Java Ee 6
Whats New In Java Ee 6Whats New In Java Ee 6
Whats New In Java Ee 6
 
Glass Fishv3 March2010
Glass Fishv3 March2010Glass Fishv3 March2010
Glass Fishv3 March2010
 
Advanced Scrum
Advanced ScrumAdvanced Scrum
Advanced Scrum
 
Scala by Luc Duponcheel
Scala by Luc DuponcheelScala by Luc Duponcheel
Scala by Luc Duponcheel
 
Intro To OSGi
Intro To OSGiIntro To OSGi
Intro To OSGi
 
Kick Start Jpa
Kick Start JpaKick Start Jpa
Kick Start Jpa
 
BeJUG - Di With Spring
BeJUG - Di With SpringBeJUG - Di With Spring
BeJUG - Di With Spring
 
BeJUG - Spring 3 talk
BeJUG - Spring 3 talkBeJUG - Spring 3 talk
BeJUG - Spring 3 talk
 

Recently uploaded

Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfhans926745
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdfChristopherTHyatt
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 

Recently uploaded (20)

Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 

BeJug.Org Java Generics

  • 1. January 21, 2009 Generic Classes in Java Department of Computer Science Java Generics E. Steegmans K.U.Leuven Overview concepts  Basic erasure  Type Wild cards   Inheritance   Generic methods   Eric Steegmans - K.U.Leuven 1
  • 2. January 21, 2009 Generic Classes in Java Non-Generic Classes No formal arguments involved in definitions of   classes and interfaces Object is often used as the most general type for   variables, method arguments and return types -  Typical example: Container classes in Java API Common practice before Java 1.5   Non-Generic Classes: Usage List employees = new ArrayList(); Person albert = …; Parrot filip = …; employees.add(albert); employees.add(filip); // No compile-time checks nor runtime checks. Person laurent = (Person) employees.get(1); // Virtual machine throws ClassCastException. Eric Steegmans - K.U.Leuven 2
  • 3. January 21, 2009 Generic Classes in Java Generics Parameterized definitions of classes and   interfaces Only reference types as formal arguments   -  No other types such as integers, … Convention: single letter arguments such as E   (element), T (type), K (key), … Generic Classes public class ArrayList extends AbstractList implements List, Cloneable, Serializable { public Arraylist(); public boolean add(Object elem); public Object get(int index); public boolean remove(Object o); public boolean contains(Object o); public Object[] toArray(); public Iterator iterator(); public List subList(int from, int to); public boolean addAll(Collection c); public boolean containsAll(Collection c); public Class getClass(); public Object[] toArray(Object[] a); … } Eric Steegmans - K.U.Leuven 3
  • 4. January 21, 2009 Generic Classes in Java Generic Classes public class ArrayList<E> { public Arraylist(); public boolean add( E elem); public E get(int index); … } Generic Classes public class ArrayList<E> { public Arraylist(); public boolean add(E elem); public E get(int index); public boolean remove(Object o); public boolean contains(Object o); public Object[] toArray(); … } Eric Steegmans - K.U.Leuven 4
  • 5. January 21, 2009 Generic Classes in Java Generic Classes public class ArrayList<E> extends AbstractList<E> implements List<E>, Cloneable, Serializable { public Arraylist(); public boolean add(E elem); public E get(int index); public boolean remove(Object o); public boolean contains(Object o); public Object[] toArray(); public Iterator<E> iterator(); public List<E> subList(int from, int to); … } Generic Classes public class ArrayList<E> extends AbstractList<E> implements List<E>, Cloneable, Serializable { public Arraylist(); public boolean add(E elem); public E get(int index); public boolean remove(Object o); public boolean contains(Object o); public Object[] toArray(); public Iterator<E> iterator(); public List<E> subList(int from, int to); public boolean addAll(Collection<? extends E> c); public boolean containsAll(Collection<?> c); public Class<? extends Object> getClass(); … } Eric Steegmans - K.U.Leuven 5
  • 6. January 21, 2009 Generic Classes in Java Generic Classes public class ArrayList<E> extends AbstractList<E> implements List<E>, Cloneable, Serializable { public Arraylist(); public boolean add(E elem); public E get(int index); public boolean remove(Object o); public boolean contains(Object o); public Object[] toArray(); public Iterator<E> iterator(); public List<E> subList(int from, int to); public boolean addAll(Collection<? extends E> c); public boolean containsAll(Collection<?> c); public Class<? extends Object> getClass(); public <T> T[] toArray(T[] a); … } Generics: Usage List<Person> employees = new ArrayList<Person>(); Person albert = …; Parrot filip = …; employees.add(albert); employees.add(filip); // Compile-time checks. Person laurent = employees.get(1); // No type cast needed. Eric Steegmans - K.U.Leuven 6
  • 7. January 21, 2009 Generic Classes in Java Generics: Advantages Correctness    Compile-time checks    Complexity    Type casts    Readability    Static information    Overview concepts  Basic erasure  Type cards  Wild Inheritance   Generic methods   Eric Steegmans - K.U.Leuven 7
  • 8. January 21, 2009 Generic Classes in Java Type Erasure The Java compiler removes all information related   to generic types The Java Virtual Machine only knows of “raw types”   -  All instances of all instantiations of a generic class (interface) belong to the same class (interface) Advantage: binary compatibility   The introduction of generics does not require any   changes to the Java Virtual Machine -  All Java applications can still run on the same virtual machine Type Erasure: Consequences public class GenericClass<T> { private T[] elems = new T[12]; // At the time an array is created, the // type of its elements must be supplied. } Eric Steegmans - K.U.Leuven 8
  • 9. January 21, 2009 Generic Classes in Java Type Erasure: Consequences public class GenericClass<T> { private T[] elems = new T[12]; public void someMethod(Object elem) { if (elem instanceof T); // No runtime type information (RTTI) // available concerning generic args. } } Type Erasure: Consequences public class GenericClass<T> { private T[] elems = new T[12]; public void someMethod(Object elem) { if (elem instanceof T); new T(); // No information concerning constructor // of generic argument. } } Eric Steegmans - K.U.Leuven 9
  • 10. January 21, 2009 Generic Classes in Java Type Erasure: Consequences public class GenericClass<T> { private T[] elems = new T[12]; public void someMethod(Object elem) { if (elem instanceof T); new T(); T theElement = (T) elem; // Unchecked cast from Object to T. } } Type Erasure: Conclusion Java 1.4: Run-time type safety   Java programs may include attempts to assign values of   improper type -  Most attempts are already caught at compile-time During the execution of a program, a variable can never   store a value of the wrong type -  The virtual machine checks the correctness of type casts Eric Steegmans - K.U.Leuven 10
  • 11. January 21, 2009 Generic Classes in Java Type Erasure: Conclusion Java 1.5: No runtime type safety for generic types   Without proper care   -  Instantiations of generic classes may store elements of the wrong type -  It is not impossible for a list of persons to store other objects such as bank accounts, books and trees -  Instantiations of generic classes may return objects of a type that differs from the return type With proper care   -  A generic class can make it impossible for clients to store elements of the wrong type -  Methods of a generic class always return objects of the type they promise Overview Basic concepts   erasure  Type cards  Wild  Inheritance Generic methods   Eric Steegmans - K.U.Leuven 11
  • 12. January 21, 2009 Generic Classes in Java Polymorphism in Java 1.4 If S is a subtype of T, an object of type S can be   assigned to a variable of type T Objects of type Woman and objects of type Man can be   assigned to variables of type Person Can we assign an array of type S[] to a variable   of type T[] Polymorphism in Java 1.5 Can we assign an object of type G<S> to a   variable of type G<T>? Assuming G is a generic class or interface, and S is a   true subtype of T -  Can we assign an object of type List<Woman> to a variable of type List<Person>? Eric Steegmans - K.U.Leuven 12
  • 13. January 21, 2009 Generic Classes in Java Unbounded Wild Cards A variable of type G<?> is a supertype for any   instantiation of the generic class or interface G A variable of type List<?> can be assigned objects of   type List<Person>, List<Integer>, ... The object referenced by a variable of type G<?>   is more or less read-only Methods of G<E> with arguments of type E cannot be   invoked against variables of type G<?> -  Invocations of methods without arguments of type E may still cause state changes Variables of type G<?> act as if the class or interface is   instantiated with Object -  Invocations of methods declared to return an object of type E, return an object of type Object Bounded Wild Cards A variable of type G<? extends X> is a super-   type for instantiations with type X or subtype of X A variable of type List<? extends Person> can be   assigned objects of type List<Man>, List<Woman>, .. This type of wild card specifies an upper bound for   actual types at the time of instantiation The object referenced by a variable of type   G<? extends X> is to some extent read-only Methods of G<E> with arguments of type E cannot be   invoked against variables of type G<? extends X> Variables of type G<? extends X> act as if the class   or interface is instantiated with X Eric Steegmans - K.U.Leuven 13
  • 14. January 21, 2009 Generic Classes in Java Bounded Wild Cards A variable of type G<? super X> is a supertype   for any instantiation with type X or supertype of X A variable of type List<? super Man> can be   assigned objects of type List<Man>, List<Person> This type of wild card specifies a lower bound for actual   types at the time of instantiation The object referenced by a variable of type   G<? super X> is to some extent write-only Methods of G<E> with actual argument of type E can   only be invoked with actual arguments of type X Invocations of methods declared to return an object of   type E, return an object of type Object Raw Types A variable of type G is a supertype for any   instantiation of the generic class or interface G A variable of type List can be assigned objects of type   List<Account>, List<Person> Be careful in approaching generic instantiations by   means of raw types! Methods of G<E> with actual argument of type E can be   invoked against variables of type G -  The Java compiler only issues warnings for possible type mismatches Invocations of methods declared to return an object of   type E, return an object of type Object Eric Steegmans - K.U.Leuven 14
  • 15. January 21, 2009 Generic Classes in Java Generic Arguments: Bounds A generic class or interface G<T extends C>   can only be instantiated with C or a subtype of C All methods of class or interface C can be invoked   against variables of type T Multiple bounds on generic arguments are   possible as in G<T extends X & Y> Wild Cards: Conclusion Wild cards are needed in polymorphic   assignments only because of type erasure Other languages such as C++, C# and Eiffel do not   need wild cards in variable declarations Approaching generic instantiations with raw types is   asking for (big) problems Wild cards are useful in specifying restrictions on   generic arguments Eric Steegmans - K.U.Leuven 15
  • 16. January 21, 2009 Generic Classes in Java Overview Basic concepts   Type erasure   cards  Wild  Inheritance methods  Generic Generic Subclasses A generic class may extend a single class and   may implement several interfaces Superclasses and superinterfaces may be instantiations   of generic classes, resp. generic interfaces -  Typically, formal arguments of the generic subclass are used to instantiate generic superclasses and superinterfaces A generic interface may extend several interfaces   Eric Steegmans - K.U.Leuven 16
  • 17. January 21, 2009 Generic Classes in Java Polymorphism If S<E> inherits from T<E>, can we assign an   object of type S<C> to a variable of type T<C>? Can an object of type SortedSet<Integer> be   assigned to a variable of type Set<Integer>? Frameworks Generics (can) considerably improve the quality of   frameworks Unfortunately they may also considerably increase the   complexity of frameworks Wild cards are not powerful enough to express all   restrictions Eric Steegmans - K.U.Leuven 17
  • 18. January 21, 2009 Generic Classes in Java The Composite Pattern Overview Basic concepts   Type erasure   Wild cards    Inheritance methods  Generic Eric Steegmans - K.U.Leuven 18
  • 19. January 21, 2009 Generic Classes in Java Generic Methods: Definition A generic method is parameterized in one or more   types As for generic classes, generic methods can only be   parameterized in reference types -  The generic parameters precede the return type of the method Generic arguments can express dependencies   among types of ordinary formal arguments Bounded wildcards in both directions are used for that   purpose The actual types of generic methods are derived   from their actual arguments The compiler rejects invocations if no matching type is   available Eric Steegmans - K.U.Leuven 19