There may be times when you’ll want to restrict the kinds of types that are allowed to be passed to a type parameter. For example, a method that operates on numbers might only want to accept instances ofNumber
or its subclasses. This is what bounded type parameters are for.
To declare a bounded type parameter, list the type parameter’s name, followed by the
extends
keyword, followed by its upper bound, which in this example isNumber
. Note that, in this context,extends
is used in a general sense to mean either “extends” (as in classes) or “implements” (as in interfaces).
By modifying our generic method to include this bounded type parameter, compilation will now fail, since our invocation of/**
* This version introduces a bounded type parameter.
*/
public class Box<T> {private T t;
public void add(T t) {
this.t = t;
}public T get() {
return t;
}public <U extends Number> void inspect(U u){
System.out.println(“T: ” + t.getClass().getName());
System.out.println(“U: ” + u.getClass().getName());
}public static void main(String[] args) {
Box<Integer> integerBox = new Box<Integer>();
integerBox.add(new Integer(10));
integerBox.inspect(“some text”); // error: this is still String!
}
}
inspect
still includes aString
:
Box.java:21: <U>inspect(U) in Box<java.lang.Integer> cannot
be applied to (java.lang.String)
integerBox.inspect(“10”);
^
1 error
To specify additional interfaces that must be implemented, use the
&
character, as in:
<U extends Number & MyInterface>
Generic Methods and Constructors
Type parameters can also be declared within method and constructor signatures to create generic methods and generic constructors. This is similar to declaring a generic type, but the type parameter’s scope is limited to the method or constructor in which it’s declared.
Here we’ve added one generic method, named/**
* This version introduces a generic method.
*/
public class Box<T> {private T t;
public void add(T t) {
this.t = t;
}public T get() {
return t;
}public <U> void inspect(U u){
System.out.println(“T: ” + t.getClass().getName());
System.out.println(“U: ” + u.getClass().getName());
}public static void main(String[] args) {
Box<Integer> integerBox = new Box<Integer>();
integerBox.add(new Integer(10));
integerBox.inspect(“some text”);
}
}
inspect
, that defines one type parameter, namedU
. This method accepts an object and prints its type to standard output. For comparison, it also prints out the type ofT
. For convenience, this class now also has amain
method so that it can be run as an application.
The output from this program is:
T: java.lang.Integer
U: java.lang.String
By passing in different types, the output will change accordingly.
A more realistic use of generic methods might be something like the following, which defines a static method that stuffs references to a single item into multiple boxes:
public static <U> void fillBoxes(U u, List<Box<U>> boxes) {To use this method, your code would look something like the following:
for (Box<U> box : boxes) {
box.add(u);
}
}Crayon red = …;The complete syntax for invoking this method is:
List<Box<Crayon>> crayonBoxes = …;
Here we’ve explicitly provided the type to be used asBox.<Crayon>fillBoxes(red, crayonBoxes);
U
, but more often than not, this can be left out and the compiler will infer the type that’s needed:
This feature, known as type inference, allows you to invoke a generic method as you would an ordinary method, without specifying a type between angle brackets.Box.fillBoxes(red, crayonBoxes); // compiler infers that U is Crayon
Generic Types
Let’s update our
Box
class to use generics. We’ll first create a generic type declaration by changing the code “public class Box
” to “public class Box<T>
“; this introduces one type variable, namedT
, that can be used anywhere inside the class. This same technique can be applied to interfaces as well. There’s nothing particularly complex about this concept. In fact, it’s quite similar to what you already know about variables in general. Just think ofT
as a special kind of variable, whose “value” will be whatever type you pass in; this can be any class type, any interface type, or even another type variable. It just can’t be any of the primitive data types. In this context, we also say thatT
is a formal type parameter of theBox
class./**
* Generic version of the Box class.
*/
public class Box<T> {private T t; // T stands for “Type”
public void add(T t) {
this.t = t;
}public T get() {
return t;
}
}
As you can see, we’ve replaced all occurrences of
Object
withT
. To reference this generic class from within your own code, you must perform a generic type invocation, which replacesT
with some concrete value, such asInteger
:
You can think of a generic type invocation as being similar to an ordinary method invocation, but instead of passing an argument to a method, you’re passing a type argument —Box<Integer> integerBox;
Integer
in this case — to theBox
class itself. Like any other variable declaration, this code does not actually create a newBox
object. It simply declares thatintegerBox
will hold a reference to a “Box
ofInteger
“, which is howBox<Integer>
is read.
An invocation of a generic type is generally known as a parameterized type.
To instantiate this class, use the
new
keyword, as usual, but place<Integer>
between the class name and the parenthesis:
integerBox = new Box<Integer>();
Or, you can put the entire statement on one line, such as:
Box<Integer> integerBox = new Box<Integer>();
Once
integerBox
is initialized, you’re free to invoke itsget
method without providing a cast, as inBoxDemo3
:
Furthermore, if you try adding an incompatible type to the box, such aspublic class BoxDemo3 {public static void main(String[] args) {
Box<Integer> integerBox = new Box<Integer>();
integerBox.add(new Integer(10));
Integer someInteger = integerBox.get(); // no cast!
System.out.println(someInteger);
}
}
String
, compilation will fail, alerting you to what previously would have been a runtime bug:BoxDemo3.java:5: add(java.lang.Integer) in Box<java.lang.Integer>
cannot be applied to (java.lang.String)
integerBox.add(“10”);
^
1 error
It’s important to understand that type variables are not actually types themselves. In the above examples, you won’t find
T.java
orT.class
anywhere on the filesystem. Furthermore,T
is not a part of theBox
class name. In fact during compilation, all generic information will be removed entirely, leaving onlyBox.class
on the filesystem. We’ll discuss this later in the section on Type Erasure
Also note that a generic type may have multiple type parameters, but each parameter must be unique within its declaring class or interface. A declaration of
Box<T,T>
, for example, would generate an error on the second occurrence ofT
, butBox<T,U>
, however, would be allowed.
Type Parameter Naming Conventions
By convention, type parameter names are single, uppercase letters. This stands in sharp contrast to the variable naming conventions that you already know about, and with good reason: Without this convention, it would be difficult to tell the difference between a type variable and an ordinary class or interface name.
The most commonly used type parameter names are:
You’ll see these names used throughout the Java SE API and the rest of this tutorial.
- E – Element (used extensively by the Java Collections Framework)
- K – Key
- N – Number
- T – Type
- V – Value
- S,U,V etc. – 2nd, 3rd, 4th types
Generics Introduction
In any nontrivial software project, bugs are simply a fact of life. Careful planning, programming, and testing can help reduce their pervasiveness, but somehow, somewhere, they’ll always find a way to creep into your code. This becomes especially apparent as new features are introduced and your code base grows in size and complexity.
Fortunately, some bugs are easier to detect than others. Compile-time bugs, for example, tell you immediately that something is wrong; you can use the compiler’s error messages to figure out what the problem is and fix it, right then and there. Runtime bugs, however, can be much more problematic; they don’t always surface immediately, and when they do, it may be at a point in time that’s far removed from the actual cause of the problem.
Generics add stability to your code by making more of your bugs detectable at compile time. Some programmers choose to learn generics by studying the Java Collections Framework; after all, generics are heavily used by those classes. However, since we haven’t yet covered collections, this chapter will focus primarily on simple “collections-like” examples that we’ll design from scratch. This hands-on approach will teach you the necessary syntax and terminology while demonstrating the various kinds of problems that generics were designed to solve.
A Simple Box Class
Let’s begin by designing a nongenericBox
class that operates on objects of any type. It need only provide two methods:add
, which adds an object to the box, andget
, which retrieves it:public class Box {Since its methods accept or returnprivate Object object;
public void add(Object object) {
this.object = object;
}public Object get() {
return object;
}
}
Object
, you’re free to pass in whatever you want, provided that it’s not one of the primitive types. However, should you need to restrict the contained type to something specific (likeInteger
), your only option would be to specify the requirement in documentation (or in this case, a comment), which of course the compiler knows nothing about:
Thepublic class BoxDemo1 {public static void main(String[] args) {
// ONLY place Integer objects into this box!
Box integerBox = new Box();integerBox.add(new Integer(10));
Integer someInteger = (Integer)integerBox.get();
System.out.println(someInteger);
}
}
BoxDemo1
program creates anInteger
object, passes it toadd
, then assigns that same object tosomeInteger
by the return value ofget
. It then prints the object’s value (10) to standard output. We know that the cast fromObject
toInteger
is correct because we’ve honored the “contract” specified in the comment. But remember, the compiler knows nothing about this — it just trusts that our cast is correct. Furthermore, it will do nothing to prevent a careless programmer from passing in an object of the wrong type, such asString
:
Inpublic class BoxDemo2 {public static void main(String[] args) {
// ONLY place Integer objects into this box!
Box integerBox = new Box();// Imagine this is one part of a large application
// modified by one programmer.
integerBox.add(“10”); // note how the type is now String// … and this is another, perhaps written
// by a different programmer
Integer someInteger = (Integer)integerBox.get();
System.out.println(someInteger);
}
}
BoxDemo2
we’ve stored the number 10 as aString
, which could be the case when, say, a GUI collects input from the user. However, the existing cast fromObject
toInteger
has mistakenly been overlooked. This is clearly a bug, but because the code still compiles, you wouldn’t know anything is wrong until runtime, when the application crashes with aClassCastException
:Exception in thread “main”
java.lang.ClassCastException:
java.lang.String cannot be cast to java.lang.Integer
at BoxDemo2.main(BoxDemo2.java:6)
If the
Box
class had been designed with generics in mind, this mistake would have been caught by the compiler, instead of crashing the application at runtime.
Numbers and Strings
Numbers
This section begins with a discussion of theNumber
class (in thejava.lang
package) and its subclasses. In particular, this section talks about the situations where you would use instantiations of these classes rather than the primitive data types. Additionally, this section talks about other classes you might need to work with numbers, such as formatting or using mathematical functions to complement the operators built into the language.
Strings
Strings, which are widely used in Java programming, are a sequence of characters. In the Java programming language, strings are objects. This section describes using theString
class to create and manipulate strings. It also compares theString
andStringBuilder
classes.
Strings
Strings, which are widely used in Java programming, are a sequence of characters. In the Java programming language, strings are objects.
The Java platform provides the
String
class to create and manipulate strings.
Creating Strings
The most direct way to create a string is to write:
In this case, “Hello world!” is a string literal—a series of characters in your code that is enclosed in double quotes. Whenever it encounters a string literal in your code, the compiler creates aString greeting = “Hello world!”;
String
object with its value—in this case,Hello world!
.
As with any other object, you can create
String
objects by using thenew
keyword and a constructor. TheString
class has 11 constructors that allow you to provide the initial value of the string using different sources, such as an array of characters:
The last line of this code snippet displayschar[] helloArray = { ‘h’, ‘e’, ‘l’, ‘l’, ‘o’, ‘.’};
String helloString = new String(helloArray);
System.out.println(helloString);
hello
.
Note: TheString
class is immutable, so that once it is created aString
object cannot be changed. TheString
class has a number of methods, some of which will be discussed below, that appear to modify strings. Since strings are immutable, what these methods really do is create and return a new string that contains the result of the operation.
String Length
Methods used to obtain information about an object are known as accessor methods. One accessor method that you can use with strings is thelength()
method, which returns the number of characters contained in the string object. After the following two lines of code have been executed,len
equals 17:
A palindrome is a word or sentence that is symmetric—it is spelled the same forward and backward, ignoring case and punctuation. Here is a short and inefficient program to reverse a palindrome string. It invokes theString palindrome = “Dot saw I was Tod”;
int len = palindrome.length();
String
methodcharAt(i)
, which returns the ith character in the string, counting from 0.
Running the program produces this output:public class StringDemo {
public static void main(String[] args) {
String palindrome = “Dot saw I was Tod”;
int len = palindrome.length();
char[] tempCharArray = new char[len];
char[] charArray = new char[len];// put original string in an array of chars
for (int i = 0; i < len; i++) {
tempCharArray[i] = palindrome.charAt(i);
}// reverse array of chars
for (int j = 0; j < len; j++) {
charArray[j] = tempCharArray[len – 1 – j];
}String reversePalindrome = new String(charArray);
System.out.println(reversePalindrome);
}
}
To accomplish the string reversal, the program had to convert the string to an array of characters (firstdoT saw I was toD
for
loop), reverse the array into a second array (secondfor
loop), and then convert back to a string. TheString
class includes a method,getChars()
, to convert a string, or a portion of a string, into an array of characters so we could replace the firstfor
loop in the program above with
palindrome.getChars(0, len – 1, tempCharArray, 0);
Concatenating Strings
TheString
class includes a method for concatenating two strings:
This returns a new string that is string1 with string2 added to it at the end.string1.concat(string2);
You can also use the
concat()
method with string literals, as in:
Strings are more commonly concatenated with the“My name is “.concat(“Rumplestiltskin”);
+
operator, as in
which results in“Hello,” + ” world” + “!”
The“Hello, world!”
+
operator is widely used in
which printsString string1 = “saw I was “;
System.out.println(“Dot ” + string1 + “Tod”);
Such a concatenation can be a mixture of any objects. For each object that is not aDot saw I was Tod
String
, itstoString()
method is called to convert it to aString
.
Note: The Java programming language does not permit literal strings to span lines in source files, so you must use the+
concatenation operator at the end of each line in a multi-line string. For example,
Breaking strings between lines using theString quote = “Now is the time for all good ” +
“men to come to the aid of their country.”;
+
concatenation operator is, once again, very common in
Creating Format Strings
You have seen the use of theprintf()
andformat()
methods to print output with formatted numbers. TheString
class has an equivalent class method,format()
, that returns aString
object rather than aPrintStream
object.
Using
String’s
staticformat()
method allows you to create a formatted string that you can reuse, as opposed to a one-time print statement. For example, instead of
you can writeSystem.out.printf(“The value of the float variable is %f, while the value of the ” +
“integer variable is %d, and the string is %s”, floatVar, intVar, stringVar);
String fs;
fs = String.format(“The value of the float variable is %f, while the value of the ” +
“integer variable is %d, and the string is %s”, floatVar, intVar, stringVar);
System.out.println(fs);
Numbers
This section begins with a discussion of theNumber
class in thejava.lang
package, its subclasses, and the situations where you would use instantiations of these classes rather than the primitive number types.
This section also presents the
PrintStream
andDecimalFormat
classes, which provide methods for writing formatted numerical output.
Finally, the
Math
class injava.lang
is discussed. It contains mathematical functions to complement the operators built into the language. This class has methods for the trigonometric functions, exponential functions, and so forth.
Interfaces and Inheritance
Interfaces
You saw an example of implementing an interface in the previous lesson. You can read more about interfaces here—what they are for, why you might want to write one, and how to write one.
Inheritance
This section describes the way in which you can derive one class from another. That is, how a subclass can inherit fields and methods from a superclass. You will learn that all classes are derived from theObject
class, and how to modify the methods that a subclass inherits from superclasses. This section also covers interface-like abstract classes.
Inheritance
In the preceding lessons, you have seen inheritance mentioned several times. In the Java language, classes can be derived from other classes, thereby inheriting fields and methods from those classes.
The idea of inheritance is simple but powerful: When you want to create a new class and there is already a class that includes some of the code that you want, you can derive your new class from the existing class. In doing this, you can reuse the fields and methods of the existing class without having to write (and debug!) them yourself.
Definitions:
A class that is derived from another class is called a subclass (also a derived class, extended class, or child class). The class from which the subclass is derived is called a superclass (also a base class or a parent class).
Excepting
Object
, which has no superclass, every class has one and only one direct superclass (single inheritance). In the absence of any other explicit superclass, every class is implicitly a subclass ofObject
.
Classes can be derived from classes that are derived from classes that are derived from classes, and so on, and ultimately derived from the topmost class,
Object
. Such a class is said to be descended from all the classes in the inheritance chain stretching back toObject
.
A subclass inherits all the members (fields, methods, and nested classes) from its superclass. Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass.
The Java Platform Class Hierarchy
TheObject
class, defined in thejava.lang
package, defines and implements behavior common to all classes—including the ones that you write. In the Java platform, many classes derive directly fromObject
, other classes derive from some of those classes, and so on, forming a hierarchy of classes.
At the top of the hierarchy,
All Classes in the Java Platform are Descendants of Object
Object
is the most general of all classes. Classes near the bottom of the hierarchy provide more specialized behavior.
An Example of Inheritance
Here is the sample code for a possible implementation of aBicycle
class that was presented in the Classes and Objects lesson:
public class Bicycle {// the Bicycle class has three fields
public int cadence;
public int gear;
public int speed;// the Bicycle class has one constructor
public Bicycle(int startCadence, int startSpeed, int startGear) {
gear = startGear;
cadence = startCadence;
speed = startSpeed;
}// the Bicycle class has four methods
public void setCadence(int newValue) {
cadence = newValue;
}public void setGear(int newValue) {
gear = newValue;
}public void applyBrake(int decrement) {
speed -= decrement;
}public void speedUp(int increment) {
speed += increment;
}}
A class declaration for a
MountainBike
class that is a subclass ofBicycle
might look like this:
public class MountainBike extends Bicycle {// the MountainBike subclass adds one field
public int seatHeight;// the MountainBike subclass has one constructor
public MountainBike(int startHeight, int startCadence, int startSpeed, int startGear) {
super(startCadence, startSpeed, startGear);
seatHeight = startHeight;
}// the MountainBike subclass adds one method
public void setHeight(int newValue) {
seatHeight = newValue;
}}
MountainBike
inherits all the fields and methods ofBicycle
and adds the fieldseatHeight
and a method to set it. Except for the constructor, it is as if you had written a newMountainBike
class entirely from scratch, with four fields and five methods. However, you didn’t have to do all the work. This would be especially valuable if the methods in theBicycle
class were complex and had taken substantial time to debug.
What You Can Do in a Subclass
A subclass inherits all of the public and protected members of its parent, no matter what package the subclass is in. If the subclass is in the same package as its parent, it also inherits the package-private members of the parent. You can use the inherited members as is, replace them, hide them, or supplement them with new members:
The following sections in this lesson will expand on these topics.
- The inherited fields can be used directly, just like any other fields.
- You can declare a field in the subclass with the same name as the one in the superclass, thus hiding it (not recommended).
- You can declare new fields in the subclass that are not in the superclass.
- The inherited methods can be used directly as they are.
- You can write a new instance method in the subclass that has the same signature as the one in the superclass, thus overriding it.
- You can write a new static method in the subclass that has the same signature as the one in the superclass, thus hiding it.
- You can declare new methods in the subclass that are not in the superclass.
- You can write a subclass constructor that invokes the constructor of the superclass, either implicitly or by using the keyword
super
.
Private Members in a Superclass
A subclass does not inherit theprivate
members of its parent class. However, if the superclass has public or protected methods for accessing its private fields, these can also be used by the subclass.
A nested class has access to all the private members of its enclosing class—both fields and methods. Therefore, a public or protected nested class inherited by a subclass has indirect access to all of the private members of the superclass.
Casting Objects
We have seen that an object is of the data type of the class from which it was instantiated. For example, if we write
thenpublic MountainBike myBike = new MountainBike();
myBike
is of typeMountainBike
.
MountainBike
is descended fromBicycle
andObject
. Therefore, aMountainBike
is aBicycle
and is also anObject
, and it can be used whereverBicycle
orObject
objects are called for.
The reverse is not necessarily true: a
Bicycle
may be aMountainBike
, but it isn’t necessarily. Similarly, anObject
may be aBicycle
or aMountainBike
, but it isn’t necessarily.
Casting shows the use of an object of one type in place of another type, among the objects permitted by inheritance and implementations. For example, if we write
thenObject obj = new MountainBike();
obj
is both anObject
and aMountainbike
(until such time asobj
is assigned another object that is not aMountainbike
). This is called implicit casting.
If, on the other hand, we write
we would get a compile-time error becauseMountainBike myBike = obj;
obj
is not known to the compiler to be aMountainBike
. However, we can tell the compiler that we promise to assign aMountainBike
toobj
by explicit casting:
This cast inserts a runtime check thatMountainBike myBike = (MountainBike)obj;
obj
is assigned aMountainBike
so that the compiler can safely assume thatobj
is aMountainBike
. Ifobj
is not aMountainbike
at runtime, an exception will be thrown.
Note: You can make a logical test as to the type of a particular object using theinstanceof
operator. This can save you from a runtime error owing to an improper cast. For example:
Here theif (obj instanceof MountainBike) {
MountainBike myBike = (MountainBike)obj;
}
instanceof
operator verifies thatobj
refers to aMountainBike
so that we can make the cast with knowledge that there will be no runtime exception thrown.
Interfaces
There are a number of situations in software engineering when it is important for disparate groups of programmers to agree to a “contract” that spells out how their software interacts. Each group should be able to write their code without any knowledge of how the other group’s code is written. Generally speaking, interfaces are such contracts.
For example, imagine a futuristic society where computer-controlled robotic cars transport passengers through city streets without a human operator. Automobile manufacturers write software (Java, of course) that operates the automobile—stop, start, accelerate, turn left, and so forth. Another industrial group, electronic guidance instrument manufacturers, make computer systems that receive GPS (Global Positioning Satellite) position data and wireless transmission of traffic conditions and use that information to drive the car.
The auto manufacturers must publish an industry-standard interface that spells out in detail what methods can be invoked to make the car move (any car, from any manufacturer). The guidance manufacturers can then write software that invokes the methods described in the interface to command the car. Neither industrial group needs to know how the other group’s software is implemented. In fact, each group considers its software highly proprietary and reserves the right to modify it at any time, as long as it continues to adhere to the published interface.
Interfaces in Java
In the Java programming language, an interface is a reference type, similar to a class, that can contain only constants, method signatures, and nested types. There are no method bodies. Interfaces cannot be instantiated—they can only be implemented by classes or extended by other interfaces. Extension is discussed later in this lesson.
Defining an interface is similar to creating a new class:
Note that the method signatures have no braces and are terminated with a semicolon.public interface OperateCar {// constant declarations, if any
// method signatures
int turn(Direction direction, // An enum with values RIGHT, LEFT
double radius, double startSpeed, double endSpeed);
int changeLanes(Direction direction, double startSpeed, double endSpeed);
int signalTurn(Direction direction, boolean signalOn);
int getRadarFront(double distanceToCar, double speedOfCar);
int getRadarRear(double distanceToCar, double speedOfCar);
……
// more method signatures
}
To use an interface, you write a class that implements the interface. When an instantiable class implements an interface, it provides a method body for each of the methods declared in the interface. For example,
In the robotic car example above, it is the automobile manufacturers who will implement the interface. Chevrolet’s implementation will be substantially different from that of Toyota, of course, but both manufacturers will adhere to the same interface. The guidance manufacturers, who are the clients of the interface, will build systems that use GPS data on a car’s location, digital street maps, and traffic data to drive the car. In so doing, the guidance systems will invoke the interface methods: turn, change lanes, brake, accelerate, and so forth.public class OperateBMW760i implements OperateCar {// the OperateCar method signatures, with implementation —
// for example:
int signalTurn(Direction direction, boolean signalOn) {
//code to turn BMW’s LEFT turn indicator lights on
//code to turn BMW’s LEFT turn indicator lights off
//code to turn BMW’s RIGHT turn indicator lights on
//code to turn BMW’s RIGHT turn indicator lights off
}// other members, as needed — for example, helper classes
// not visible to clients of the interface}
Interfaces as APIs
The robotic car example shows an interface being used as an industry standard Application Programming Interface (API). APIs are also common in commercial software products. Typically, a company sells a software package that contains complex methods that another company wants to use in its own software product. An example would be a package of digital image processing methods that are sold to companies making end-user graphics programs. The image processing company writes its classes to implement an interface, which it makes public to its customers. The graphics company then invokes the image processing methods using the signatures and return types defined in the interface. While the image processing company’s API is made public (to its customers), its implementation of the API is kept as a closely guarded secret—in fact, it may revise the implementation at a later date as long as it continues to implement the original interface that its customers have relied on.
Interfaces and Multiple Inheritance
Interfaces have another very important role in the Java programming language. Interfaces are not part of the class hierarchy, although they work in combination with classes. The Java programming language does not permit multiple inheritance (inheritance is discussed later in this lesson), but interfaces provide an alternative.
In Java, a class can inherit from only one class but it can implement more than one interface. Therefore, objects can have multiple types: the type of their own class and the types of all the interfaces that they implement. This means that if a variable is declared to be the type of an interface, its value can reference any object that is instantiated from any class that implements the interface. This is discussed later in this lesson, in the section titled “Using an Interface as a Type.”