Java Reference
In-Depth Information
4
The Java Type System
m
e
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.
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
dynami‐
cally 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 ref‐
erence type. This chapter starts by discussing another very important kind of refer‐
ence 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 intro‐
duced in Java 8.
Let's get started by taking a look at interfaces—probably the most important of Java's
reference types after classes, and a key building block for the whole of Java's type
system.