Chapter 5. 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. Languages that check type compatibility at runtime are called dynamically typed.
Java is a fairly classic example of a statically typed language. JavaScript is an example of a dynamically typed language that allows any variable to store any type of value. Python started off as dynamically typed but is adding the ability to selectively adopt aspects of static typing—an approach that some people call gradual typing.
The Java type system involves not only classes and primitive types but also other kinds of reference types that are related to the basic concept of a class but that 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 types. 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, ...
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