Java's Generics syntax provides us with a means to increase the reusability of our code by allowing us to build software, particularly library software, that can work on many different types, even with limited knowledge about those types. The most familiar examples are the classes in Java's core collections API which can store and retrieve data of arbitrary types, without degenerating those types to java.lang.Object.
However, while the generics mechanism is very simple to use in simple cases such as using the collections API, it's much more powerful than that. Frankly, it can also be a little puzzling.
This session investigates the issues of type erasure, assignment compatibility in generic types, co- and contra-variance, through to bridge methods.
Course outline
Type erasure
Two approaches for generics and Java's design choice
How to break generics (and how not to!)
Maintaining concrete type at runtime
Assignment compatibility of generic types
What's the problem–Understanding Liskov substitution in generic types
Co-variance
Two syntax options for co-variance
Contra-variance
Syntax for contra-variance
Worked examples with co- and contra-variance
Building arrays from generic types
Effective use of functional interfaces
Bridge methods
Review of overloading requirements
Faking overloading in generic types
Setup requirements
This course includes extensive live coding demonstrations and attendees will have access to the code that's created via a git repo. The majority of the examples will work in any version of Java from version 11 onwards, but some might use newer library features. You can use any Java development environment / IDE that you like and no other tooling is required.
Simon Roberts wrote his first program in 1978 in high school on punched cards. He started his career as a programmer building embedded control systems in a variety of assembly languages, C, and C++. Alongside programming, Simon taught part-time at a local college. In 1994 his career transitioned to full-time teacher and part time programmer, and he joined Sun Microsystems in 1995 where he worked until going independent in 2004
While at Sun he created training courses on a diverse range of Java topics, developed the original Sun Certified Java Programmer and Developer exams, and presented at JavaOne and other conferences on Java and Java performance topics.
Today, Simon is president of Dancing Cloud Services, LLC., based in Westminster Colorado, and he provides training, course development, and mentoring services in Java, Scala, JavaScript, Python, Go, along with design, and software architecture topics. Simon is equally comfortable offering training in classroom, recorded video, and live, remote-access, formats.
More About Simon »