Chapter 4. The Java Type System
In this chapter, we move beyond basic object-oriented programming with classes and into the additional concepts required to work effectively with Java’s static type system.
Note
A statically typed language is one in which variables have definite types, and where it is a compile-time error to assign a value of an incompatible type to a variable. Java is an example of a statically typed language. Languages that only check type compatibility at runtime are called dynamically typed—JavaScript is an example of a dynamically typed language.
Java’s type system involves not only classes and primitive types, but also other kinds of reference type that are related to the basic concept of a class, but which differ in some way, and are usually treated in a special way by javac or the JVM.
We have already met arrays and classes, two of Java’s most widely used kinds of reference type. This chapter starts by discussing another very important kind of reference type—interfaces. We then move on to discuss Java’s generics, which have a major role to play in Java’s type system. With these topics under our belts, we can discuss the differences between compile-time and runtime types in Java.
To complete the full picture of Java’s reference types, we look at specialized kinds of classes and interfaces—known as enums and annotations. We conclude the chapter by looking at nested types and finally the new lambda expressions functionality introduced in Java 8.
Let’s get ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access