february 24 2016 type safety enums and generics
play

February 24, 2016 Type safety, enums and Generics generics and enum - PDF document

February 24, 2016 Type safety, enums and Generics generics and enum types HOM DOS HVD HEE generics and enum types (In)Compatible Assignment Type safety The Java Reality Pieter van den Hombergh Generics Example Generic container Thijs


  1. February 24, 2016 Type safety, enums and Generics generics and enum types HOM DOS HVD HEE generics and enum types (In)Compatible Assignment Type safety The Java Reality Pieter van den Hombergh Generics Example Generic container Thijs Dorssers Generic methods Richard van den Ham Type erasure Runtime type safety: checkedXXX Uwe van Heesch Type bounds: Upper bounds Wild card type Fontys Hogeschool voor Techniek en Logistiek CS Enum Types Use case February 24, 2016 HOMDOSHVDHEE/FHTenL generics and enum types February 24, 2016 1/38 generics and enum Topics types HOM DOS (In)Compatible Assignment HVD HEE Type safety (In)Compatible The Java Reality Assignment Type safety The Java Reality Generics Generics Example Generic container Example Generic container Generic methods Generic methods Type erasure Runtime type safety: Type erasure checkedXXX Type bounds: Upper Runtime type safety: checkedXXX bounds Wild card type Type bounds: Upper bounds CS Wild card type Enum Types Use case Type bounds: Lower Bounds, accepting things Enum Types Use case HOMDOSHVDHEE/FHTenL generics and enum types February 24, 2016 2/38 generics and enum Type safety, boon or bust? types HOM DOS HVD HEE (In)Compatible Assignment Type safety The Java Reality Generics Example Generic container Generic methods Type erasure Runtime type safety: checkedXXX Type bounds: Upper bounds Wild card type CS Enum Types Use case This can be quite frustrating, if you do not grasp the concept. HOMDOSHVDHEE/FHTenL generics and enum types February 24, 2016 3/38 generics and enum Java is type safe types HOM DOS HVD HEE . . . and the consequences are: (In)Compatible Assignment The compiler will forbid incompatible assignments. Type safety You might need to convince the compiler with a cast The Java Reality Generics that the operation is okay. Example Generic container Generic methods Then the compiler says to runtime: “Please check that Type erasure cast”. Runtime type safety: checkedXXX Runtime: “Okay, If the programmer lied, I will kick ass Type bounds: Upper bounds with a ClassCastException ”. Wild card type CS It helps the programmer to avoid errors or to at least Enum Types find them early. ClassCastException s are a typical Use case case of wrong design/implementation or wrong usage. HOMDOSHVDHEE/FHTenL generics and enum types February 24, 2016 4/38 1

  2. February 24, 2016 Type safety, enums and Generics generics and enum Java history and evolution types HOM DOS HVD The Java solution may seem strange. E.g. HEE Arrays do check against incompatible assignments. The (In)Compatible Assignment reference type (of what you want to stick in) is type Type safety The Java Reality checked against the array declaration. ( Safety! ). Generics Arrays of objects will accept any ref type, so Example Generic container Generic methods Containers ( ArrayList etc.) of object did not/could Type erasure not check against incompatible assignments in the pre- Runtime type safety: checkedXXX Java5 era. Type bounds: Upper bounds e.g. ArrayList uses, you guessed it, and array of Wild card type CS Object inside. Enum Types Since Java5 1 the compiler can (statically) check Use case against incompatible assignments. but. . . 1 introducing generics HOMDOSHVDHEE/FHTenL generics and enum types February 24, 2016 5/38 generics and enum The ideal type system types HOM DOS HVD HEE (In)Compatible Assignment Type safety The Java Reality Generics Example Generic container Generic methods Type erasure Runtime type safety: checkedXXX Type bounds: Upper bounds Wild card type CS Enum Types Use case Only real . . . allowed here. . . ... HOMDOSHVDHEE/FHTenL generics and enum types February 24, 2016 6/38 generics and enum But it would prohibit this use. . . types HOM DOS HVD HEE (In)Compatible Assignment Type safety The Java Reality Generics Example Generic container Generic methods Type erasure Runtime type safety: checkedXXX Type bounds: Upper bounds Wild card type CS Enum Types Use case . . . that has evolved with the Java culture. HOMDOSHVDHEE/FHTenL generics and enum types February 24, 2016 7/38 generics and enum Type safety summary types HOM DOS HVD HEE Type safety protects the programmer against many (In)Compatible Assignment errors, like putting round pegs in square holes. Type safety The Java Reality Even with type safety the type system and usage is Generics quite flexible. Example Generic container Generic methods Only having reference object types and no value object Type erasure Runtime type safety: types also makes the implementation efficient. You checkedXXX Type bounds: Upper need only one binary (class file) for of List, Set etc., bounds Wild card type whereas e.g. C++ needs a new separate binary for CS every member type and need to recompile 2 with that Enum Types Use case member type. 2 see C++ templates HOMDOSHVDHEE/FHTenL generics and enum types February 24, 2016 8/38 2

  3. February 24, 2016 Type safety, enums and Generics Generics HOM DOS HVD HEE Generics (In)Compatible Assignment Type safety The Java Reality Generics Pieter van den Hombergh Example Generic container Thijs Dorssers Generic methods Type erasure Richard van den Ham Runtime type safety: checkedXXX Uwe van Heesch Type bounds: Upper bounds Wild card type CS Fontys Hogeschool voor Techniek en Logistiek Enum Types Use case February 24, 2016 HOMDOSHVDHEE/FHTenL Generics February 24, 2016 9/38 Generics Generics Example - String Container HOM DOS HVD HEE Collection of Strings (In)Compatible Assignment p u b l i c c l a s s StringContainer { Type safety The Java Reality p r i v a t e value ; String Generics Example Generic container p u b l i c getValue ( ) { String Generic methods r e t u r n value ; Type erasure Runtime type safety: } checkedXXX Type bounds: Upper bounds p u b l i c void setValue ( String value ) { Wild card type t h i s . value = value ; CS } Enum Types Use case p u b l i c StringContainer ( String value ) { t h i s . value = value ; } } HOMDOSHVDHEE/FHTenL Generics February 24, 2016 10/38 Generics Generics Example - Integer Container HOM DOS HVD HEE Collection of Integers (In)Compatible Assignment p u b l i c c l a s s IntegerContainer { Type safety The Java Reality p r i v a t e value ; Integer Generics Example Generic container p u b l i c getValue ( ) { Integer Generic methods r e t u r n value ; Type erasure Runtime type safety: } checkedXXX Type bounds: Upper bounds p u b l i c void setValue ( Integer value ) { Wild card type t h i s . value = value ; CS } Enum Types Use case p u b l i c IntegerContainer ( Integer value ) { t h i s . value = value ; } } HOMDOSHVDHEE/FHTenL Generics February 24, 2016 11/38 Generics Analysis of an example HOM DOS HVD HEE (In)Compatible Assignment Type safety What is remarkable The Java Reality New class for every data type Generics Basic structure is same Example Generic container Generic methods Copy and waste Type erasure Runtime type safety: Consequences: checkedXXX Type bounds: Upper bounds Bad extensibility Wild card type Reduced maintainability CS Enum Types Bigger chance for errors Use case HOMDOSHVDHEE/FHTenL Generics February 24, 2016 12/38 3

  4. February 24, 2016 Type safety, enums and Generics Generics Generics HOM DOS HVD HEE Generic Container (In)Compatible Assignment p u b l i c c l a s s GenericContainer < C > { Type safety The Java Reality p r i v a t e C value ; Generics Example Generic container p u b l i c C getValue ( ) { Generic methods r e t u r n value ; Type erasure Runtime type safety: } checkedXXX Type bounds: Upper bounds p u b l i c void setValue ( C value ) { Wild card type t h i s . value = value ; CS } Enum Types Use case p u b l i c GenericContainer ( C value ) { t h i s . value = value ; } } HOMDOSHVDHEE/FHTenL Generics February 24, 2016 13/38 Generics Generics Container HOM DOS HVD HEE (In)Compatible Assignment Type safety The Java Reality In stead of a concrete type we have a formal Type Generics parameter (C in the example) Example Generic container Generic methods Type erasure The instantiation takes place based on the Runtime type safety: checkedXXX parameterized type Type bounds: Upper bounds All generic operations can now have a type. Wild card type CS Enum Types Use case HOMDOSHVDHEE/FHTenL Generics February 24, 2016 14/38 Generics Generics in Methods HOM DOS HVD HEE (In)Compatible Assignment A class can also have generic (static) methods. Type safety The Java Reality The Type parameter is applied on the current method Generics Example Generic container definition only. Generic methods Type erasure Runtime type safety: checkedXXX Typed (static) method Type bounds: Upper bounds Wild card type p u b l i c s t a t i c < T > T random ( T m , T n ) { CS r e t u r n Math . random ( ) > 0.5 ? m : n ; Enum Types } Use case HOMDOSHVDHEE/FHTenL Generics February 24, 2016 15/38 Generics Implementation of Generic classes, type erasure HOM DOS HVD HEE (In)Compatible Assignment Type safety The Java Reality Generics Instead of the generic type, Object is used. Example Generic container Generic methods Type erasure Everywhere a type access is used, a typecast is applied Runtime type safety: checkedXXX for you Type bounds: Upper bounds Wild card type CS Enum Types Use case HOMDOSHVDHEE/FHTenL Generics February 24, 2016 16/38 4

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend