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

february 24 2016 type safety enums and generics
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

generics and enum 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

generics 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/38

generics and enum 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

Topics

(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/38

generics and enum 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

Type 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/38

generics and enum 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

Java 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/38

February 24, 2016 Type safety, enums and Generics 1

slide-2
SLIDE 2

generics and enum 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

Java history and evolution

The Java solution may seem strange. E.g. Arrays do check against incompatible assignments. The reference type (of what you want to stick in) is type checked against the array declaration. (Safety!). Arrays of objects will accept any ref type, so

Containers (ArrayList etc.) of object did not/could not check against incompatible assignments in the pre- Java5 era. e.g. ArrayList uses, you guessed it, and array of Object inside. Since Java51 the compiler can (statically) check against incompatible assignments.

  • but. . .

1introducing generics

HOMDOSHVDHEE/FHTenL generics and enum types February 24, 2016 5/38

generics and enum 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

The ideal type system

Only real . . . allowed here. . . ...

HOMDOSHVDHEE/FHTenL generics and enum types February 24, 2016 6/38

generics and enum 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

But it would prohibit this use. . .

. . . that has evolved with the Java culture.

HOMDOSHVDHEE/FHTenL generics and enum types February 24, 2016 7/38

generics and enum 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

Type safety summary

Type safety protects the programmer against many errors, like putting round pegs in square holes. Even with type safety the type system and usage is quite flexible. Only having reference object types and no value object types also makes the implementation efficient. You need only one binary (class file) for of List, Set etc., whereas e.g. C++ needs a new separate binary for every member type and need to recompile2 with that member type.

2see C++ templates

HOMDOSHVDHEE/FHTenL generics and enum types February 24, 2016 8/38

February 24, 2016 Type safety, enums and Generics 2

slide-3
SLIDE 3

Generics 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

Generics

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 February 24, 2016 9/38

Generics 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

Generics Example - String Container

Collection of Strings

p u b l i c c l a s s StringContainer { p r i v a t e String value ; p u b l i c String getValue ( ) { r e t u r n value ; } p u b l i c void setValue ( String value ) { t h i s . value = value ; } p u b l i c StringContainer ( String value ) { t h i s . value = value ; } }

HOMDOSHVDHEE/FHTenL Generics February 24, 2016 10/38

Generics 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

Generics Example - Integer Container

Collection of Integers

p u b l i c c l a s s IntegerContainer { p r i v a t e Integer value ; p u b l i c Integer getValue ( ) { r e t u r n value ; } p u b l i c void setValue ( Integer value ) { t h i s . value = value ; } p u b l i c IntegerContainer ( Integer value ) { t h i s . value = value ; } }

HOMDOSHVDHEE/FHTenL Generics February 24, 2016 11/38

Generics 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

Analysis of an example

What is remarkable

New class for every data type Basic structure is same Copy and waste

Consequences:

Bad extensibility Reduced maintainability Bigger chance for errors

HOMDOSHVDHEE/FHTenL Generics February 24, 2016 12/38

February 24, 2016 Type safety, enums and Generics 3

slide-4
SLIDE 4

Generics 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

Generics

Generic Container

p u b l i c c l a s s GenericContainer <C> { p r i v a t e C value ; p u b l i c C getValue ( ) { r e t u r n value ; } p u b l i c void setValue ( C value ) { t h i s . value = value ; } p u b l i c GenericContainer ( C value ) { t h i s . value = value ; } }

HOMDOSHVDHEE/FHTenL Generics February 24, 2016 13/38

Generics 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

Generics Container

In stead of a concrete type we have a formal Type parameter (C in the example) The instantiation takes place based on the parameterized type All generic operations can now have a type.

HOMDOSHVDHEE/FHTenL Generics February 24, 2016 14/38

Generics 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

Generics in Methods

A class can also have generic (static) methods. The Type parameter is applied on the current method definition only.

Typed (static) method

p u b l i c s t a t i c <T> T random ( T m , T n ) { r e t u r n Math . random ( ) > 0.5 ? m : n ; }

HOMDOSHVDHEE/FHTenL Generics February 24, 2016 15/38

Generics 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

Implementation of Generic classes, type erasure

Instead of the generic type, Object is used. Everywhere a type access is used, a typecast is applied for you

HOMDOSHVDHEE/FHTenL Generics February 24, 2016 16/38

February 24, 2016 Type safety, enums and Generics 4

slide-5
SLIDE 5

Generics 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

Implementation of Generics - 2

The compiler erases the type information, so it is not available in the class file or at runtime Reason: The old, pre-generics code would not know what to do. Think of the donkey in the car. This implies that the following code produces a compiler error. At runtime only the raw GenericContainer is available.

Consequence of type erasure

GenericContainer <String> genericStringContainer = new GenericContainer <String> ( "A String" ) ; i f ( genericStringContainer i n s t a n c e o f ← ֓ GenericContainer<String >){ // ... }

HOMDOSHVDHEE/FHTenL Generics February 24, 2016 17/38

Generics 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

Implementation of Generics - 2

This is cheating

List<Integer> list = new ArrayList<Integer >() ; ( ( List ) list ) . add ( "a String" ) ; System . out . println ( list . get (0) ) ;

What’s this? Of course if you want to cheat, you can. To prevent this kind of folly, wrap your collectiona , say Set, in a checkedSet like

Set<String> s = Collections.checkedSet( new ← ֓ TreeSet<>(), String.class );

The class object, passed as second parameter, stands guard to make sure no

  • ther type gets in.

aList, Map, Set

HOMDOSHVDHEE/FHTenL Generics February 24, 2016 18/38

Generics 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

Putting bound to types

You can constrain or put bounds on the type The extends keyword can be followed by both a class or an interface Using & you can use a combination of types

public class Container <C extends Person & Serializable>...3

3extends here applies to both classes and interfaces; read it as

inherits from.

HOMDOSHVDHEE/FHTenL Generics February 24, 2016 19/38

Generics 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

Wildcard usage

In generic code, <?>, called the wildcard, represents an unknown type, which is of course implicitly of type Object. You can use the wildcard with upper (of which should the type be a child) or lower (what type should at least be accepted) bound. This is the preferred usage, instead of using a raw type, that is, omitting the type parameter altogether.

HOMDOSHVDHEE/FHTenL Generics February 24, 2016 20/38

February 24, 2016 Type safety, enums and Generics 5

slide-6
SLIDE 6

Generics 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

Wildcard with upper bound

Upper wildcard bounds are easy to understand: you specify what class hierarchy you accept to have a compatible assignment. You use it when a some value is produced, such as in a collection that is used as input parameter, which produces the objects. Here the list is assumed to produce Foo objects or children of Foo. So the method knows what operations are available on the objects. (These are the methods defined in type Foo).

HOMDOSHVDHEE/FHTenL Generics February 24, 2016 21/38

Generics 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

Producer example

List<Shape> shapes = new ArrayList <>() ; /** * Add shapes from list to this. * ShapesToAdd is the "producer" of the shapes * to add. * * @param shapesToAdd */ void addShapes ( Collection <? extends Shape> ← ֓ shapesToAdd ) { f o r ( Shape s : shapesToAdd ) { i f ( s . area ( ) > 0) { t h i s . shapes . add ( s ) ; } } }

HOMDOSHVDHEE/FHTenL Generics February 24, 2016 22/38

Generics 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

Wildcard with lower bound

If you want to pass a container that should receive

  • bjects that are selected in a method, than that

receiving container or consumer should accept the produced objects. The way to specify that is to say that the objects are the least things that should be acceptable. But that implies also the super types of the acceptable type. You specify that lower bound with <? super T>. In the example, the list consumes Integers, and must accept at least these, but a super of Integer (like Number or Object) will do, but a String won’t. Not that you only use this construct when defining the formal container parameter in the method taking a consumer as parameter.

HOMDOSHVDHEE/FHTenL Generics February 24, 2016 23/38

Generics 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

Consumer Example, method with consumer argument

30

/**

31

* Filter circles from shapes.

32

* Collect the circles in this into a collection .

33

* The specification (Generic Type and all)

34

* should accept circles.

35

* The collection is a "consumer" of Circle.

36

*

37

* @param collection

38

*/

39

void collectCircles ( List<? super Circle> collection ← ֓ ) {

40

f o r ( Shape c : shapes ) {

41

i f ( c i n s t a n c e o f Circle ) {

42

collection . add ( ( Circle ) c ) ;

43

}

44

}

45

}

HOMDOSHVDHEE/FHTenL Generics February 24, 2016 24/38

February 24, 2016 Type safety, enums and Generics 6

slide-7
SLIDE 7

Generics 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

Consumer Example, caller site

54

List<Shape> foundShapes = new ArrayList <>() ;

55

List<Object> foundObjects = new ArrayList <>() ;

56

List<Circle> foundCircles = new ArrayList <>() ;

57 58

toy . collectCircles ( foundObjects ) ;

59

toy . collectCircles ( foundShapes ) ;

60

toy . collectCircles ( foundCircles ) ;

Without the wildcard with lower (super) bound, the method calls in lines 58 and 59 would be rejected by the compiler a message looking like this:

1 shapes/ BrokenToy .java :58: error: method collectCircles in class 2 BrokenToy cannot be applied to given types; 3

  • toy. collectCircles ( foundObjects );

4 ˆ 5 required : List <Circle > 6 found: List <Object > 7 reason: argument mismatch ; List <Object > cannot be converted to List <← ֓ Circle >

HOMDOSHVDHEE/FHTenL Generics February 24, 2016 25/38

Generics 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

P.E.C.S.

P roducer E xtends C onsumer S uper PECS colloquial for Pectoralis major muscle. Remember, or I will be back.

HOMDOSHVDHEE/FHTenL Generics February 24, 2016 26/38

Generics 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

extends and super Summary

It is important to notice that the wild card types specify either the element or the container, not both. Producer specification uses extends to guarantee the type

  • f elements that come out of that producer.

A Producer of Foo is therefor specified as producing Foos or children of Foo Consumer specification uses super to guarantee what the container will accept. Super is applicable because an instance of a type in java is also of its super type, implying that a container

  • f a super type will also accept the lower bound. A

consumer of Bar must accept Bars, but any container that accepts Baf, the super of Bar will also fit the bill because a super declared variable will always accept a reference to a sub type object.

HOMDOSHVDHEE/FHTenL Generics February 24, 2016 27/38

enums 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

enums

Pieter van den Hombergh Thijs Dorssers Richard van den Ham Uwe van Heesch

Fontys Hogeschool voor Techniek en Logistiek

February 24, 2016

HOMDOSHVDHEE/FHTenL enums February 24, 2016 28/38

February 24, 2016 Type safety, enums and Generics 7

slide-8
SLIDE 8

enums 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

Use for constants

Constant values, reasons to define them. Traditional way: C:

1 #define BLACK 0x000000

  • r

1 enum {MON ,TUE ,WED ,THU ,FRI ,SAT ,SUN} WEEKDAYS ;

which is a just a list of named int values. Equivalent code in Java:

1 public static final int BLACK = 0 x000000 ; 2 public static final String DEFAULT_GREETING = "Hello"

In many cases you want to create a value domain, like in the c-enum example.

HOMDOSHVDHEE/FHTenL enums February 24, 2016 29/38

enums 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

Problems with traditional constants and value domains

There is no Type defined. The constants are just named integers or Strings. No Type: no type-safety; You can assign any value to the int/String, not just the named constants. In short, this use is not type safe as in assignment safe.

HOMDOSHVDHEE/FHTenL enums February 24, 2016 30/38

enums 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

Java enums

Addresses the type safeness issues. Defines a (fixed) domain with (fixed, final) values. Is a class, so can implement interfaces. An enum type is a subclass of Enum, and therefor cannot extend other classes. Can have multiple domain values, and all values are constants (static final in Java speak).

You can, however, specialize the instances by

  • verwriting methods declared in the enum type.

You cannot create new instances. That has already been done between compiler and class loader. A enum type declare inside another class behaves as a static inner class, i.e. has NO association with the outer class.

HOMDOSHVDHEE/FHTenL enums February 24, 2016 31/38

enums 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

Math op example

The interface:

1 p u b l i c i n t e r f a c e MathOp { 2 double apply ( double x , double y ) ; 3 }

which is implemented in the enum on the next sheets. Note that the method to be implemented is NOT implemented in the enum class object, but rather in the specializing instances, which all MUST implement the apply(double, double) method. source: effective Java, second edition.

HOMDOSHVDHEE/FHTenL enums February 24, 2016 32/38

February 24, 2016 Type safety, enums and Generics 8

slide-9
SLIDE 9

enums 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

Implementing as enum

6 p u b l i c enum Operation implements MathOp { 7 8 PLUS ( "+" ) { 9 @Override 10 p u b l i c double apply ( double x , double y ) { 11 r e t u r n x + y ; 12 } 13 } , 14 MINUS ( "-" ) { 15 @Override 16 p u b l i c double apply ( double x , double y ) { 17 r e t u r n x − y ; 18 } 19 } , 20 TIMES ( "*" ) { 21 @Override 22 p u b l i c double apply ( double x , double y ) { 23 r e t u r n x ∗ y ; 24 } 25 } , 26 DIVIDE ( "/" ) { 27 @Override 28 p u b l i c double apply ( double x , double y ) { 29 r e t u r n x / y ; 30 } 31 };

HOMDOSHVDHEE/FHTenL enums February 24, 2016 33/38

enums 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

Implementation other details

33 p r i v a t e f i n a l String symbol ; 34 35 Operation ( String symbol ) { 36 t h i s . symbol = symbol ; 37 } 38 39 @Override 40 p u b l i c String toString ( ) { 41 r e t u r n symbol ; 42 }

HOMDOSHVDHEE/FHTenL enums February 24, 2016 34/38

enums 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

Implementation, helpers

44 // Implementing a fromString method on an enum type - Page 154 45 p r i v a t e s t a t i c f i n a l Map<String , Operation> stringToEnum 46 = new HashMap<String , Operation >() ; 47 48 s t a t i c { // Initialize map from constant name to enum constant 49 f o r ( Operation

  • p

: values ( ) ) { 50 stringToEnum . put (

  • p . toString ( ) ,
  • p

) ; 51 } 52 } 53 54 // Returns Operation for string , or null if string is invalid 55 p u b l i c s t a t i c Operation fromString ( String symbol ) { 56 r e t u r n stringToEnum . get ( symbol ) ; 57 }

HOMDOSHVDHEE/FHTenL enums February 24, 2016 35/38

enums 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

Implementation, use

48 // Test program to perform all

  • perations
  • n given
  • perands

49 p u b l i c s t a t i c void main ( String [ ] args ) { 50 double x = Double . parseDouble ( args [ ] ) ; 51 double y = Double . parseDouble ( args [ 1 ] ) ; 52 f o r ( Operation

  • p

: Operation . values ( ) ) { 53 System . out . printf ( "%f %s %f = %f%n" , x ,

  • p ,

y ,

  • p . apply ( ←

֓ x , y ) ) ; 54 } 55 Operation o = Operation . DIVIDE ; 56 double z = o . apply ( 2.0 D , 3.0 D ) ; 57 }

HOMDOSHVDHEE/FHTenL enums February 24, 2016 36/38

February 24, 2016 Type safety, enums and Generics 9

slide-10
SLIDE 10

enums 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

Java enum consequences and summary

Type-safeness as in: defines a true type, with all the advantages that has in a type safe language. All values are created as singletons. The instances are available once the class loader is ready with the class. The enums can have members, just like normal classes. The enum instances can have operations with a possible per value different implementation. An enum class is a final class, so you cannot extend the domain (adding enum values) without recompiling the enum class file and the client application. Quiz: Client?

HOMDOSHVDHEE/FHTenL enums February 24, 2016 37/38

enums 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

Questions and links

Not all understood? For next time read: BlueJ: Chapter 12.5 Java enum type tutorial at http://docs.oracle.com/ javase/tutorial/java/javaOO/enum.html Java generics tutorial at http://docs.oracle.com/ javase/tutorial/java/generics/

Questions?

Questions or remarks?

HOMDOSHVDHEE/FHTenL enums February 24, 2016 38/38

February 24, 2016 Type safety, enums and Generics 10