Data Structures and Algorithms in Java
Since 5.0, Java supports enumerated types, called enums. These are types that are
allowed only to take on values that come from a specified set of names. They are
declared inside of a class as follows:
where the modifier can be blank, public, protected, or private. The name of
this enum, name, can be any legal Java identifier. Each of the value identifiers,
value namei, is the name of a possible value that variables of this enum type can
take on. These name values can each also be any legal Java identifier, but the Java
convention is to use capitalized words.
Simple Input Using the java.util.Scanner Class
Just as there is a special object for performing output to the Java console window,
there is also a special object, called System.in, for performing input from the Java
console window. Technically, the input is actually coming from the “standard input”
device, which by default is the computer keyboard echoing its characters in
the Java console. The System.in object is an object associated with the standard
input device. A simple way of reading input with this object is to use it to create a
Scanner object, using the expression
The Scanner class reads the input stream and divides it into tokens, which are contiguous
strings of characters separated by delimiters, which are special separating
characters. The default delimiter is whitespace; that is, tokens are are separated by
strings of spaces, tabs, and newlines, by default. Tokens can either be read immediately
as strings or a Scanner object can converted a token to a base type, if the
token is in the right syntax. Specifically, the Scanner class includes the following
methods for dealing with tokens.
Casting and Autoboxing/Unboxing
Casting is an operation that allows us to change the type of the value returned by
an expression. In its simplest application, we can take a variable of one type and
cast it into an equivalent variable of another type. Casting can be useful for doing
certain numerical and input/output operations.
Casting with Operators
Certain binary operators, like division, will have different results depending on the
variable types they are used with. We must take care to make sure operations perform
their computations on values of the intended type. When used with integers,
division does not keep track of the fractional part, for example.
A generic type is a type that is not defined at compilation time, but becomes fully
specified at run time.
The generics framework allows us define a class in terms of a set of formal type
parameters, with could be used, for example, to abstract the types of some internal
variables of the class. Angle brackets are used to enclose the list of formal type
parameters. Although any valid identifier can be used for a formal type parameter,
single-letter uppercase names are conventionally used. Given a class that has been
defined with such parametrized types, we instantiate an object of this class by using
actual type parameters to indicate the concrete types to be used.
In Code Fragment A.2, we show a class Pair storing key-value pairs, where the
types of the key and value are specified by parameters K and V, respectively. The
main method creates two instances of this class, one for a String-Integer pair (e.g.,
to store a dimension and its value), and the other for a Student-Double pair (e.g.,
to store the grade given to a student).