Andrew Cooke | Contents | Latest | RSS | Previous | Next

C[omp]ute

Welcome to my blog, which was once a mailing list of the same name and is still generated by mail. Please reply via the "comment" links.

Always interested in offers/projects/new ideas. Eclectic experience in fields like: numerical computing; Python web; Java enterprise; functional languages; GPGPU; SQL databases; etc. Based in Santiago, Chile; telecommute worldwide. CV; email.

Personal Projects

Choochoo Training Diary

Last 100 entries

[Programming] React Leaflet; AliExpress Independent Sellers; Applebaum - Twilight of Democracy; [Politics] Back + US Elections; [Programming,Exercise] Simple Timer Script; [News] 2019: The year revolt went global; [Politics] The world's most-surveilled cities; [Bike] Hope Freehub; [Restaurant] Mama Chau's (Chinese, Providencia); [Politics] Brexit Podcast; [Diary] Pneumonia; [Politics] Britain's Reichstag Fire moment; install cairo; [Programming] GCC Sanitizer Flags; [GPU, Programming] Per-Thread Program Counters; My Bike Accident - Looking Back One Year; [Python] Geographic heights are incredibly easy!; [Cooking] Cookie Recipe; Efficient, Simple, Directed Maximisation of Noisy Function; And for argparse; Bash Completion in Python; [Computing] Configuring Github Jekyll Locally; [Maths, Link] The Napkin Project; You can Masquerade in Firewalld; [Bike] Servicing Budget (Spring) Forks; [Crypto] CIA Internet Comms Failure; [Python] Cute Rate Limiting API; [Causality] Judea Pearl Lecture; [Security, Computing] Chinese Hardware Hack Of Supermicro Boards; SQLAlchemy Joined Table Inheritance and Delete Cascade; [Translation] The Club; [Computing] Super Potato Bruh; [Computing] Extending Jupyter; Further HRM Details; [Computing, Bike] Activities in ch2; [Books, Link] Modern Japanese Lit; What ended up there; [Link, Book] Logic Book; Update - Garmin Express / Connect; Garmin Forerunner 35 v 230; [Link, Politics, Internet] Government Trolls; [Link, Politics] Why identity politics benefits the right more than the left; SSH Forwarding; A Specification For Repeating Events; A Fight for the Soul of Science; [Science, Book, Link] Lost In Math; OpenSuse Leap 15 Network Fixes; Update; [Book] Galileo's Middle Finger; [Bike] Chinese Carbon Rims; [Bike] Servicing Shimano XT Front Hub HB-M8010; [Bike] Aliexpress Cycling Tops; [Computing] Change to ssh handling of multiple identities?; [Bike] Endura Hummvee Lite II; [Computing] Marble Based Logic; [Link, Politics] Sanity Check For Nuclear Launch; [Link, Science] Entropy and Life; [Link, Bike] Cheap Cycling Jerseys; [Link, Music] Music To Steal 2017; [Link, Future] Simulated Brain Drives Robot; [Link, Computing] Learned Index Structures; Solo Air Equalization; Update: Higher Pressures; Psychology; [Bike] Exercise And Fuel; Continental Race King 2.2; Removing Lowers; Mnesiacs; [Maths, Link] Dividing By Zero; [Book, Review] Ray Monk - Ludwig Wittgenstein: The Duty Of Genius; [Link, Bike, Computing] Evolving Lacing Patterns; [Jam] Strawberry and Orange Jam; [Chile, Privacy] Biometric Check During Mail Delivery; [Link, Chile, Spanish] Article on the Chilean Drought; [Bike] Extended Gear Ratios, Shimano XT M8000 (24/36 Chainring); [Link, Politics, USA] The Future Of American Democracy; Mass Hysteria; [Review, Books, Links] Kazuo Ishiguro - Never Let Me Go; [Link, Books] David Mitchell's Favourite Japanese Fiction; [Link, Bike] Rear Suspension Geometry; [Link, Cycling, Art] Strava Artwork; [Link, Computing] Useful gcc flags; [Link] Voynich Manuscript Decoded; [Bike] Notes on Servicing Suspension Forks; [Links, Computing] Snap, Flatpack, Appimage; [Link, Computing] Oracle is leaving Java (to die); [Link, Politics] Cubans + Ultrasonics; [Book, Link] Laurent Binet; VirtualBox; [Book, Link] No One's Ways; [Link] The Biggest Problem For Cyclists Is Bad Driving; [Computing] Doxygen, Sphinx, Breathe; [Admin] Brokw Recent Permalinks; [Bike, Chile] Buying Bearings in Santiago; [Computing, Opensuse] Upgrading to 42.3; [Link, Physics] First Support for a Physics Theory of Life; [Link, Bike] Peruvian Frame Maker; [Link] Awesome Game Theory Tit-For-Tat Thing; [Food, Review] La Fabbrica - Good Italian Food In Santiago; [Link, Programming] MySQL UTF8 Broken; [Link, Books] Latin American Authors

© 2006-2017 Andrew Cooke (site) / post authors (content).

Java Generics: A Review

From: "andrew cooke" <andrew@...>

Date: Sat, 13 Jan 2007 01:03:04 -0300 (CLST)

This is the first draft of a review for Slashdot.


Background to Generics

If you're a Java programmer you've probably heard of generics, an
extension to the type system that was introduced in Java 5.  They give
you, as a programmer, a way to write code even when you don't know exactly
what classes will be used.

The obvious example is collections - the author of a List class has no
idea what type of objects will be stored when the code is used.

Before generics, if you wanted to write code that handled unknown
classes you had to use make use of inheritance: write the code as if it
would get Objects, and then let the caller cast the result as
necessary.  Since casts happen at runtime any mistakes may cause a runtime
error (a ClassCastException).

Generics fix this.  They let you treat write code in which the classes are
named (a parameter) and the compiler can then check that the use of these
class parameters is consistent in your program.  So if you have a List of
Foo instances you write List<Foo> and the compiler
knows that when you read that list you will receive a Foo, not an
Object.


History

I'll get to the book in a moment, but first a little history.  If you know
any type theory - particularly as used in functional languages like ML and
Haskell - then you'll recognise it as "parametric
polymorphism".  You'll also know that it is incredibly useful, and wonder
how Java programmers could ever have managed without it.

Which explains why Philp Wadler, one of the people responsible for
Haskell, was part of a team that wrote GJ (Generic Java), one of the
experimental Java mutations (others included PJ and Pizza) that, back in
the day (late 90s) helped explore how parametric polymorphism could be
added to Java, and which formed the basis for the generics
introduced in Java 5.

So if you want to understand generics, Wadler is your man.  Which, in
turn, explains why I jumped at the chance to review O'Reilly's `Java
Generics and Collections', by Maurice Naftalin and Philip Wadler.


The Book

This is a moderately slim book (just under 300 pages).  It looks like any
other O'Reilly work - the animal is an Alligator this time.  It's well
organised, easy to read, and has a decent index.

There's an odd discrepancy, though: Wadler is the generics Guru; this is
going to be `the generics reference'; generics are sexy (in
relative terms - we're talking Java here) and collections are not; the
title has "Java Generics" in great big letters with "and Collections" in
little tiny ones down in a corner.  Yet very nearly half this book is
dedicated to collections.

So in the next section I'll justify the `reference' comment above, and in
the one after I'll take a look at the collections half of the book and ask
to what extent it's padding.


Part I - Generics

This is a great, practical read.  It starts simply, introducing a
range of new features in Java 5, and then builds rapidly.

If you are completely new to generics, you'll want to read slowly.
Everything is here, and it's very clear and friendly, but there are not
the chapters of simple, repeated examples you might find a fatter book. 
Within just 30 pages you meet pretty much all of generics,
including wildcards and constraints.

If that makes your head spin, don't worry.  Read on.  The next hundred or
so pages don't introduce any new syntax, but instead discuss a wide range
of related issues.  The chapters on "Comparisons and Bounds" and
"Declarations" contain more examples that will help clarify what
generics do.  And the following chapters on "Evolution",
"Reification", and "Reflection" will explain exactly why.

So the first seven chapters introduce generics and then justify the
implementation - any programmer that takes the time to understand this
will have a very solid base in generics.

The next two chapters, however, were my favourites.  "Effective Generics"
and "Design Patterns" give sensible, practical advice on using generics in
your work, including the best explanation of <X extends Foo<X>> I've seen
yet (so if you don't know what I am talking about here, read the book).

(A practical note here - if at all possible, use Java 6 with generics.
Java 5 had some sneaky bugs).


Part II - Collections

This part of the book was more along O'Reilly's `Nutshell' lines: the
different chapters explore different collection types in detail.  I must
admit that at first I skipped this - it looked like API docs
re-hashed to extend the size of the book.

But then I felt bad, because I was supposed to be reviewing this book
(full disclosure: if you review a book for Slashdot you get to keep it). 
And you know what?  It turned out to be pretty interesting.
I've programmed in Java for (too many) years, and I guess I've not been
quite as dedicated to tracking how the librray has changed as I should
have - I learnt a lot.

The way I see it now, this part is a bonus: the first half, on
generics, makes this book one of the standards; the second half is an
extra treat I'm glad I stumbled across (I guess if you're some kind of
collection-fetish maybe it's even worth buying the book for).


Conclusions

I've used generics since the first beta release of Java 5 and had
experience with parametric polymorphism in functional languages before
that (in other words, I can tell my co- from my contra-variance).  So I
guess I'm heading towards the more expert end of the spectrum and I was
worried I'd find the book boring.  It wasn't.  After claiming to be expert
I don't want to spoil things with evidence that I'm actually stupid, but
reading this book cleared up a few `misunderstandings' I'd had.  I wish I
had read it earlier.

If you're new to generics, and you don't mind thinking, I recommend this
book.  If you're a Java programmer who's a bit confused by "<? super Foo>"
then this is the book for you.

The only people who shouldn't read this are people new to Java.  You need
to go elsewhere first.  This is not a book for beginners.

A great book in the classic - practical, concise and intelligent -
O'Reilly mould.

Andrew

Java Generics: A Review (Final Version)

From: "andrew cooke" <andrew@...>

Date: Sun, 14 Jan 2007 07:35:23 -0300 (CLST)

This has been "pending" on Slashdot all day, so I've put it here -
http://www.acooke.org/andrew/writing/compjava.html#generics

Andrew

Comment on this post