TwiceToldby Develoker
ConceptsSystem designLabsCheat sheetsAbout

Develoker makes free tools for learning to build software. TwiceTold explains each idea twice, as an everyday analogy and the real thing; CloudArch lets you design cloud systems and put load on them.

About ยท Labs ยท Cheat sheets ยท Learning paths

โ† Back to Generics and type erasure

๐Ÿท๏ธ Generics and type erasure: cheat sheet

๐ŸŽจ The analogyBoxes labelled "apples only" are checked at the hall door, but the labels come off at the factory gate.
๐ŸŽ“ The regular explanationHow Java generics let the compiler check element types and drop casts, why List<Integer> is not a List<Number>, how wildcards follow PECS, and what type erasure removes at run time.
Why genericsStep 4 of 4
Plain boxA1key apple: A10P1key pear: P1123Apples only boxA1key apple: A1โœฆโœฆโœฆโœฆโœฆ0123๐Ÿ‘ฉPacker Ana๐Ÿ•ต๏ธDoor checkerok๐Ÿ‘จBen
  1. In the old days the boxes were plain. Ana packs an apple and then, by mistake, a pear. Nobody stops her.
  2. Ben takes the second item out, expecting an apple. It's a pear. The mistake shows up here, far from where Ana made it.
  3. Now the box has a label, "apples only", and a checker stands at the door. Ana tries the pear again and the checker turns it back at once.
  4. The apple passes the check and goes in. Ben takes it out without inspecting it, because the label promises it's an apple.
Generic classes, methods and boundsStep 4 of 4
Apples boxA1key apple: A10A2key ripe apple: A2123Bricks boxB1key brick: B10123๐Ÿท๏ธ Box design with a blank๐Ÿ•ต๏ธDoor checkernot a fruit๐Ÿ‘ฉโ€๐ŸŒพCleo, fruit judgereturns an Apple
  1. The factory has one box design with a blank on the label. Fill in "apples" and you get an apples box.
  2. The same design, with "bricks" in the blank, makes a bricks box. No new design needed.
  3. Cleo's job card says "pick the ripest from any fruit box". Handed the apples box, she hands back an apple.
  4. Someone hands Cleo the bricks box. Her card says fruit only, so the checker stops it before she ever starts.
Invariance vs covariant arraysStep 4 of 4
Apples box ยท stays Apple only0123Wooden apple crate ยท ArrayStoreExceptionA1key apple: A10123๐Ÿ‘จBen๐Ÿ•ต๏ธDoor checker
  1. Ben wants to call the apples box "a fruit box". The checker refuses, because then he could drop a pear in.
  2. The old wooden crate has its label burnt in, so the checker lets Ben call it a fruit crate.
  3. Ben puts an apple in. Fine.
  4. Then a pear. The crate reads its burnt-in label and refuses, right in the middle of the work instead of at the door.
Wildcards and PECSStep 4 of 4
Some kind of fruit box ยท take out onlyA1key apple: A10A2key apple: A2123Fruit or anything box ยท put in onlyA1key apple: A10123๐Ÿ‘ฉโ€๐ŸŒพCleo the moveronly an Object
  1. Cleo takes from a box of "some kind of fruit". Today it's apples. Whatever she pulls out is surely a fruit.
  2. She tries to put a pear back into that box. Not allowed. It might be the apples box, and a pear would be wrong.
  3. She puts the fruit into a box that accepts "fruit or anything bigger". Always safe.
  4. If she takes something back out of that box, all she knows is that it's "a thing". So take from "some kind of", put into "or bigger".
Type erasureStep 4 of 4
Plain crateA1key apple: A10123๐Ÿ‘ฉPacker Ana๐Ÿ•ต๏ธDoor checkertypes checked๐ŸšงFactory gatelabel removed๐ŸญShipping yardnew Apple() ok๐Ÿท๏ธ Label card
  1. In the packing hall the box says "apples only", and the checker makes sure every item fits the label.
  2. At the factory gate the label is peeled off. Downstairs it's a plain crate, with a little "check it's an apple" note where things come out.
  3. In the yard someone asks "is this an apples crate?" and "make me one more of whatever goes in here". No label left, so nobody can answer.
  4. So Ana sends a label card along on purpose. The yard reads "apple" off the card and can make a new apple.
Raw types and heap pollutionStep 4 of 4
Apples box ยท pollutedA1key apple: A10P1key pear: P1123๐Ÿ‘ดBob, old habits๐Ÿ•ต๏ธDoor checkerwarning silenced๐Ÿ‘ฉโ€๐ŸŒพCleo with a bundleโœ• โœ•๐Ÿ’ซBen
  1. The apples box holds an apple. Bob treats it as a plain box, so the checker can't tell what he puts in. It can only warn him.
  2. Bob drops a pear into the apples box. Nothing stops it and nothing notices. The box is now polluted.
  3. Much later Ben takes out "an apple" and gets the pear. Things go wrong for Ben, far from Bob.
  4. Cleo's card takes a bundle in a plain sack, so the checker warns too. She only reads from it, so she signs a promise and the warning goes away.

๐Ÿงฉ Key questions

Ana tries to drop a pear into a box labelled "apples only". When is the mistake caught?
Right away, by the checker at the door. That's the point of the label: the checker stops the pear before it goes anywhere.
Why won't the checker let Ben call an apples box "a fruit box"?
Because he could then put a pear into the apples box. An apples box is an apples box. Wooden crates allow the name but refuse the pear later, mid-work.
Cleo moves fruit from one box into another. Which labels should her job card ask for?
Take from "some kind of fruit", put into "fruit or anything bigger". What comes out of a "some kind of fruit" box is surely fruit, and a "fruit or bigger" box safely takes fruit.
Downstairs, someone asks a plain crate to make one more of whatever goes inside it. What does Ana need to do?
Send a label card along that says "apple". Labels are peeled at the factory gate, so the type has to travel on purpose.
Bob treats the apples box as a plain box and drops a pear in. When does something go wrong?
Later, when someone takes the pear out expecting an apple. The checker could only warn Bob, and the plain crate can't tell, so the problem shows up far away.