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