Enums, Generics, Nested Typespsnyder/cs342-summer2017/... · Which is false of Enums? A. Enums are...

33
Enums, Generics, Nested Types June 23, 2017

Transcript of Enums, Generics, Nested Typespsnyder/cs342-summer2017/... · Which is false of Enums? A. Enums are...

Page 1: Enums, Generics, Nested Typespsnyder/cs342-summer2017/... · Which is false of Enums? A. Enums are a programer convenience that allow you to give your values more meaningful names

Enums, Generics, Nested Types

June 23, 2017

Page 2: Enums, Generics, Nested Typespsnyder/cs342-summer2017/... · Which is false of Enums? A. Enums are a programer convenience that allow you to give your values more meaningful names

Reading Quiz

Page 3: Enums, Generics, Nested Typespsnyder/cs342-summer2017/... · Which is false of Enums? A. Enums are a programer convenience that allow you to give your values more meaningful names

What is true of Generics?

A. They allow you to maintain more type information in your code

B. They are required to store different types of objects in a container

C. They restrict you to having a single type of object in a container

D. They only work with primitive data types.

Page 4: Enums, Generics, Nested Typespsnyder/cs342-summer2017/... · Which is false of Enums? A. Enums are a programer convenience that allow you to give your values more meaningful names

Whats the Result?

A. "0"

B. "1"

C. "" (ie empty string)

D. Won't compile

E. Runtime error

ArrayList<Object> parent = new ArrayList<Object>(); Integer child = new Integer(0);

parent.add(child); System.out.println(parent.size());

Page 5: Enums, Generics, Nested Typespsnyder/cs342-summer2017/... · Which is false of Enums? A. Enums are a programer convenience that allow you to give your values more meaningful names

Whats the Result?

A. Parent is an ArrayList that can contain any type of object

B. Parent is an ArrayList that can contain only Integers (and subtypes)

C. Won't compile

D. Runtime error

ArrayList<Object> parent = new ArrayList<Integer>();

Page 6: Enums, Generics, Nested Typespsnyder/cs342-summer2017/... · Which is false of Enums? A. Enums are a programer convenience that allow you to give your values more meaningful names

Which is true of Nested Types

A. Nested types are a way of describing inheritance

B. Nested types are a way of describing classes with a more convenient syntax

C. Nested types are a way of controlling type visibility

D. Nested types are a way of handing concurrency

Page 7: Enums, Generics, Nested Typespsnyder/cs342-summer2017/... · Which is false of Enums? A. Enums are a programer convenience that allow you to give your values more meaningful names

Which is false of Enums?A. Enums are a programer convenience that allow you to give your values more meaningful names

B. Enums are a reliability mechanism that allow the type system to catch more errors

C. Enums are a reliability mechanism that allows the type system to perform additional checks

D. Enums are a performance mechanism that allow compiler optimizations

Page 8: Enums, Generics, Nested Typespsnyder/cs342-summer2017/... · Which is false of Enums? A. Enums are a programer convenience that allow you to give your values more meaningful names

Done!

Page 9: Enums, Generics, Nested Typespsnyder/cs342-summer2017/... · Which is false of Enums? A. Enums are a programer convenience that allow you to give your values more meaningful names

Housekeeping

• Homework 3 is out

• Class pace

• Feedback

Page 10: Enums, Generics, Nested Typespsnyder/cs342-summer2017/... · Which is false of Enums? A. Enums are a programer convenience that allow you to give your values more meaningful names

Enums

Page 11: Enums, Generics, Nested Typespsnyder/cs342-summer2017/... · Which is false of Enums? A. Enums are a programer convenience that allow you to give your values more meaningful names

Problem

• There are cases where we'd like to capture a finite set of values / states

• We'd like the compiler to be able to catch errors

• Invalid range values

• Typos

Page 12: Enums, Generics, Nested Typespsnyder/cs342-summer2017/... · Which is false of Enums? A. Enums are a programer convenience that allow you to give your values more meaningful names

State Range Example• NetworkRequest.state

• initialized

• sent

• acknowledged

• receiving

• finished

Page 13: Enums, Generics, Nested Typespsnyder/cs342-summer2017/... · Which is false of Enums? A. Enums are a programer convenience that allow you to give your values more meaningful names

State Range Example• NetworkRequest.state

• initialized = 0

• sent = 1

• acknowledged = 2

• receiving = 3

• finished = 4

Page 14: Enums, Generics, Nested Typespsnyder/cs342-summer2017/... · Which is false of Enums? A. Enums are a programer convenience that allow you to give your values more meaningful names

State Range Example• NetworkRequest.state

• initialized = 0

• sent = 1

• acknowledged = 2

• receiving = 3

• finished = 4

switch (aRequest.state) { case 0: // Send the request break;

case 1: // Wait for the request break;

// Other states… }

Issues?

Page 15: Enums, Generics, Nested Typespsnyder/cs342-summer2017/... · Which is false of Enums? A. Enums are a programer convenience that allow you to give your values more meaningful names

State Range Example• NetworkRequest.state

• initialized = "init"

• sent = "sent"

• acknowledged = "ack"

• receiving = "recv"

• finished = "fin"

switch (aRequest.state) { case "init": // Send the request break;

case "sent": // Wait for the request break;

// Other states… }

Issues?

Page 16: Enums, Generics, Nested Typespsnyder/cs342-summer2017/... · Which is false of Enums? A. Enums are a programer convenience that allow you to give your values more meaningful names

Java Solution

• Enums

• Variables can have a finite number of values

• Values can have human readable names

• Compiler can catch errors

Page 17: Enums, Generics, Nested Typespsnyder/cs342-summer2017/... · Which is false of Enums? A. Enums are a programer convenience that allow you to give your values more meaningful names

Java Solutionpublic enum NetworkStates { INITIALIZED, SENT, ACKNOWLEDGED, RECEIVING, FINISHED}

Page 18: Enums, Generics, Nested Typespsnyder/cs342-summer2017/... · Which is false of Enums? A. Enums are a programer convenience that allow you to give your values more meaningful names

Java Solutionpublic enum NetworkStates { INITIALIZED, SENT, ACKNOWLEDGED, RECEIVING, FINISHED}

switch (aRequest.state) { case NetworkStates.INITIALIZED: // Send the request break;

case NetworkStates.SENT: // Wait for the request break;

// Other states… }

Page 19: Enums, Generics, Nested Typespsnyder/cs342-summer2017/... · Which is false of Enums? A. Enums are a programer convenience that allow you to give your values more meaningful names

Generics

Page 20: Enums, Generics, Nested Typespsnyder/cs342-summer2017/... · Which is false of Enums? A. Enums are a programer convenience that allow you to give your values more meaningful names

Problem• Java's type system is double edged sword

• Specificity can require redundancy

• Code is written to work with one datatype

• Rewrite code for other datatypes?

• How to have specificity (types) and convenience (concision)?

Page 21: Enums, Generics, Nested Typespsnyder/cs342-summer2017/... · Which is false of Enums? A. Enums are a programer convenience that allow you to give your values more meaningful names

Randomizer.java –>

Page 22: Enums, Generics, Nested Typespsnyder/cs342-summer2017/... · Which is false of Enums? A. Enums are a programer convenience that allow you to give your values more meaningful names

Java Solution: Generics• Type variables for method signatures

• Ensure consistency of types

• Ex: If you give me an X and a Y, I'll give you a X back

• String and an Integer

• Integer and Integer

• whatever

Page 23: Enums, Generics, Nested Typespsnyder/cs342-summer2017/... · Which is false of Enums? A. Enums are a programer convenience that allow you to give your values more meaningful names

Randomizer.java –>

Page 24: Enums, Generics, Nested Typespsnyder/cs342-summer2017/... · Which is false of Enums? A. Enums are a programer convenience that allow you to give your values more meaningful names

Generics in Standard Library

• Extremely common

• Containers

• ArrayList<E>

• Set<E>

• Map<K, V>

• Documented extensively (ex, ArrayList)

Page 25: Enums, Generics, Nested Typespsnyder/cs342-summer2017/... · Which is false of Enums? A. Enums are a programer convenience that allow you to give your values more meaningful names

HashExample.java –>

Page 26: Enums, Generics, Nested Typespsnyder/cs342-summer2017/... · Which is false of Enums? A. Enums are a programer convenience that allow you to give your values more meaningful names

It can get Wild…

• Functions https://docs.oracle.com/javase/8/docs/api/java/util/function/package-summary.html

• NestedHashMap< HashMap<K, V>, HashMap<V, K> >

Page 27: Enums, Generics, Nested Typespsnyder/cs342-summer2017/... · Which is false of Enums? A. Enums are a programer convenience that allow you to give your values more meaningful names

HashReverser.java –>

Page 28: Enums, Generics, Nested Typespsnyder/cs342-summer2017/... · Which is false of Enums? A. Enums are a programer convenience that allow you to give your values more meaningful names

Nested Types

Page 29: Enums, Generics, Nested Typespsnyder/cs342-summer2017/... · Which is false of Enums? A. Enums are a programer convenience that allow you to give your values more meaningful names

Just the Basics• `class` is overloaded in Java

• Types

• Namespace

• Data

• Functionality

• Nested types are a bandaid for this

Page 30: Enums, Generics, Nested Typespsnyder/cs342-summer2017/... · Which is false of Enums? A. Enums are a programer convenience that allow you to give your values more meaningful names

Problem Example

• Java doesn't have `function` (or anything like it)

• How to pass functionality around?

• Data-less classes

• These are one-offs

Page 31: Enums, Generics, Nested Typespsnyder/cs342-summer2017/... · Which is false of Enums? A. Enums are a programer convenience that allow you to give your values more meaningful names

NestedTypes.java –>

Page 32: Enums, Generics, Nested Typespsnyder/cs342-summer2017/... · Which is false of Enums? A. Enums are a programer convenience that allow you to give your values more meaningful names
Page 33: Enums, Generics, Nested Typespsnyder/cs342-summer2017/... · Which is false of Enums? A. Enums are a programer convenience that allow you to give your values more meaningful names

Homework 3