Imagine a bacon-wrapped Ferrari. Still not better than our free technical reports.

Generics: Past, Present and Future by Richard Warburton & Raoul-Gabriel Urma

We continue our series of Virtual JUG session recaps with this post, talking through what we learnt from the recent session “Generics: Past, Present and Future” by Richard Warburton and Raoul-Gabriel Urma.

Usually, the Virtual JUG speakers are among the best Java minds in the world, and this time was no exception.

Richard Warburton, an independent Software Engineer and a Trainer, empirical technologist and a problem solver! Recently has written a book on Java 8 lambdas, he’s a world famous conference speaker and a leader London Java Community. Also Richard has a PhD in Computer Science from The University of Warwick. You can find and socialize with him on Twitter, he goes by @RichardWarburto.

Raoul-Gabriel Urma is an instructor and frequent conference speaker, author of the books “Java 8 in Action” and ”Introducing Java 8”. Raoul is a co-founder of Cambridge Coding Academy and a Fellow of the Royal Society of Arts. He’s a Master of Engineering, working towards a PhD in Computer Science at the University of Cambridge. And in the past he has worked for a few giant companies, Google, eBay, and Goldman Sachs: he has seen things! Chat with him on twitter: @RaoulUK.

I’ve embedded the session below, so you can enjoy it yourself. However, read this post till the end for the TL;DW version and watch my interview with Richard and Raoul! They are great Java experts and have lots of knowledge to share!  

Here’s the session:

Generics: the Past

Generics in Java can often make you ponder the reason of life, why you even program and why you use Java as the platform of choice. In this session Richard and Raoul explain why the signatures of Java generics often look like a crazy regex. They also discuss what developers actually want from generics.

Java generics can be confusing

Well, the original idea behind generics is pretty simple. To specify the exact type of the elements that go into a collection. You can use a raw collection type, say a List, and add elements of different types like a String and an Integer.The list consumes them just fine, but you might get a ClassCastException when you iterate over it.

The idea behind generics was to express the types of a collection statically. So that if there is a problem, instead of failing at the runtime, we can declare the generic type of the collection, say List and happily see all the type safety violations when compiling our code.

In general, generics represent a careful balance of simplicity, conciseness and static safety in the platform. Other languages, like Scala or Clojure, or Fantom, can solve the tradeoff between these three qualities differently and end up with a different system for generics.

Having explained all that Richard and Raoul continued to explain what features of generics are really interesting, but less widely used in the Java industry.

Other Features of Generics and why developers might use them

Intersection types are one of the less often used generic patterns. In a nutshell, an intersection type is a combination of types that state the variable or method argument simultaneously belongs to all of the specified types.  

So if you need to declare a method that operates on something that is both of type DataInput and Closeable, you don’t necessary have to introduce a new interface that extends both of these, but you can use the intersection type like on the snippet below:

private static <I extends DataInput, Closeable> void doWork(I source) {
  // here you can invoke DataInput methods on the source object
  // and Closeable methods on the same object without any casts

Now, you’ll need a bit more experience and knowledge to parse and understand method signatures like this one below (which is of the Collections.max method by the way), but the main principle of the intersection types stays the same.

<T extends Object & Comparable<? super T>> T max(Collection<? extends T> coll);

During the session we also received an explanation why the Object declaration is necessary in that signature. The short answer is – backward compatibility, the long answer – you have to watch the session! You’ll be dangerous enough to know how to automatically generate a serializable lambda, even if the functional interface is not serializable.

During the session Richard and Raoul also described thecuriously recursive generics definitions.So if you’re wondering why you can often see a parametrization that looks like: class Enum> you have one more reason to watch the session. In a nutshell, if will allow the compiler to understand the types of the variable in the methods that operate on parameterized values already. Confused? Just watch the session already for a really good and concise explanation of this phenomenon.

Don’t worry though, you’re unlikely to have to deal with such constructs on a daily basis. Based on what the speakers shared with us on the session, 90% of the usage of generics are on the collection classes and the remaining 10% are wildcards to remove compilation warnings. For instance: Class.

Future of Generics

Don’t assume generics are done and dusted in the Java language. There are a number of proposals that are currently in the works that touch the topic of generics. For example JEP 8043488: Improved variance for generic classes and interfaces proposes to change javac, the Java Language specification, the JVM and various other parts of the Java platform to allow the type level contra-/covariance declaration. This basically means, we would be able to say:

interface Predicate<? super T> {
  boolean test(T arg);

And not specify the super or extends clause of the type parameter declaration where the Predicate type is used. So the correct positioning of supers and extends in the generic declarations moved from the client code one level deeper into the libraries. Which makes the lives of regular Java developers much easier.

Naturally, this change is not all that this JEP aims to fix. It talks about majorly redesigning the type system of the Java language, but for the average developer, that’s probably the most visible change to the language.

Another change that is coming is the introduction of Value Types, which are the future structs of Java. The objects that have no identity and can be laid out in memory without any overhead. Think of an int[], we know the size of one int and thus we can just pack a bunch of them next to each other. Which offers immense performance wins across many parts of the JDK.

This effort, called the Specialization is ongoing under the code name Valhalla, where the bleeding edge builds of Java with value types ride off into the future. Which means that you can download them already now and play with value types yourself.


After the session, during our regular RebelLabs interview with Virtual JUG speakers I had a chance to ask Raoul and Richard about the best ways new developers can get into Java development, how to learn more intricate details of the software industry and which open source projects they like the most.
Watch the interview and if you have comments tell us in the section below or on Twitter: @ZeroTurnaround.


Read next: