Sie sind auf Seite 1von 214

1

Learning the Java Language






This trail covers the Iundamentals oI programming in the Java programming
language.
Object-Oriented Programming Concepts teaches you the core concepts behind
object-oriented programming: objects, messages, classes, and inheritance. This lesson
ends by showing you how these concepts translate into code. Feel Iree to skip this
lesson iI you are already Iamiliar with object-oriented programming.
Language Basics describes the traditional Ieatures oI the language, including
variables, arrays, data types, operators, and control Ilow.
Classes and Objects describes how to write the classes Irom which objects are
created, and how to create and use the objects.
Interfaces and Inheritance describes interIaceswhat they are, why you would
want to write one, and how to write one. This section also describes the way in which
you can derive one class Irom another. That is, how a subclass can inherit Iields and
methods Irom asuperclass. You will learn that all classes are derived Irom
the Jbject class, and how to modiIy the methods that a subclass inherits Irom
superclasses.
umbers and Strings This lesson describes how to use :2ber and String objects
The lesson also shows you how to Iormat data Ior output.
enerics are a powerIul Ieature oI the Java programming language. They improve
the type saIety oI your code, making more oI your bugs detectable at compile time.
Packages are a Ieature oI the Java programming language that help you to
organize and structure your classes and their relationships to one another.




Object-Oriented Programming Concepts 5

1. What s an Object? 7
2. What s a Class? 9
3. What s nheritance? 11
4. What s an nterface? 1
5. What s a Package? 13
6. Questions and Exercises 14

Language asics 15

1. Variables 16
2. Primitive Data Types 19
3. Arrays 4
4. Summary of Variables
5. Questions and Exercises
6. Operators 9
7. Assignment, Arithmetic, and Unary Operators 30
8. Equality, Relational, and Conditional Operators 33
9. Bitwise and Bit Shift Operators 33
10. Summary of Operators 36
11. Questions and Exercises 37
12. Expressions, Statements, and Blocks 3
13. Questions and Exercises 41
14. Control Flow Statements 4
15. The if-then and if-then-else Statements 4
16. The switch Statement 43
17. The while and do-while Statements 4
18. The for Statement 49
19. Branching Statements 31
20. Summary of Control Flow Statements 34
21. Questions and Exercises 33

C CIasses and Objects 56

1. Classes 37
2. Declaring Classes 39
3. Declaring Member Variables 60
4. Defining Methods 6
5. Providing Constructors for Your Classes 64
6. Passing nformation to a Method or a Constructor 63
3

7. Objects 69
8. Creating Objects 71
9. Using Objects 73
10. More on Classes 7
11. Returning a Value from a Method 7
12. Using the this Keyword 0
13. Controlling Access to Members of a Class
14. Understanding nstance and Class Members 4
15. nitializing Fields
16. Summary of Creating and Using Classes and Objects 90
17. Questions and Exercises 90
18. Questions and Exercises 9
19. Nested Classes 93
20. nner Class Example 96
21. Summary of Nested Classes 9
22. Questions and Exercises 9
23. Enum Types 99
24. Questions and Exercises 10
25. Annotations 10
26. Questions and Exercises 106


Interfaces and Inheritance 107

1. nterfaces 10
2. Defining an nterface 111
3. mplementing an nterface 111
4. Using an nterface as a Type 113
5. Rewriting nterfaces 114
6. Summary of nterfaces 113
7. Questions and Exercises 113
8. nheritance 116
9. Overriding and Hiding Methods 11
10. Polymorphism 13
11. Hiding Fields 13
12. Using the Keyword super 13
13. Object as a Superclass 17
14. Writing Final Classes and Methods 131
15. Abstract Methods and Classes 13
16. Summary of nheritance 133
17. Questions and Exercises 133
4


Numbers and Strings 137

1. Numbers 13
2. The Numbers Classes 13
3. Formatting Numeric Print Output 141
4. Beyond Basic Arithmetic 146
5. Summary of Numbers 131
6. Questions and Exercises 131
7. Characters 133
8. Strings 133
9. Converting Between Numbers and Strings 139
10. Manipulating Characters in a String 161
11. Comparing Strings and Portions of Strings 166
12. The StringBuilder Class 16
13. Summary of Characters and Strings 173
14. Questions and Exercises 174

Generics 176

1. ntroduction 17
2. Generic Types 10
3. Generic Methods and Constructors 13
4. Type nference 14
5. Bounded Type Parameters 17
6. Subtyping 1
7. Wildcards 190
8. Type Erasure 191
9. Using Non-Reifiable Parameters with Varargs Methods 19
10. Summary of Generics 199
11. Questions and Exercises 199

G Packages 201

1. Creating and Using Packages 0
2. Creating a Package 03
3. Naming a Package 04
4. Using Package Members 03
5. Managing Source and Class Files 09
6. Summary of Creating and Using Packages 1
7. Questions and Exercises 1
3

Object-Oriented Programming Concepts


1. What s an Object?
2. What s a Class?
3. What s nheritance?
4. What s an nterface?
5. What s a Package?
6. Questions and Exercises

















6

Lesson: Object-Oriented Programming Concepts
lf youve never used an ob[ecLorlenLed programmlng language before youll
need Lo learn a few baslc concepLs before you can begln wrlLlng any code 1hls
lesson wlll lnLroduce you Lo ob[ecLs classes lnherlLance lnLerfaces and packages
Lach dlscusslon focuses on how Lhese concepLs relaLe Lo Lhe real world whlle
slmulLaneously provldlng an lnLroducLlon Lo Lhe synLax of Lhe !ava programmlng
language
1 What Is an Object?
n ob[ecL ls a sofLware bundle of relaLed sLaLe and behavlor SofLware ob[ecLs are
ofLen used Lo model Lhe realworld ob[ecLs LhaL you flnd ln everyday llfe 1hls lesson
explalns how sLaLe and behavlor are represenLed wlLhln an ob[ecL lnLroduces Lhe
concepL of daLa encapsulaLlon and explalns Lhe beneflLs of deslgnlng your sofLware
ln Lhls manner
2 What Is a CIass?
class ls a blueprlnL or proLoLype from whlch ob[ecLs are creaLed 1hls secLlon
deflnes a class LhaL models Lhe sLaLe and behavlor of a realworld ob[ecL lL
lnLenLlonally focuses on Lhe baslcs showlng how even a slmple class can cleanly
model sLaLe and behavlor
3 What Is Inheritance?
lnherlLance provldes a powerful and naLural mechanlsm for organlzlng and
sLrucLurlng your sofLware 1hls secLlon explalns how classes lnherlL sLaLe and
behavlor from Lhelr superclasses and explalns how Lo derlve one class from anoLher
uslng Lhe slmple synLax provlded by Lhe !ava programmlng language
What Is an Interface?
n lnLerface ls a conLracL beLween a class and Lhe ouLslde world When a class
lmplemenLs an lnLerface lL promlses Lo provlde Lhe behavlor publlshed by LhaL
lnLerface 1hls secLlon deflnes a slmple lnLerface and explalns Lhe necessary changes
for any class LhaL lmplemenLs lL
5 What Is a Package?
package ls a namespace for organlzlng classes and lnLerfaces ln a loglcal manner
laclng your code lnLo packages makes large sofLware pro[ecLs easler Lo manage
7

1hls secLlon explalns why Lhls ls useful and lnLroduces you Lo Lhe ppllcaLlon
rogrammlng lnLerface (l) provlded by Lhe !ava plaLform
6 Questions and ercises: Object-Oriented Programming Concepts
Dse Lhe quesLlons and exerclses presenLed ln Lhls secLlon Lo LesL your undersLandlng
of ob[ecLs classes lnherlLance lnLerfaces and packages

1 What Is an Object?
Objects are key to understanding 4bfect-4riented technology. Look around right
now and you'll Iind many examples oI real-world objects: your dog, your desk, your
television set, your bicycle.
Real-world objects share two characteristics: They all have state and behavi4r.
Dogs have state (name, color, breed, hungry) and behavior (barking, Ietching,
wagging tail). Bicycles also have state (current gear, current pedal cadence, current
speed) and behavior (changing gear, changing pedal cadence, applying brakes).
IdentiIying the state and behavior Ior real-world objects is a great way to begin
thinking in terms oI object-oriented programming.
Take a minute right now to observe the real-world objects that are in your
immediate area. For each object that you see, ask yourselI two questions: "What
possible states can this object be in?" and "What possible behavior can this object
perIorm?". Make sure to write down your observations. As you do, you'll notice that
real-world objects vary in complexity; your desktop lamp may have only two possible
states (on and oII) and two possible behaviors (turn on, turn oII), but your desktop
radio might have additional states (on, oII, current volume, current station) and
behavior (turn on, turn oII, increase volume, decrease volume, seek, scan, and tune).
You may also notice that some objects, in turn, will also contain other objects. These
real-world observations all translate into the world oI object-oriented programming.



A software object.
$oItware objects are conceptually similar to real-world objects: they too consist oI
state and related behavior. An object stores its state in 1ields (variables in some
programming languages) and exposes its behavior through meth4ds (Iunctions in
some programming languages). Methods operate on an object's internal state and
serve as the primary mechanism Ior object-to-object communication. Hiding internal
state and requiring all interaction to be perIormed through an object's methods is
known as data encapsulati4n a Iundamental principle oI object-oriented
programming.
Consider a bicycle, Ior example:

A bicycle modeled as a software object.
By attributing state (current speed, current pedal cadence, and current gear) and
providing methods Ior changing that state, the object remains in control oI how the
outside world is allowed to use it. For example, iI the bicycle only has 6 gears, a
method to change gears could reject any value that is less than 1 or greater than 6.
9

Bundling code into individual soItware objects provides a number oI beneIits,
including:
1. Modularity: The source code Ior an object can be written and maintained
independently oI the source code Ior other objects. Once created, an object
can be easily passed around inside the system.
2. InIormation-hiding: By interacting only with an object's methods, the details
oI its internal implementation remain hidden Irom the outside world.
3. Code re-use: II an object already exists (perhaps written by another soItware
developer), you can use that object in your program. This allows specialists
to implement/test/debug complex, task-speciIic objects, which you can then
trust to run in your own code.
4. Pluggability and debugging ease: II a particular object turns out to be
problematic, you can simply remove it Irom your application and plug in a
diIIerent object as its replacement. This is analogous to Iixing mechanical
problems in the real world. II a bolt breaks, you replace it, not the entire
machine.
2 What Is a CIass?
In the real world, you'll oIten Iind many individual objects all oI the same
kind. There may be thousands oI other bicycles in existence, all oI the same make
and model. Each bicycle was built Irom the same set oI blueprints and thereIore
contains the same components. In object-oriented terms, we say that your bicycle is
an instance oI the class 41 4bfects known as bicycles. A class is the blueprint Irom
which individual objects are created.
The Iollowing icycle class is one possible implementation oI a bicycle:

1. class icycle
2. ,
3. int cadence = 0;
4. int speed = 0;
5. int gear = 1;
6.
7. void changeCadence(int newVal:e) ,
8. cadence = newVal:e;
9. ,
10.
11. void changeGear(int newVal:e) ,
12. gear = newVal:e;
13. ,
14.
15. void speedUp(int incre2ent) ,
16. speed = speed + incre2ent;
17. ,
18.
10

19. void applyrakes(int decre2ent) ,
20. speed = speed - decre2ent;
21. ,
22.
23. void printStates() ,
24. Syste2.o:t.println("cadence:"+cadence+"
speed:"+speed+" gear:"+gear);
25. ,
26. ,
The syntax oI the Java programming language will look new to you, but the
design oI this class is based on the previous discussion oI bicycle objects. The
Iieldscadence, speed, and gear represent the object's state, and the methods
(changeCadence, changeGear,speedUp etc.) deIine its interaction with the outside
world.
You may have noticed that the icycle class does not contain a 2ain method.
That's because it's not a complete application; it's just the blueprint Ior bicycles
that might be used in an application. The responsibility oI creating and using
new icycle objects belongs to some other class in your application.
Here's a icycleDe2o class that creates two separateicycle objects and
invokes their methods:
1. class icycleDe2o
2. ,
3. p:blic static void 2ain(String, args) ,
4.
5. // Create two different icycle objects
6. icycle bike1 = new icycle();
7. icycle bike2 = new icycle();
8.
9. // Invoke 2ethods on those objects
10. bike1.changeCadence(50);
11. bike1.speedUp(10);
12. bike1.changeGear(2);
13. bike1.printStates();
14.
15. bike2.changeCadence(50);
16. bike2.speedUp(10);
17. bike2.changeGear(2);
18. bike2.changeCadence(40);
19. bike2.speedUp(10);
20. bike2.changeGear(3);
21. bike2.printStates();
22. ,
23. ,

The output oI this test prints the ending pedal cadence, speed, and gear Ior the
two bicycles:
cadence:50 speed:10 gear:2
cadence:40 speed:20 gear:3
11

3 What Is Inheritance?

DiIIerent kinds oI objects oIten have a certain amount in common with each other.
Mountain bikes, road bikes, and tandem bikes, Ior example, all share the
characteristics oI bicycles (current speed, current pedal cadence, current gear). Yet
each also deIines additional Ieatures that make them diIIerent: tandem bicycles have
two seats and two sets oI handlebars; road bikes have drop handlebars; some
mountain bikes have an additional chain ring, giving them a lower gear ratio.
Object-oriented programming allows classes to inheritcommonly used state and
behavior Irom other classes. In this example, icycle now becomes
the superclass oIo:ntainike, Roadike, and Tande2ike. In the Java programming
language, each class is allowed to have one direct superclass, and each superclass has
the potential Ior an unlimited number oI subclasses:

A hierarchy of bicycle classes.
The syntax Ior creating a subclass is simple. At the beginning oI your class
declaration, use the extendskeyword, Iollowed by the name oI the class to inherit
Irom:
1. class o:ntainike extends icycle
2. ,

3. // new fields and 2ethods defining a 2o:ntain bike wo:ld go here

4. ,
1

This gives o:ntainike all the same Iields and methods as icycle, yet allows its
code to Iocus exclusively on the Ieatures that make it unique. This makes code Ior
your subclasses easy to read. However, you must take care to properly document the
state and behavior that each superclass deIines, since that code will not appear in the
source Iile oI each subclass.


What Is an Interface?
As you've already learned, objects deIine their interaction with the outside world
through the methods that they expose. Methods Iorm the object's inter1acewith the
outside world; the buttons on the Iront oI your television set, Ior example, are the
interIace between you and the electrical wiring on the other side oI its plastic casing.
You press the "power" button to turn the television on and oII.
1. In its most common Iorm, an interIace is a group oI related methods with
empty bodies. A bicycle's behavior, iI speciIied as an interIace, might appear
as Iollows:

1. interface icycle ,
2.
3. void changeCadence(int newVal:e); // wheel
revol:tions per 2in:te
4.
5. void changeGear(int newVal:e);
6.
7. void speedUp(int incre2ent);
8.
9. void applyrakes(int decre2ent);
10. ,

To implement this interIace, the name oI your class would change (to a particular
brand oI bicycle, Ior example, such as Cicycle), and you'd use
thei2ple2ents keyword in the class declaration:

1. class Cicycle implements icycle ,
2.
3. // re2ainder of this class i2ple2ented as before
4.
5. ,

Implementing an interIace allows a class to become more Iormal about the
behavior it promises to provide. InterIaces Iorm a contract between the class and the
outside world, and this contract is enIorced at build time by the compiler. II your
13

class claims to implement an interIace, all methods deIined by that interIace must
appear in its source code beIore the class will successIully compile.

ote: To actually compile the Cicycleclass, you'll need to add
the p:blic keyword to the beginning oI the implemented interIace methods.
You'll learn the reasons Ior this later in the lessons on Classes and
Objects andInterIaces and Inheritance.

5 What Is a Package?
A package is a namespace that organizes a set oI related classes and interIaces.
Conceptually you can think oI packages as being similar to diIIerent Iolders on your
computer. You might keep HTML pages in one Iolder, images in another, and scripts
or applications in yet another. Because soItware written in the Java programming
language can be composed oI hundreds orth4usands oI individual classes, it makes
sense to keep things organized by placing related classes and interIaces into
packages.
The Java platIorm provides an enormous class library (a set oI packages) suitable
Ior use in your own applications. This library is known as the "Application
Programming InterIace", or "API" Ior short. Its packages represent the tasks most
commonly associated with general-purpose programming. For example,
a Stringobject contains state and behavior Ior character strings; aFile object allows a
programmer to easily create, delete, inspect, compare, or modiIy a Iile on the
Iilesystem; a Socket object allows Ior the creation and use oI network sockets;
various GUI objects control buttons and checkboxes and anything else related to
graphical user interIaces. There are literally thousands oI classes to choose Irom. This
allows you, the programmer, to Iocus on the design oI your particular application,
rather than the inIrastructure required to make it work.
The Java PlatIorm API $peciIication contains the complete listing Ior all
packages, interIaces, classes, Iields, and methods supplied by the Java PlatIorm 6,
$tandard Edition. Load the page in your browser and bookmark it. As a programmer,
it will become your single most important piece oI reIerence documentation.


14

Questions and ercises: Object-Oriented Programming
Concepts
Questions
1. ealworld ob[ecLs conLaln ___ and ___
sofLware ob[ecLs sLaLe ls sLored ln ___
3 sofLware ob[ecLs behavlor ls exposed Lhrough ___
4 Pldlng lnLernal daLa from Lhe ouLslde world and accesslng lL only Lhrough
publlcly exposed meLhods ls known as daLa ___
3 blueprlnL for a sofLware ob[ecL ls called a ___
6 Common behavlor can be deflned ln a ___ and lnherlLed lnLo a ___ uslng Lhe
___ keyword
7 collecLlon of meLhods wlLh no lmplemenLaLlon ls called an ___
namespace LhaL organlzes classes and lnLerfaces by funcLlonallLy ls called a
___
9 1he Lerm l sLands for ___?
ercises
1. CreaLe new classes for each realworld ob[ecL LhaL you observed aL Lhe
beglnnlng of Lhls Lrall efer Lo Lhe 8lcycle class lf you forgeL Lhe requlred
synLax
or each new class LhaL youve creaLed above creaLe an lnLerface LhaL deflnes
lLs behavlor Lhen requlre your class Lo lmplemenL lL CmlL one or Lwo
meLhods and Lry complllng WhaL does Lhe error look llke?













13

Language asics

1. Variables
2. Primitive Data Types
3. Arrays
4. Summary of Variables
5. Questions and Exercises
6. Operators
7. Assignment, Arithmetic, and Unary Operators
8. Equality, Relational, and Conditional Operators
9. Bitwise and Bit Shift Operators
10. Summary of Operators
11. Questions and Exercises
12. Expressions, Statements, and Blocks
13. Questions and Exercises
14. Control Flow Statements
15. The if-then and if-then-else Statements
16. The switch Statement
17. The while and do-while Statements
18. The for Statement
19. Branching Statements
20. Summary of Control Flow Statements
21. Questions and Exercises









16

Lesson: Language asics
VariabIes
?ouve already learned LhaL ob[ecLs sLore Lhelr sLaLe ln flelds Powever Lhe !ava
programmlng language also uses Lhe Lerm varlable as well 1hls secLlon dlscusses
Lhls relaLlonshlp plus varlable namlng rules and convenLlons baslc daLa Lypes
(prlmlLlve Lypes characLer sLrlngs and arrays) defaulL values and llLerals
Operators
1hls secLlon descrlbes Lhe operaLors of Lhe !ava programmlng language lL presenLs
Lhe mosL commonlyused operaLors flrsL and Lhe less commonlyused operaLors lasL
Lach dlscusslon lncludes code samples LhaL you can complle and run
pressions, Statements, and Iocks
CperaLors may be used ln bulldlng expresslons whlch compuLe values expresslons
are Lhe core componenLs of sLaLemenLs sLaLemenLs may be grouped lnLo blocks
1hls secLlon dlscusses expresslons sLaLemenLs and blocks uslng example code LhaL
youve already seen
ControI Iow Statements
1hls secLlon descrlbes Lhe conLrol flow sLaLemenLs supporLed by Lhe !ava
programmlng language lL covers Lhe declslonsmaklng looplng and branchlng
sLaLemenLs LhaL enable your programs Lo condlLlonally execuLe parLlcular blocks of
code
1 VariabIes
s you learned ln Lhe prevlous lesson an ob[ecL sLores lLs sLaLe ln flelJs
int .,den.e = 0;


int speed = 0;

int ge,7 = 1;
The What Is an Object? discussion introduced you to Iields, but you probably have
still a Iew questions, such as: What are the rules and conventions Ior naming a Iield?
Besides int, what other data types are there? Do Iields have to be initialized when
they are declared? Are Iields assigned a deIault value iI they are not explicitly
initialized? We'll explore the answers to such questions in this lesson, but beIore we
17

do, there are a Iew technical distinctions you must Iirst become aware oI. In the Java
programming language, the terms "Iield" and "variable" are both used; this is a
common source oI conIusion among new developers, since both oIten seem to reIer to
the same thing.
The Java programming language deIines the Iollowing kinds oI variables:
O Instance Var|ab|es (NonStat|c I|e|ds)1echnlcally speaklng ob[ecLs sLore Lhelr
lndlvldual sLaLes ln nonsLaLlc flelds LhaL ls flelds declared wlLhouL
Lhe static keyword nonsLaLlc flelds are also known as lostooce
votlobles because Lhelr values are unlque Lo each lostooce of a class (Lo each
ob[ecL ln oLher words) Lhec:rrentSpeed of one blcycle ls lndependenL from
Lhe c:rrentSpeed of anoLher
O |ass Var|ab|es (Stat|c I|e|ds) closs votloble ls any fleld declared wlLh
Lhe staticmodlfler Lhls Lells Lhe compller LhaL Lhere ls exacLly one copy of Lhls
varlable ln exlsLence regardless of how many Llmes Lhe class has been
lnsLanLlaLed fleld deflnlng Lhe number of gears for a parLlcular klnd of
blcycle could be marked asstatic slnce concepLually Lhe same number of
gears wlll apply Lo all lnsLances 1he code static int n:2Gears = 6; would
creaLe such a sLaLlc fleld ddlLlonally Lhe keyword final could be added Lo
lndlcaLe LhaL Lhe number of gears wlll never change
O oca| Var|ab|es Slmllar Lo how an ob[ecL sLores lLs sLaLe ln flelds a meLhod wlll
ofLen sLore lLs Lemporary sLaLe ln locol votlobles 1he synLax for declarlng a
local varlable ls slmllar Lo declarlng a fleld (for example int co:nt = 0;)
1here ls no speclal keyword deslgnaLlng a varlable as local LhaL deLermlnaLlon
comes enLlrely from Lhe locaLlon ln whlch Lhe varlable ls declared whlch ls
beLween Lhe openlng and closlng braces of a meLhod s such local varlables
are only vlslble Lo Lhe meLhods ln whlch Lhey are declared Lhey are noL
accesslble from Lhe resL of Lhe class
O 9arameters ?ouve already seen examples of parameLers boLh ln
Lhe icycle class and ln Lhe2ain meLhod of Lhe Pello World! appllcaLlon
ecall LhaL Lhe slgnaLure for Lhe 2ain meLhod lsp:blic static void
2ain(String, args) Pere Lhe args varlable ls Lhe parameLer Lo Lhls meLhod
1he lmporLanL Lhlng Lo remember ls LhaL parameLers are always classlfled as
varlables noL flelds 1hls applles Lo oLher parameLeraccepLlng consLrucLs
as well (such as consLrucLors and excepLlon handlers) LhaL youll learn abouL
laLer ln Lhe LuLorlal
1

Pavlng sald LhaL Lhe remalnder of Lhls LuLorlal uses Lhe followlng general guldellnes
when dlscusslng flelds and varlables lf we are Lalklng abouL flelds ln general
(excludlng local varlables and parameLers) we may slmply say flelds lf Lhe
dlscusslon applles Lo all of Lhe above we may slmply say varlables lf Lhe conLexL
calls for a dlsLlncLlon we wlll use speclflc Lerms (sLaLlc fleld local varlables eLc) as
approprlaLe ?ou may also occaslonally see Lhe Lerm member used as well Lypes
flelds meLhods and nesLed Lypes are collecLlvely called lLs ebets
Naming
Every programming language has its own set oI rules and conventions Ior the kinds oI
names that you're allowed to use, and the Java programming language is no diIIerent.
The rules and conventions Ior naming your variables can be summarized as Iollows:
O Iarlable names are casesenslLlve varlables name can be any legal
ldenLlfler an unllmlLedlengLh sequence of Dnlcode leLLers and dlglLs
beglnnlng wlLh a leLLer Lhe dollar slgn or Lhe underscore characLer *
1he convenLlon however ls Lo always begln your varlable names wlLh a leLLer
noL or * ddlLlonally Lhe dollar slgn characLer by convenLlon ls never
used aL all ?ou may flnd some slLuaLlons where auLogeneraLed names wlll
conLaln Lhe dollar slgn buL your varlable names should always avold uslng lL
slmllar convenLlon exlsLs for Lhe underscore characLer whlle lLs Lechnlcally
legal Lo begln your varlables name wlLh * Lhls pracLlce ls dlscouraged
WhlLe space ls noL permlLLed
O SubsequenL characLers may be leLLers dlglLs dollar slgns or underscore
characLers ConvenLlons (and common sense) apply Lo Lhls rule as well When
chooslng a name for your varlables use full words lnsLead of crypLlc
abbrevlaLlons uolng so wlll make your code easler Lo read and undersLand ln
many cases lL wlll also make your code selfdocumenLlng flelds
named cadence speed and gear for example are much more lnLulLlve Lhan
abbrevlaLed verslons such as s c and g lso keep ln mlnd LhaL Lhe name you
choose musL noL be a keyword or reserved word
O lf Lhe name you choose conslsLs of only one word spell LhaL word ln all
lowercase leLLers lf lL conslsLs of more Lhan one word caplLallze Lhe flrsL
leLLer of each subsequenL word 1he namesgearRatio and c:rrentGear are
prlme examples of Lhls convenLlon lf your varlable sLores a consLanL value
such as static final int U*GRS = 6 Lhe convenLlon changes sllghLly
caplLallzlng every leLLer and separaLlng subsequenL words wlLh Lhe underscore
characLer 8y convenLlon Lhe underscore characLer ls never used elsewhere
19

2 Primitive ata Types
The Java programming language is statically-typed, which means that all variables
must Iirst be declared beIore they can be used. This involves stating the variable's type
and name, as you've already seen:
int gear = 1;
Doing so tells your program that a Iield named "gear" exists, holds numerical data,
and has an initial value oI "1". A variable's data type determines the values it may
contain, plus the operations that may be perIormed on it. In addition to int, the Java
programming language supports seven other primitive data types. A primitive type is
predeIined by the language and is named by a reserved keyword. Primitive values do
not share state with other primitive values. The eight primitive data types supported
by the Java programming language are:
O byte: The byte data type is an 8-bit signed two's complement integer. It has a
minimum value oI -128 and a maximum value oI 127 (inclusive). Thebyte data
type can be useIul Ior saving memory in large arrays, where the memory
savings actually matters. They can also be used in place oI intwhere their
limits help to clariIy your code; the Iact that a variable's range is limited can
serve as a Iorm oI documentation.
O short: The short data type is a 16-bit signed two's complement integer. It has a
minimum value oI -32,768 and a maximum value oI 32,767 (inclusive). As
with byte, the same guidelines apply: you can use a short to save memory in
large arrays, in situations where the memory savings actually matters.
O int: The int data type is a 32-bit signed two's complement integer. It has a
minimum value oI -2,147,483,648 and a maximum value oI 2,147,483,647
(inclusive). For integral values, this data type is generally the deIault choice
unless there is a reason (like the above) to choose something else. This data
type will most likely be large enough Ior the numbers your program will use,
but iI you need a wider range oI values, uselong instead.
O long: The long data type is a 64-bit signed two's complement integer. It has a
minimum value oI -9,223,372,036,854,775,808 and a maximum value oI
9,223,372,036,854,775,807 (inclusive). Use this data type when you need a
range oI values wider than those provided by int.
O float: The float data type is a single-precision 32-bit IEEE 754 Iloating point.
Its range oI values is beyond the scope oI this discussion, but is speciIied in
section 4.2.3 oI the Java Language $peciIication. As with the recommendations
Iorbyte and short, use a float (instead oIdo:ble) iI you need to save memory
in large arrays oI Iloating point numbers. This data type should never be used
Ior precise values, such as currency. For that, you will need to use
0

thejava.math.BigDecimal class instead. Numbers and
$trings covers igDeci2al and other useIul classes provided by the Java
platIorm.
O double: The do:ble data type is a double-precision 64-bit IEEE 754 Iloating
point. Its range oI values is beyond the scope oI this discussion, but is speciIied
in section 4.2.3 oI the Java Language $peciIication. For decimal values, this
data type is generally the deIault choice. As mentioned above, this data type
should never be used Ior precise values, such as currency.
O boolean: The boolean data type has only two possible values: tr:e and false.
Use this data type Ior simple Ilags that track true/Ialse conditions. This data
type represents one bit oI inIormation, but its "size" isn't something that's
precisely deIined.
O char: The char data type is a single 16-bit Unicode character. It has a minimum
value oI'\:0000' (or 0) and a maximum value oI'\:ffff' (or 65,535 inclusive).
In addition to the eight primitive data types listed above, the Java programming
language also provides special support Ior character strings via
the java.lang.$tringclass. Enclosing your character string within double quotes will
automatically create a new String object; Ior example, String s = "this is a
string";.String objects are immutable, which means that once created, their values
cannot be changed. The Stringclass is not technically a primitive data type, but
considering the special support given to it by the language, you'll probably tend to
think oI it as such. You'll learn more about the String class in $imple Data Objects
efauIt VaIues
It's not always necessary to assign a value when a Iield is declared. Fields that are
declared but not initialized will be set to a reasonable deIault by the compiler.
Generally speaking, this deIault will be zero or n:ll, depending on the data type.
Relying on such deIault values, however, is generally considered bad programming
style.
The Iollowing chart summarizes the deIault values Ior the above data types.
ata 1ype efau|t Va|ue (for f|e|ds)
byLe 0
shorL 0
lnL 0
1

long 0L
floaL 00f
double 00d
char u0000
SLrlng (or any ob[ecL) null
boolean false
Local variables are slightly diIIerent; the compiler never assigns a deIault value to an
uninitialized local variable. II you cannot initialize your local variable where it is
declared, make sure to assign it a value beIore you attempt to use it. Accessing an
uninitialized local variable will result in a compile-time error.
LiteraIs
You may have noticed that the new keyword isn't used when initializing a variable oI a
primitive type. Primitive types are special data types built into the language; they are
not objects created Irom a class. A literal is the source code representation oI a Iixed
value; literals are represented directly in your code without requiring computation. As
shown below, it's possible to assign a literal to a variable oI a primitive type:
boolean res:lt = tr:e;
char capitalC = 'C';
byte b = 100;
short s = 10000;
int i = 100000;
Integer Literals
An integer literal is oI type long iI it ends with the letterL or l; otherwise it is oI
type int. It is recommended that you use the upper case letter L because the lower
case letter l is hard to distinguish Irom the digit 1.
Values oI the integral types byte, short, int, andlong can be created Irom int literals.
Values oI typelong that exceed the range oI int can be created Iromlong literals.
Integer literals can be expressed these number systems:
O ueclmal 8ase 10 whose dlglLs conslsLs of Lhe numbers 0 Lhrough 0 Lhls ls Lhe
number sysLem you use every day


O Pexadeclmal 8ase 16 whose dlglLs conslsL of Lhe numbers 0 Lhrough 9 and
Lhe leLLers Lhrough
O 8lnary 8ase whose dlglLs conslsLs of Lhe numbers 0 and 1 (you can creaLe
blnary llLerals ln !ava SL 7 and laLer)
For general-purpose programming, the decimal system is likely to be the only number
system you'll ever use. However, iI you need to use another number system, the
Iollowing example shows the correct syntax. The preIix0x indicates hexadecimal
and 0b indicates binary:
int decVal = 26; // The n:2ber 26, in deci2al
int hexVal = 0x1a; // The n:2ber 26, in hexadeci2al
int binVal = 0b11010; // The n:2ber 26, in binary
loating-Point Literals
A Iloating-point literal is oI type float iI it ends with the letter F or f; otherwise its
type is do:ble and it can optionally end with the letter D or d.
The Iloating point types (float and do:ble) can also be expressed using E or e (Ior
scientiIic notation), F or I (32-bit Iloat literal) and D or d (64-bit double literal; this is
the deIault and by convention is omitted).
do:ble d1 = 123.4;
do:ble d2 = 1.234e2; // sa2e val:e as d1, b:t in scientific notation
float f1 = 123.4f;
aracter and String Literals
Literals oI types char and String may contain any Unicode (UTF-16) characters. II
your editor and Iile system allow it, you can use such characters directly in your code.
II not, you can use a "Unicode escape" such as '\:0108' (capital C with circumIlex),
or "S\:00D se\:00F1or" ($J $eor in $panish). Always use 'single quotes'
Ior char literals and "double quotes" Ior Stringliterals. Unicode escape sequences may
be used elsewhere in a program (such as in Iield names, Ior example), not just
in char or String literals.
The Java programming language also supports a Iew special escape sequences
Ior char and String literals:\b (backspace), \t (tab), \n (line Ieed), \f (Iorm
Ieed),\r (carriage return), \" (double quote), \' (single quote), and \\ (backslash).
There's also a special n:ll literal that can be used as a value Ior any reIerence
type. n:ll may be assigned to any variable, except variables oI primitive types.
There's little you can do with a n:ll value beyond testing Ior its presence.
3

ThereIore, n:ll is oIten used in programs as a marker to indicate that some object is
unavailable.
Finally, there's also a special kind oI literal called a class literal, Iormed by taking a
type name and appending ".class"; Ior example, String.class. This reIers to the
object (oI type Class) that represents the type itselI.
&sing &nderscore Characters in Numeric LiteraIs
In Java $E 7 and later, any number oI underscore characters (*) can appear anywhere
between digits in a numerical literal. This Ieature enables you, Ior example. to
separate groups oI digits in numeric literals, which can improve the readability oI your
code.
For instance, iI your code contains numbers with many digits, you can use an
underscore character to separate digits in groups oI three, similar to how you would
use a punctuation mark like a comma, or a space, as a separator.
The Iollowing example shows other ways you can use the underscore in numeric
literals:
long creditCard:2ber = 1234*5678*9012*3456L;
long socialSec:rity:2ber = 999*99*9999L;
float pi = 3.14*15F;
long hexytes = 0xFF*C*D*5;
long hexWords = 0xCF*;
long 2axLong = 0x7fff*ffff*ffff*ffffL;
byte nybbles = 0b0010*0101;
long bytes = 0b11010010*01101001*10010100*10010010;
You can place underscores only between digits; you cannot place underscores in the
Iollowing places:
O L Lhe beglnnlng or end of a number
O d[acenL Lo a declmal polnL ln a floaLlng polnL llLeral
O rlor Lo an F or L sufflx
O ln poslLlons where a sLrlng of dlglLs ls expecLed
The Iollowing examples demonstrate valid and invalid underscore placements (which
are highlighted) in numeric literals:
1lo,t pi1 = 3_.1415F; // Inv,lid; .,nnot put unde7s.o7es ,dj,.ent to ,
de.im,l point
1lo,t pi2 = 3._1415F; // Inv,lid; .,nnot put unde7s.o7es ,dj,.ent to ,
de.im,l point
4

long so.i,lSe.u7ityNumbe71
= 999_99_9999_L; // Inv,lid; .,nnot put unde7s.o7es p7io7 to ,n L
su11ix

int x1 = *52; // This is an identifier, not a n:2eric literal
int x2 = 5*2; // JK (deci2al literal)
int x3 = 52_; // Inv,lid; .,nnot put unde7s.o7es ,t the end o1 ,
lite7,l
int x4 = 5*******2; // JK (deci2al literal)

int x5 = 0_x52; // Inv,lid; .,nnot put unde7s.o7es in the 0x 7,dix
p7e1ix
int x6 = 0x_52; // Inv,lid; .,nnot put unde7s.o7es ,t the
beginning o1 , numbe7
int x7 = 0x5*2; // JK (hexadeci2al literal)
int x8 = 0x52_; // Inv,lid; .,nnot put unde7s.o7es ,t the end o1 ,
numbe7
3 rrays
n ottoy ls a conLalner ob[ecL LhaL holds a flxed number of values of a slngle Lype
1he lengLh of an array ls esLabllshed when Lhe array ls creaLed fLer creaLlon lLs
lengLh ls flxed ?ouve seen an example of arrays already ln Lhe 2ain meLhod of Lhe
Pello World! appllcaLlon 1hls secLlon dlscusses arrays ln greaLer deLall

An array of ten elements
Each item in an array is called an element, and each element is accessed by its
numerical index. As shown in the above illustration, numbering begins with 0. The 9th
element, Ior example, would thereIore be accessed at index 8.
The Iollowing program, rrayDe2o, creates an array oI integers, puts some values in it,
and prints each value to standard output.

class rrayDe2o ,
p:blic static void 2ain(String, args) ,
int, anrray; // declares an array of integers

3

anrray = new int10,; // allocates 2e2ory for 10 integers

anrray0, = 100; // initialize first ele2ent
anrray1, = 200; // initialize second ele2ent
anrray2, = 300; // etc.
anrray3, = 400;
anrray4, = 500;
anrray5, = 600;
anrray6, = 700;
anrray7, = 800;
anrray8, = 900;
anrray9, = 1000;

Syste2.o:t.println("le2ent at index 0: " + anrray0,);
Syste2.o:t.println("le2ent at index 1: " + anrray1,);
Syste2.o:t.println("le2ent at index 2: " + anrray2,);
Syste2.o:t.println("le2ent at index 3: " + anrray3,);
Syste2.o:t.println("le2ent at index 4: " + anrray4,);
Syste2.o:t.println("le2ent at index 5: " + anrray5,);
Syste2.o:t.println("le2ent at index 6: " + anrray6,);
Syste2.o:t.println("le2ent at index 7: " + anrray7,);
Syste2.o:t.println("le2ent at index 8: " + anrray8,);
Syste2.o:t.println("le2ent at index 9: " + anrray9,);
,
,
1he ouLpuL from Lhls program ls
le2ent at index 0: 100
le2ent at index 1: 200
le2ent at index 2: 300
le2ent at index 3: 400
le2ent at index 4: 500
le2ent at index 5: 600
le2ent at index 6: 700
le2ent at index 7: 800
le2ent at index 8: 900
le2ent at index 9: 1000
In a real-world programming situation, you'd probably use one oI the
supported l44ping c4nstructs to iterate through each element oI the array, rather than
write each line individually as shown above. However, this example clearly illustrates
the array syntax. You'll learn about the various looping constructs (for, while, and do-
while) in the Control Flow section.
ecIaring a VariabIe to Refer to an rray
1he above program declares anrray wlLh Lhe followlng llne of code
int, anrray; // declares an array of integers
Like declarations Ior variables oI other types, an array declaration has two
components: the array's type and the array's name. An array's type is written
6

as type,, where type is the data type oI the contained elements; the square brackets
are special symbols indicating that this variable holds an array. The size oI the array is
not part oI its type (which is why the brackets are empty). An array's name can be
anything you want, provided that it Iollows the rules and conventions as previously
discussed in the naming section. As with variables oI other types, the declaration does
not actually create an array it simply tells the compiler that this variable will hold
an array oI the speciIied type.
$imilarly, you can declare arrays oI other types:
byte, anrrayJfytes;
short, anrrayJfShorts;
long, anrrayJfLongs;
float, anrrayJfFloats;
do:ble, anrrayJfDo:bles;
boolean, anrrayJfooleans;
char, anrrayJfChars;
String, anrrayJfStrings;
You can also place the square brackets aIter the array's name:
float anrrayJfFloats,; // this for2 is disco:raged
Powever convenLlon dlscourages Lhls form Lhe brackeLs ldenLlfy Lhe array Lype and
should appear wlLh Lhe Lype deslgnaLlon
Creating, InitiaIizing, and ccessing an rray
Cne way Lo creaLe an array ls wlLh Lhe new operaLor 1he nexL sLaLemenL ln
Lhe rrayDe2o program allocaLes an array wlLh enough memory for Len lnLeger
elemenLs and asslgns Lhe array Lo Lhe anrray varlable
anrray = new int10,; // create an array of integers
lf Lhls sLaLemenL were mlsslng Lhe compller would prlnL an error llke Lhe followlng
and compllaLlon would fall
rrayDe2o.java:4: Variable anrray 2ay not have been initialized.
1he nexL few llnes asslgn values Lo each elemenL of Lhe array
anrray0, = 100; // initialize first ele2ent
anrray1, = 200; // initialize second ele2ent
anrray2, = 300; // etc.
Lach array elemenL ls accessed by lLs numerlcal lndex
Syste2.o:t.println("le2ent 1 at index 0: " + anrray0,);
Syste2.o:t.println("le2ent 2 at index 1: " + anrray1,);
Syste2.o:t.println("le2ent 3 at index 2: " + anrray2,);
lLernaLlvely you can use Lhe shorLcuL synLax Lo creaLe and lnlLlallze an array
7

int, anrray = ,100, 200, 300, 400, 500, 600, 700, 800, 900, 1000,;
Pere Lhe lengLh of Lhe array ls deLermlned by Lhe number of values provlded
beLween and
You can also declare an array oI arrays (also known as amultidimensi4nal array) by
using two or more sets oI square brackets, such as String,, na2es. Each element,
thereIore, must be accessed by a corresponding number oI index values.
In the Java programming language, a multidimensional array is simply an array whose
components are themselves arrays. This is unlike arrays in C or Fortran. A
consequence oI this is that the rows are allowed to vary in length, as shown in the
IollowingMultiDimArrayDemo program:
class :ltiDi2rrayDe2o ,
p:blic static void 2ain(String, args) ,
String,, na2es = ,,"r. ", "rs. ", "s. ",,
,"S2ith", "Jones",,;
Syste2.o:t.println(na2es0,0, + na2es1,0,); //r. S2ith
Syste2.o:t.println(na2es0,2, + na2es1,1,); //s. Jones
,
,
1he ouLpuL from Lhls program ls
r. S2ith
s. Jones
Finally, you can use the built-in length property to determine the size oI any array.
The code
Syste2.o:t.println(anrray.length);
wlll prlnL Lhe arrays slze Lo sLandard ouLpuL
Copying rrays
1he Syste2 class has an arraycopy meLhod LhaL you can use Lo efflclenLly copy daLa
from one array lnLo anoLher
p:blic static void arraycopy(Jbject src,
int srcPos,
Jbject dest,
int destPos,
int length)
1he Lwo Jbject argumenLs speclfy Lhe array Lo copyfto and Lhe array Lo copy to
1he Lhree int argumenLs speclfy Lhe sLarLlng poslLlon ln Lhe source array Lhe sLarLlng
poslLlon ln Lhe desLlnaLlon array and Lhe number of array elemenLs Lo copy


The Iollowing program, rrayCopyDe2o, declares an array oI char elements, spelling
the word "decaIIeinated". It uses arraycopy to copy a subsequence oI array
components into a second array:

class rrayCopyDe2o ,
p:blic static void 2ain(String, args) ,
char, copyFro2 = , 'd', 'e', 'c', 'a', 'f', 'f', 'e',
'i', 'n', 'a', 't', 'e', 'd' ,;
char, copyTo = new char7,;

Syste2.arraycopy(copyFro2, 2, copyTo, 0, 7);
Syste2.o:t.println(new String(copyTo));
,
,
The output Irom this program is:
caffein
Summary of VariabIes
The Java programming language uses both "Iields" and "variables" as part oI its
terminology. Instance variables (non-static Iields) are unique to each instance oI a
class. Class variables (static Iields) are Iields declared with thestatic modiIier; there
is exactly one copy oI a class variable, regardless oI how many times the class has
been instantiated. Local variables store temporary state inside a method. Parameters
are variables that provide extra inIormation to a method; both local variables and
parameters are always classiIied as "variables" (not "Iields"). When naming your
Iields or variables, there are rules and conventions that you should (or must) Iollow.
The eight primitive data types are: byte, short, int, long, Iloat, double, boolean, and
char. Thejava.lang.String class represents character strings. The compiler will assign
a reasonable deIault value Ior Iields oI the above types; Ior local variables, a deIault
value is never assigned. A literal is the source code representation oI a Iixed value. An
array is a container object that holds a Iixed number oI values oI a single type. The
length oI an array is established when the array is created. AIter creation, its length is
Iixed.
5 Questions and ercises: VariabIes
Questions
1. 1he Lerm lnsLance varlable ls anoLher name for ___
1he Lerm class varlable ls anoLher name for ___
9

3 local varlable sLores Lemporary sLaLe lL ls declared lnslde a ___
4 varlable declared wlLhln Lhe openlng and closlng parenLhesls of a meLhod
slgnaLure ls called a ____
3 WhaL are Lhe elghL prlmlLlve daLa Lypes supporLed by Lhe !ava programmlng
language?
6 CharacLer sLrlngs are represenLed by Lhe class ___
7 n ___ ls a conLalner ob[ecL LhaL holds a flxed number of values of a slngle
Lype
ercises
1. CreaLe a small program LhaL deflnes some flelds 1ry creaLlng some lllegal fleld
names and see whaL klnd of error Lhe compller produces Dse Lhe namlng
rules and convenLlons as a gulde
ln Lhe program you creaLed ln Lxerclse 1 Lry leavlng Lhe flelds unlnlLlallzed
and prlnL ouL Lhelr values 1ry Lhe same wlLh a local varlable and see whaL klnd
of compller errors you can produce 8ecomlng famlllar wlLh common compller
errors wlll make lL easler Lo recognlze bugs ln your code
Check your answers
6 Operators
Now that you've learned how to declare and initialize variables, you probably want to
know how to d4 s4mething with them. Learning the operators oI the Java
programming language is a good place to start. Operators are special symbols that
perIorm speciIic operations on one, two, or three 4perands, and then return a result.
As we explore the operators oI the Java programming language, it may be helpIul Ior
you to know ahead oI time which operators have the highest precedence. The
operators in the Iollowing table are listed according to precedence order. The closer to
the top oI the table an operator appears, the higher its precedence. Operators with
higher precedence are evaluated beIore operators with relatively lower precedence.
Operators on the same line have equal precedence. When operators oI equal
precedence appear in the same expression, a rule must govern which is evaluated Iirst.
All binary operators except Ior the assignment operators are evaluated Irom leIt to
right; assignment operators are evaluated right to leIt.
Operator Precedence
Operators Precedence
30

postIix
expr++ expr--
unary
++expr --expr +expr -expr ~ !
multiplicative
/ %
additive
+ -
shiIt
<<
relational
< <= = instanceof
equality
== !=
bitwise AND
&
bitwise exclusive OR
^
bitwise inclusive OR
|
logical AND
&&
logical OR
||
ternary
. :
assignment
= += -= = /= %= &= ^= |= <<= = =
In general-purpose programming, certain operators tend to appear more Irequently
than others; Ior example, the assignment operator "=" is Iar more common than the
unsigned right shiIt operator "". With that in mind, the Iollowing discussion
Iocuses Iirst on the operators that you're most likely to use on a regular basis, and ends
Iocusing on those that are less common. Each discussion is accompanied by sample
code that you can compile and run. $tudying its output will help reinIorce what you've
just learned.
7 ssignment, rithmetic, and &nary Operators
%he Simple Assignment Operator
One oI the most common operators that you'll encounter is the simple assignment
operator "=". You saw this operator in the Bicycle class; it assigns the value on its
right to the operand on its leIt:
int cadence = 0;
int speed = 0;
int gear = 1;
31

This operator can also be used on objects to assign4bfect re1erences, as discussed
in Creating Objects.
%he Arithmetic Operators
The Java programming language provides operators that perIorm addition,
subtraction, multiplication, and division. There's a good chance you'll recognize them
by their counterparts in basic mathematics. The only symbol that might look new to
you is "%", which divides one operand by another and returns the remainder as its
result.
+ additive operator (also :sed for String concatenation)
- s:btraction operator
2:ltiplication operator
/ division operator
% re2ainder operator
The Iollowing program, rith2eticDe2o, tests the arithmetic operators.

class rith2eticDe2o ,

p:blic static void 2ain (String, args),

int res:lt = 1 + 2; // res:lt is now 3
Syste2.o:t.println(res:lt);

res:lt = res:lt - 1; // res:lt is now 2
Syste2.o:t.println(res:lt);

res:lt = res:lt 2; // res:lt is now 4
Syste2.o:t.println(res:lt);

res:lt = res:lt / 2; // res:lt is now 2
Syste2.o:t.println(res:lt);

res:lt = res:lt + 8; // res:lt is now 10
res:lt = res:lt % 7; // res:lt is now 3
Syste2.o:t.println(res:lt);

,
,
You can also combine the arithmetic operators with the simple assignment operator to
create c4mp4und assignments. For example, x+=1; and x=x+1; both increment the
value oI x by 1.
The + operator can also be used Ior concatenating (joining) two strings together, as
shown in the IollowingConcatDe2o program:

class ConcatDe2o ,
p:blic static void 2ain(String, args),
3

String firstString = "This is";
String secondString = " a concatenated string.";
String thirdString = firstString+secondString;
Syste2.o:t.println(thirdString);
,
,
By the end oI this program, the variable thirdStringcontains "This is a concatenated
string.", which gets printed to standard output.
%he Unary Operators
The unary operators require only one operand; they perIorm various operations such
as incrementing/decrementing a value by one, negating an expression, or inverting the
value oI a boolean.
+ Unary pl:s operator; indicates positive val:e (n:2bers are positive
witho:t this, however)
- Unary 2in:s operator; negates an expression
++ Incre2ent operator; incre2ents a val:e by 1
-- Decre2ent operator; decre2ents a val:e by 1
! Logical co2ple2ent operator; inverts the val:e of a boolean
The Iollowing program, UnaryDe2o, tests the unary operators:

class UnaryDe2o ,

p:blic static void 2ain(String, args),
int res:lt = +1; // res:lt is now 1
Syste2.o:t.println(res:lt);
res:lt--; // res:lt is now 0
Syste2.o:t.println(res:lt);
res:lt++; // res:lt is now 1
Syste2.o:t.println(res:lt);
res:lt = -res:lt; // res:lt is now -1
Syste2.o:t.println(res:lt);
boolean s:ccess = false;
Syste2.o:t.println(s:ccess); // false
Syste2.o:t.println(!s:ccess); // tr:e
,
,
The increment/decrement operators can be applied beIore (preIix) or aIter (postIix)
the operand. The coderes:lt++; and ++res:lt; will both end in res:ltbeing
incremented by one. The only diIIerence is that the preIix version (++res:lt)
evaluates to the incremented value, whereas the postIix version (res:lt++) evaluates
to the original value. II you are just perIorming a simple increment/decrement, it
doesn't really matter which version you choose. But iI you use this operator in part oI
a larger expression, the one that you choose may make a signiIicant diIIerence.
33

The Iollowing program, PrePostDe2o, illustrates the preIix/postIix unary increment
operator:

class PrePostDe2o ,
p:blic static void 2ain(String, args),
int i = 3;
i++;
Syste2.o:t.println(i); // "4"
++i;
Syste2.o:t.println(i); // "5"
Syste2.o:t.println(++i); // "6"
Syste2.o:t.println(i++); // "6"
Syste2.o:t.println(i); // "7"
,
,
6uaIity, ReIationaI, and ConditionaI Operators
%he Equality and Relational Operators
The equality and relational operators determine iI one operand is greater than, less
than, equal to, or not equal to another operand. The majority oI these operators will
probably look Iamiliar to you as well. Keep in mind that you must use "==", not "=",
when testing iI two primitive values are equal.
== eq:al to
!= not eq:al to
greater than
= greater than or eq:al to
< less than
<= less than or eq:al to
The Iollowing program, Co2parisonDe2o, tests the comparison operators:

class Co2parisonDe2o ,

p:blic static void 2ain(String, args),
int val:e1 = 1;
int val:e2 = 2;
if(val:e1 == val:e2) Syste2.o:t.println("val:e1 == val:e2");
if(val:e1 != val:e2) Syste2.o:t.println("val:e1 != val:e2");
if(val:e1 val:e2) Syste2.o:t.println("val:e1 val:e2");
if(val:e1 < val:e2) Syste2.o:t.println("val:e1 < val:e2");
if(val:e1 <= val:e2) Syste2.o:t.println("val:e1 <= val:e2");
,
,
Output:
val:e1 != val:e2
val:e1 < val:e2
val:e1 <= val:e2
34

%he Conditional Operators
The && and || operators perIorm 4nditi4nal-ANDand 4nditi4nal-OR operations on
two boolean expressions. These operators exhibit "short-circuiting" behavior, which
means that the second operand is evaluated only iI needed.
&& Conditional-D
|| Conditional-JR
The Iollowing program, ConditionalDe2o1, tests these operators:

class ConditionalDe2o1 ,

p:blic static void 2ain(String, args),
int val:e1 = 1;
int val:e2 = 2;
if((val:e1 == 1) && (val:e2 == 2)) Syste2.o:t.println("val:e1 is 1
D val:e2 is 2");
if((val:e1 == 1) || (val:e2 == 1)) Syste2.o:t.println("val:e1 is 1
JR val:e2 is 1");

,
,
Another conditional operator is .:, which can be thought oI as shorthand Ior an if-
then-else statement (discussed in the Control Flow $tatements section oI this lesson).
This operator is also known as the ternary 4perat4r because it uses three operands. In
the Iollowing example, this operator should be read as: "IIso2eCondition is tr:e,
assign the value oI val:e1 tores:lt. Otherwise, assign the value oI val:e2 tores:lt."
The Iollowing program, ConditionalDe2o2, tests the.: operator:

class ConditionalDe2o2 ,

p:blic static void 2ain(String, args),
int val:e1 = 1;
int val:e2 = 2;
int res:lt;
boolean so2eCondition = tr:e;
res:lt = so2eCondition . val:e1 : val:e2;

Syste2.o:t.println(res:lt);

,
,
Because so2eCondition is true, this program prints "1" to the screen. Use
the .: operator instead oI an if-then-else statement iI it makes your code more
33

readable; Ior example, when the expressions are compact and without side-eIIects
(such as assignments).
%he %ype Comparison Operator instanceof
The instanceof operator compares an object to a speciIied type. You can use it to test
iI an object is an instance oI a class, an instance oI a subclass, or an instance oI a class
that implements a particular interIace.
The Iollowing program, InstanceofDe2o, deIines a parent class (named Parent), a
simple interIace (namedyInterface), and a child class (named Child) that inherits
Irom the parent and implements the interIace.

class InstanceofDe2o ,
p:blic static void 2ain(String, args) ,

Parent obj1 = new Parent();
Parent obj2 = new Child();

Syste2.o:t.println("obj1 instanceof Parent: " + (obj1 instanceof
Parent));
Syste2.o:t.println("obj1 instanceof Child: " + (obj1 instanceof Child));
Syste2.o:t.println("obj1 instanceof yInterface: " + (obj1 instanceof
yInterface));
Syste2.o:t.println("obj2 instanceof Parent: " + (obj2 instanceof
Parent));
Syste2.o:t.println("obj2 instanceof Child: " + (obj2 instanceof Child));
Syste2.o:t.println("obj2 instanceof yInterface: " + (obj2 instanceof
yInterface));
,
,

class Parent,,
class Child extends Parent i2ple2ents yInterface,,
interface yInterface,,
Output:
obj1 instanceof Parent: tr:e
obj1 instanceof Child: false
obj1 instanceof yInterface: false
obj2 instanceof Parent: tr:e
obj2 instanceof Child: tr:e
obj2 instanceof yInterface: tr:e
When using the instanceof operator, keep in mind thatn:ll is not an instance oI
anything.
itwise and it Shift Operators
The Java programming language also provides operators that perIorm bitwise and bit
shiIt operations on integral types. The operators discussed in this section are less
36

commonly used. ThereIore, their coverage is brieI; the intent is to simply make you
aware that these operators exist.
The unary bitwise complement operator "~" inverts a bit pattern; it can be applied to
any oI the integral types, making every "0" a "1" and every "1" a "0". For example,
a byte contains 8 bits; applying this operator to a value whose bit pattern is
"00000000" would change its pattern to "11111111".
The signed leIt shiIt operator "<<" shiIts a bit pattern to the leIt, and the signed right
shiIt operator "" shiIts a bit pattern to the right. The bit pattern is given by the leIt-
hand operand, and the number oI positions to shiIt by the right-hand operand. The
unsigned right shiIt operator "" shiIts a zero into the leItmost position, while the
leItmost position aIter "" depends on sign extension.
The bitwise & operator perIorms a bitwise AND operation.
The bitwise ^ operator perIorms a bitwise exclusive OR operation.
The bitwise | operator perIorms a bitwise inclusive OR operation.
The Iollowing program, itDe2o, uses the bitwise AND operator to print the number
"2" to standard output.

class itDe2o ,
p:blic static void 2ain(String, args) ,
int bit2ask = 0x000F;
int val = 0x2222;
Syste2.o:t.println(val & bit2ask); // prints "2"
,
,
10 Summary of Operators
1he followlng qulck reference summarlzes Lhe operaLors supporLed by Lhe !ava
programmlng language
SimpIe ssignment Operator
= Si2ple assign2ent operator
rithmetic Operators
+ dditive operator (also :sed for String concatenation)
- S:btraction operator
:ltiplication operator
/ Division operator
% Re2ainder operator
37

&nary Operators
+ Unary pl:s operator; indicates positive val:e (n:2bers are positive
witho:t this, however)
- Unary 2in:s operator; negates an expression
++ Incre2ent operator; incre2ents a val:e by 1
-- Decre2ent operator; decre2ents a val:e by 1
! Logical co2ple2ent operator; inverts the val:e of a boolean
6uaIity and ReIationaI Operators
== q:al to
!= ot eq:al to
Greater than
= Greater than or eq:al to
< Less than
<= Less than or eq:al to
ConditionaI Operators
&& Conditional-D
|| Conditional-JR
.: Ternary (shorthand for if-then-else state2ent)
Type Comparison Operator
instanceof Co2pares an object to a specified type
itwise and it Shift Operators
~ Unary bitwise co2ple2ent
<< Signed left shift
Signed right shift
Unsigned right shift
& itwise D
^ itwise excl:sive JR
| itwise incl:sive JR


11 Questions and ercises: Operators
Questions
1. Conslder Lhe followlng code snlppeL
2. arrayJfIntsj, arrayJfIntsj+1,
Whlch operaLors does Lhe code conLaln?
3 Conslder Lhe followlng code snlppeL
4. int i = 10;
5. int n = i++%5;
a WhaL are Lhe values of i and n afLer Lhe code ls execuLed?
b WhaL are Lhe flnal values of i and n lf lnsLead of uslng Lhe posLflx
lncremenL operaLor (i++) you use Lhe preflx verslon (++i))?
6 1o lnverL Lhe value of a boolean whlch operaLor would you use?
3

7 Whlch operaLor ls used Lo compare Lwo values =or == ?
Lxplaln Lhe followlng code sample res:lt = so2eCondition . val:e1 :
val:e2;
ercises
1. Change Lhe followlng program Lo use compound asslgnmenLs
2. class rith2eticDe2o ,
3.
4. p:blic static void 2ain (String, args),
5.
6. int res:lt = 1 + 2; // res:lt is now 3
7. Syste2.o:t.println(res:lt);
8.
9. res:lt = res:lt - 1; // res:lt is now 2
10. Syste2.o:t.println(res:lt);
11.
12. res:lt = res:lt 2; // res:lt is now 4
13. Syste2.o:t.println(res:lt);
14.
15. res:lt = res:lt / 2; // res:lt is now 2
16. Syste2.o:t.println(res:lt);
17.
18. res:lt = res:lt + 8; // res:lt is now 10
19. res:lt = res:lt % 7; // res:lt is now 3
20. Syste2.o:t.println(res:lt);
21.
22. ,
23. ,
24.
3 ln Lhe followlng program explaln why Lhe value 6 ls prlnLed Lwlce ln a row
26. class PrePostDe2o ,
27. p:blic static void 2ain(String, args),
28. int i = 3;
29. i++;
30. Syste2.o:t.println(i); // "4"
31. ++i;
32. Syste2.o:t.println(i); // "5"
33. Syste2.o:t.println(++i); // "6"
34. Syste2.o:t.println(i++); // "6"
35. Syste2.o:t.println(i); // "7"
36. ,
37. ,
Check your answers
12 pressions, Statements, and Iocks
now LhaL you undersLand varlables and operaLors lLs Llme Lo learn
abouL exptessloos stoteeots andblocks CperaLors may be used ln bulldlng
39

expresslons whlch compuLe values expresslons are Lhe core componenLs of
sLaLemenLs sLaLemenLs may be grouped lnLo blocks
pressions
n exptessloo ls a consLrucL made up of varlables operaLors and meLhod
lnvocaLlons whlch are consLrucLed accordlng Lo Lhe synLax of Lhe language LhaL
evaluaLes Lo a slngle value ?ouve already seen examples of expresslons lllusLraLed
ln bold below
int .,den.e = 0;
,nA77,y[0] = 100;
Syste2.o:t.println("Element 1 ,t index 0: " + ,nA77,y[0]);

int 7esult = 1 + 2; // res:lt is now 3
if(v,lue1 == v,lue2) Syste2.o:t.println("v,lue1 == v,lue2");
1he daLa Lype of Lhe value reLurned by an expresslon depends on Lhe elemenLs used
ln Lhe expresslon 1he expresslon cadence = 0 reLurns an int because Lhe asslgnmenL
operaLor reLurns a value of Lhe same daLa Lype as lLs lefLhand operand ln Lhls
case cadence ls anint s you can see from Lhe oLher expresslons an expresslon can
reLurn oLher Lypes of values as well such as boolean or String
The Java programming language allows you to construct compound expressions Irom
various smaller expressions as long as the data type required by one part oI the
expression matches the data type oI the other. Here's an example oI a compound
expression:

1 2 3
In this particular example, the order in which the expression is evaluated is
unimportant because the result oI multiplication is independent oI order; the outcome
is always the same, no matter in which order you apply the multiplications. However,
this is not true oI all expressions. For example, the Iollowing expression gives
diIIerent results, depending on whether you perIorm the addition or the division
operation Iirst:
x + y / 100 // a2big:o:s
You can speciIy exactly how an expression will be evaluated using balanced
parenthesis: ( and ). For example, to make the previous expression unambiguous, you
could write the Iollowing:

40

(x + y) / 100 // :na2big:o:s, reco22ended
II you don't explicitly indicate the order Ior the operations to be perIormed, the order
is determined by the precedence assigned to the operators in use within the
expression. Operators that have a higher precedence get evaluated Iirst. For example,
the division operator has a higher precedence than does the addition operator.
ThereIore, the Iollowing two statements are equivalent:
x + y / 100


x + (y / 100) // :na2big:o:s, reco22ended
When writing compound expressions, be explicit and indicate with parentheses which
operators should be evaluated Iirst. This practice makes code easier to read and to
maintain.
Statements
SLaLemenLs are roughly equlvalenL Lo senLences ln naLural languages
stoteeot forms a compleLe unlL of execuLlon 1he followlng Lypes of expresslons
can be made lnLo a sLaLemenL by LermlnaLlng Lhe expresslon wlLh a semlcolon (;)
O sslgnmenL expresslons
O ny use of ++ or --
O ,eLhod lnvocaLlons
O Cb[ecL creaLlon expresslons
Such sLaLemenLs are called exptessloo stoteeots Pere are some examples of
expresslon sLaLemenLs
aVal:e = 8933.234; // assign2ent state2ent
aVal:e++; // incre2ent state2ent
Syste2.o:t.println("Hello World!"); // 2ethod invocation state2ent
icycle 2yike = new icycle(); // object creation state2ent
ln addlLlon Lo expresslon sLaLemenLs Lhere are Lwo oLher klnds of
sLaLemenLs Jeclototloo stoteeots andcoottol flow stoteeots Jeclototloo
stoteeotdeclares a varlable ?ouve seen many examples of declaraLlon sLaLemenLs
already
do:ble aVal:e = 8933.234; //declaration state2ent
lnally coottol flow stoteeots regulaLe Lhe order ln whlch sLaLemenLs geL execuLed
?oull learn abouL conLrol flow sLaLemenLs ln Lhe nexL secLlon ConLrol low
SLaLemenLs
41

Iocks
block ls a group of zero or more sLaLemenLs beLween balanced braces and can be
used anywhere a slngle sLaLemenL ls allowed 1he followlng examplelockDe2o
lllusLraLes Lhe use of blocks
class lockDe2o ,
p:blic static void 2ain(String, args) ,
boolean condition = tr:e;
if (condition) , // begin blo. 1
Syste2.o:t.println("Condition is tr:e.");
, // end blo. one
else , // begin blo. 2
Syste2.o:t.println("Condition is false.");
, // end blo. 2
,
,
13 Questions and ercises: pressions,
Statements, and Iocks
Questions
1. CperaLors may be used ln bulldlng ___ whlch compuLe values
Lxpresslons are Lhe core componenLs of ___
3 SLaLemenLs may be grouped lnLo ___
4 1he followlng code snlppeL ls an example of a ___ expresslon
5. 1 2 3
6 SLaLemenLs are roughly equlvalenL Lo senLences ln naLural languages buL
lnsLead of endlng wlLh a perlod a sLaLemenL ends wlLh a ___
7 block ls a group of zero or more sLaLemenLs beLween balanced ___ and can
be used anywhere a slngle sLaLemenL ls allowed
ercises
ldenLlfy Lhe followlng klnds of expresslon sLaLemenLs
O aVal:e = 8933.234;
O aVal:e++;
O Syste2.o:t.println("Hello World!");
O icycle 2yike = new icycle();
Check your answers

4

1 ControI Iow Statements
The statements inside your source Iiles are generally executed Irom top to bottom, in
the order that they appear. 4ntr4l 1l4 statements, however, break up the Ilow oI
execution by employing decision making, looping, and branching, enabling your
program toc4nditi4nally execute particular blocks oI code. This section describes the
decision-making statements (if-then, if-then-else, switch), the looping statements
(for, while, do-while), and the branching statements (break, contin:e, ret:rn)
supported by the Java programming language.
15 The if-then and if-then-eIse Statements
The i1then Statement
1he if-then sLaLemenL ls Lhe mosL baslc of all Lhe conLrol flow sLaLemenLs lL Lells
your program Lo execuLe a cerLaln secLlon of code ooly lf a parLlcular LesL evaluaLes
Lo tr:e or example Lhe icycle class could allow Lhe brakes Lo decrease Lhe
blcycles speedooly lf Lhe blcycle ls already ln moLlon Cne posslble lmplemenLaLlon
of Lhe applyrakes meLhod could be as follows
void applyrakes(),
if (isoving), // the "if" cla:se: bicycle 2:st be 2oving
c:rrentSpeed--; // the "then" cla:se: decrease c:rrent speed
,
,
lf Lhls LesL evaluaLes Lo false (meanlng LhaL Lhe blcycle ls noL ln moLlon) conLrol
[umps Lo Lhe end of Lhe if-thensLaLemenL
In addition, the opening and closing braces are optional, provided that the "then"
clause contains only one statement:
void applyrakes(),
if (isoving) c:rrentSpeed--; // sa2e as above, b:t witho:t braces
,
uecldlng when Lo omlL Lhe braces ls a maLLer of personal LasLe CmlLLlng Lhem can
make Lhe code more brlLLle lf a second sLaLemenL ls laLer added Lo Lhe Lhen
clause a common mlsLake would be forgeLLlng Lo add Lhe newly requlred braces
1he compller cannoL caLch Lhls sorL of error youll [usL geL Lhe wrong resulLs
The i1thenelse Statement
1he if-then-else sLaLemenL provldes a secondary paLh of execuLlon when an lf
clause evaluaLes Lo false ?ou could use an if-then-else sLaLemenL ln
43

Lheapplyrakes meLhod Lo Lake some acLlon lf Lhe brakes are applled when Lhe
blcycle ls noL ln moLlon ln Lhls case Lhe acLlon ls Lo slmply prlnL an error message
sLaLlng LhaL Lhe blcycle has already sLopped
void applyrakes(),
if (isoving) ,
c:rrentSpeed--;
, else ,
Syste2.err.println("The bicycle has already stopped!");
,
,
The Iollowing program, IflseDe2o, assigns a grade based on the value oI a test score:
an A Ior a score oI 90 or above, a B Ior a score oI 80 or above, and so on.

class IflseDe2o ,
p:blic static void 2ain(String, args) ,

int testscore = 76;
char grade;

if (testscore = 90) ,
grade = '';
, else if (testscore = 80) ,
grade = '';
, else if (testscore = 70) ,
grade = 'C';
, else if (testscore = 60) ,
grade = 'D';
, else ,
grade = 'F';
,
Syste2.o:t.println("Grade = " + grade);
,
,
1he ouLpuL from Lhe program ls
Grade = C
?ou may have noLlced LhaL Lhe value of testscore can saLlsfy more Lhan one
expresslon ln Lhe compound sLaLemenL 76 = 70 and 76 = 60 Powever once a
condlLlon ls saLlsfled Lhe approprlaLe sLaLemenLs are execuLed (grade = 'C';) and
Lhe remalnlng condlLlons are noL evaluaLed
16 The switch Statement
Unlike if-then and if-then-else statements, theswitch statement can have a number
oI possible execution paths. A switch works with the byte,short, char,
44

and int primitive data types. It also works with enumerated types (discussed in Enum
Types), the String class, and a Iew special classes that wrap certain primitive
types: Character, yte, Short, and Integer (discussed in Numbers and $trings).
The Iollowing code example, SwitchDe2o, declares anint named 2onth whose value
represents a month. The code displays the name oI the month, based on the value
oI 2onth, using the switch statement.

p:blic class SwitchDe2o ,
p:blic static void 2ain(String, args) ,

int 2onth = 8;
String 2onthString;
switch (2onth) ,
case 1: 2onthString = "Jan:ary"; break;
case 2: 2onthString = "Febr:ary"; break;
case 3: 2onthString = "arch"; break;
case 4: 2onthString = "pril"; break;
case 5: 2onthString = "ay"; break;
case 6: 2onthString = "J:ne"; break;
case 7: 2onthString = "J:ly"; break;
case 8: 2onthString = ":g:st"; break;
case 9: 2onthString = "Septe2ber"; break;
case 10: 2onthString = "Jctober"; break;
case 11: 2onthString = "ove2ber"; break;
case 12: 2onthString = "Dece2ber"; break;
defa:lt: 2onthString = "Invalid 2onth"; break;
,
Syste2.o:t.println(2onthString);
,
,
In this case, :g:st is printed to standard output.
The body oI a switch statement is known as a sitch bl4ck. A statement in
the switch block can be labeled with one or more case or defa:lt labels.
The switchstatement evaluates its expression, then executes all statements that Iollow
the matching case label.
You could also display the name oI the month with if-then-else statements:
int 2onth = 8;
if (2onth == 1) ,
Syste2.o:t.println("Jan:ary");
, else if (2onth == 2) ,
Syste2.o:t.println("Febr:ary");
,
. . . // and so on
43

Deciding whether to use if-then-else statements or aswitch statement is based on
readability and the expression that the statement is testing. An if-then-else statement
can test expressions based on ranges oI values or conditions, whereas
a switch statement tests expressions based only on a single integer, enumerated value,
or String object.
Another point oI interest is the break statement. Eachbreak statement terminates the
enclosing switchstatement. Control Ilow continues with the Iirst statement Iollowing
the switch block. The break statements are necessary because without them,
statements in switchblocks 1all thr4ugh: All statements aIter the matchingcase label
are executed in sequence, regardless oI the expression oI subsequent case labels, until
a breakstatement is encountered. The programSwitchDe2oFallThro:gh shows
statements in aswitch block that Iall through. The program displays the month
corresponding to the integer 2onth and the months that Iollow in the year:

p:blic class SwitchDe2oFallThro:gh ,

p:blic static void 2ain(String args,) ,
java.:til.rrayList<String f:t:reonths = new
java.:til.rrayList<String();

int 2onth = 8;

switch (2onth) ,
case 1: f:t:reonths.add("Jan:ary");
case 2: f:t:reonths.add("Febr:ary");
case 3: f:t:reonths.add("arch");
case 4: f:t:reonths.add("pril");
case 5: f:t:reonths.add("ay");
case 6: f:t:reonths.add("J:ne");
case 7: f:t:reonths.add("J:ly");
case 8: f:t:reonths.add(":g:st");
case 9: f:t:reonths.add("Septe2ber");
case 10: f:t:reonths.add("Jctober");
case 11: f:t:reonths.add("ove2ber");
case 12: f:t:reonths.add("Dece2ber"); break;
defa:lt: break;
,

if (f:t:reonths.is2pty()) ,
Syste2.o:t.println("Invalid 2onth n:2ber");
, else ,
for (String 2ontha2e : f:t:reonths) ,
Syste2.o:t.println(2ontha2e);
,
,
,
,
This is the output Irom the code:
46

:g:st
Septe2ber
Jctober
ove2ber
Dece2ber
Technically, the Iinal break is not required because Ilow Ialls out oI
the switch statement. Using a break is recommended so that modiIying the code is
easier and less error prone. The defa:lt section handles all values that are not
explicitly handled by one oI the casesections.
The Iollowing code example, SwitchDe2o2, shows how a statement can have
multiple case labels. The code example calculates the number oI days in a particular
month:

class SwitchDe2o2 ,
p:blic static void 2ain(String, args) ,

int 2onth = 2;
int year = 2000;
int n:2Days = 0;

switch (2onth) ,
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
n:2Days = 31;
break;
case 4:
case 6:
case 9:
case 11:
n:2Days = 30;
break;
case 2:
if ( ((year % 4 == 0) && !(year % 100 == 0))
|| (year % 400 == 0) )
n:2Days = 29;
else
n:2Days = 28;
break;
defa:lt:
Syste2.o:t.println("Invalid 2onth.");
break;
,
Syste2.o:t.println(":2ber of Days = " + n:2Days);
,
,
47

This is the output Irom the code:
:2ber of Days = 29
&sing Strings in switch Statements
In Java $E 7 and later, you can use a String object in the switch statement's
expression. The Iollowing code example, StringSwitchDe2o, displays the number oI
the month based on the value oI the String named2onth:

p:blic class StringSwitchDe2o ,

p:blic static int getonth:2ber(String 2onth) ,

int 2onth:2ber = 0;

if (2onth == n:ll) , ret:rn 2onth:2ber; ,

switch (2onth.toLowerCase()) ,
case "jan:ary": 2onth:2ber = 1; break;
case "febr:ary": 2onth:2ber = 2; break;
case "2arch": 2onth:2ber = 3; break;
case "april": 2onth:2ber = 4; break;
case "2ay": 2onth:2ber = 5; break;
case "j:ne": 2onth:2ber = 6; break;
case "j:ly": 2onth:2ber = 7; break;
case "a:g:st": 2onth:2ber = 8; break;
case "septe2ber": 2onth:2ber = 9; break;
case "october": 2onth:2ber = 10; break;
case "nove2ber": 2onth:2ber = 11; break;
case "dece2ber": 2onth:2ber = 12; break;
defa:lt: 2onth:2ber = 0; break;
,

ret:rn 2onth:2ber;
,

p:blic static void 2ain(String, args) ,

String 2onth = ":g:st";

int ret:rnedonth:2ber =
StringSwitchDe2o.getonth:2ber(2onth);

if (ret:rnedonth:2ber == 0) ,
Syste2.o:t.println("Invalid 2onth");
, else ,
Syste2.o:t.println(ret:rnedonth:2ber);
,
,
,
4

The output Irom this code is 8.
The String in the switch expression is compared with the expressions associated with
each case label as iI theString.eq:als method were being used. In order Ior
the StringSwitchDe2o example to accept any month regardless oI case, 2onth is
converted to lowercase (with the toLowerCase method), and all the strings associated
with the case labels are in lowercase.
ote: This example checks iI the expression in theswitch statement is n:ll. Ensure
that the expression in any switch statement is not null to prevent
a:llPointerxception Irom being thrown.
17 The whiIe and do-whiIe Statements
The while statement continually executes a block oI statements while a particular
condition is tr:e. Its syntax can be expressed as:
while (expression) ,
state2ent(s)
,
The while statement evaluates expressi4n, which must return a boolean value. II the
expression evaluates totr:e, the while statement executes the statement(s) in
the while block. The while statement continues testing the expression and executing
its block until the expression evaluates to false. Using the whilestatement to print the
values Irom 1 through 10 can be accomplished as in the Iollowing WhileDe2o program:

class WhileDe2o ,
p:blic static void 2ain(String, args),
int co:nt = 1;
while (co:nt < 11) ,
Syste2.o:t.println("Co:nt is: " + co:nt);
co:nt++;
,
,
,
You can implement an inIinite loop using the whilestatement as Iollows:
while (tr:e),
// yo:r code goes here
,
The Java programming language also provides a do-while statement, which can be
expressed as Iollows:
do ,
state2ent(s)
, while (expression);
49

The diIIerence between do-while and while is thatdo-while evaluates its expression at
the bottom oI the loop instead oI the top. ThereIore, the statements within the do block
are always executed at least once, as shown in the Iollowing DoWhileDe2o program:

class DoWhileDe2o ,
p:blic static void 2ain(String, args),
int co:nt = 1;
do ,
Syste2.o:t.println("Co:nt is: " + co:nt);
co:nt++;
, while (co:nt <= 11);
,
,
1 The for Statement
The for statement provides a compact way to iterate over a range oI values.
Programmers oIten reIer to it as the "Ior loop" because oI the way in which it
repeatedly loops until a particular condition is satisIied. The general Iorm oI
the for statement can be expressed as Iollows:
for (initialization; termination; increment) ,
statement(s)
,
When using this version oI the for statement, keep in mind that:
O The initiali:ati4n expression initializes the loop; it's executed once, as the
loop begins.
O When the terminati4n expression evaluates tofalse, the loop terminates.
O The increment expression is invoked aIter each iteration through the loop; it
is perIectly acceptable Ior this expression to increment 4r decrement a value.
The Iollowing program, ForDe2o, uses the general Iorm oI the for statement to print
the numbers 1 through 10 to standard output:

class ForDe2o ,
p:blic static void 2ain(String, args),
for(int i=1; i<11; i++),
Syste2.o:t.println("Co:nt is: " + i);
,
,
,
The output oI this program is:
Co:nt is: 1
Co:nt is: 2
Co:nt is: 3
Co:nt is: 4
Co:nt is: 5
Co:nt is: 6
30

Co:nt is: 7
Co:nt is: 8
Co:nt is: 9
Co:nt is: 10
Notice how the code declares a variable within the initialization expression. The scope
oI this variable extends Irom its declaration to the end oI the block governed by
the for statement, so it can be used in the termination and increment expressions as
well. II the variable that controls a for statement is not needed outside oI the loop, it's
best to declare the variable in the initialization expression. The names i, j, and k are
oIten used to control for loops; declaring them within the initialization expression
limits their liIe span and reduces errors.
The three expressions oI the for loop are optional; an inIinite loop can be created as
Iollows:
for ( ; ; ) , // infinite loop

// yo:r code goes here
,
The for statement also has another Iorm designed Ior iteration
through Collections and arrays This Iorm is sometimes reIerred to as the enhanced
14r statement, and can be used to make your loops more compact and easy to read. To
demonstrate, consider the Iollowing array, which holds the numbers 1 through 10:
int, n:2bers = ,1,2,3,4,5,6,7,8,9,10,;
The Iollowing program, nhancedForDe2o, uses the enhanced for to loop through the
array:

class nhancedForDe2o ,
p:blic static void 2ain(String, args),
int, n:2bers = ,1,2,3,4,5,6,7,8,9,10,;
for (int ite2 : n:2bers) ,
Syste2.o:t.println("Co:nt is: " + ite2);
,
,
,
In this example, the variable ite2 holds the current value Irom the numbers array. The
output Irom this program is the same as beIore:
Co:nt is: 1
Co:nt is: 2
Co:nt is: 3
Co:nt is: 4
Co:nt is: 5
Co:nt is: 6
Co:nt is: 7
Co:nt is: 8
31

Co:nt is: 9
Co:nt is: 10
We recommend using this Iorm oI the for statement instead oI the general Iorm
whenever possible.
1 ranching Statements
The b7e, Statement
1he break sLaLemenL has Lwo forms labeled and unlabeled ?ou saw Lhe unlabeled
form ln Lhe prevlous dlscusslon of Lhe switch sLaLemenL ?ou can also use an
unlabeled break Lo LermlnaLe a for while or do-while loop as shown ln Lhe
followlng reakDe2oprogram
class reakDe2o ,
p:blic static void 2ain(String, args) ,

int, arrayJfInts = , 32, 87, 3, 589, 12, 1076,
2000, 8, 622, 127 ,;
int searchfor = 12;

int i;
boolean fo:ndIt = false;

for (i = 0; i < arrayJfInts.length; i++) ,
if (arrayJfIntsi, == searchfor) ,
fo:ndIt = tr:e;
b7e,;
,
,

if (fo:ndIt) ,
Syste2.o:t.println("Fo:nd " + searchfor
+ " at index " + i);
, else ,
Syste2.o:t.println(searchfor
+ " not in the array");
,
,
,
1hls program searches for Lhe number 1 ln an array 1he break sLaLemenL shown ln
boldface LermlnaLes Lhefor loop when LhaL value ls found ConLrol flow Lhen
Lransfers Lo Lhe prlnL sLaLemenL aL Lhe end of Lhe program 1hls programs ouLpuL ls
Fo:nd 12 at index 4
n unlabeled break sLaLemenL LermlnaLes Lhe lnnermosLswitch for while or do-
while sLaLemenL buL a labeled break LermlnaLes an ouLer sLaLemenL 1he followlng
program reakWithLabelDe2o ls slmllar Lo Lhe prevlous program buL uses
3

nesLed for loops Lo search for a value ln a Lwodlmenslonal array When Lhe value ls
found a labeled break LermlnaLes Lhe ouLer forloop (labeled search)

class reakWithLabelDe2o ,
p:blic static void 2ain(String, args) ,

int,, arrayJfInts = , , 32, 87, 3, 589 ,,
, 12, 1076, 2000, 8 ,,
, 622, 127, 77, 955 ,
,;
int searchfor = 12;

int i;
int j = 0;
boolean fo:ndIt = false;

search:
for (i = 0; i < arrayJfInts.length; i++) ,
for (j = 0; j < arrayJfIntsi,.length; j++) ,
if (arrayJfIntsi,j, == searchfor) ,
fo:ndIt = tr:e;
break search;
,
,
,

if (fo:ndIt) ,
Syste2.o:t.println("Fo:nd " + searchfor +
" at " + i + ", " + j);
, else ,
Syste2.o:t.println(searchfor
+ " not in the array");
,
,
,
This is the output oI the program.
Fo:nd 12 at 1, 0
1he break sLaLemenL LermlnaLes Lhe labeled sLaLemenL lL does noL Lransfer Lhe flow
of conLrol Lo Lhe label ConLrol flow ls Lransferred Lo Lhe sLaLemenL lmmedlaLely
followlng Lhe labeled (LermlnaLed) sLaLemenL
The .ontinue Statement
1he contin:e sLaLemenL sklps Lhe currenL lLeraLlon of afor while or do-while loop
1he unlabeled form sklps Lo Lhe end of Lhe lnnermosL loops body and evaluaLes
Lhe boolean expresslon LhaL conLrols Lhe loop 1he followlng program Contin:eDe2o
sLeps Lhrough a String counLlng Lhe occurences of Lhe leLLer p lf Lhe currenL
33

characLer ls noL a p Lhe contin:e sLaLemenL sklps Lhe resL of Lhe loop and proceeds
Lo Lhe nexL characLer lf lL ls a p Lhe program lncremenLs Lhe leLLer counL

class Contin:eDe2o ,
p:blic static void 2ain(String, args) ,

String searche = "peter piper picked a peck of pickled peppers";
int 2ax = searche.length();
int n:2Ps = 0;

for (int i = 0; i < 2ax; i++) ,
//interested only in p's
if (searche.chart(i) != 'p')
contin:e;

//process p's
n:2Ps++;
,
Syste2.o:t.println("Fo:nd " + n:2Ps + " p's in the string.");
,
,
Pere ls Lhe ouLpuL of Lhls program
Fo:nd 9 p's in the string.
1o see Lhls effecL more clearly Lry removlng Lhecontin:e sLaLemenL and recomplllng
When you run Lhe program agaln Lhe counL wlll be wrong saylng LhaL lL found 33 ps
lnsLead of 9
A labeled contin:e statement skips the current iteration oI an outer loop marked with
the given label. The Iollowing example program, Contin:eWithLabelDe2o, uses nested
loops to search Ior a substring within another string. Two nested loops are required:
one to iterate over the substring and one to iterate over the string being searched. The
Iollowing program,Contin:eWithLabelDe2o , uses the labeled Iorm oI continue to skip
an iteration in the outer loop.

class Contin:eWithLabelDe2o ,
p:blic static void 2ain(String, args) ,

String searche = "Look for a s:bstring in 2e";
String s:bstring = "s:b";
boolean fo:ndIt = false;

int 2ax = searche.length() - s:bstring.length();

test:
for (int i = 0; i <= 2ax; i++) ,
int n = s:bstring.length();
int j = i;
34

int k = 0;
while (n-- != 0) ,
if (searche.chart(j++)
!= s:bstring.chart(k++)) ,
contin:e test;
,
,
fo:ndIt = tr:e;
break test;
,
Syste2.o:t.println(fo:ndIt . "Fo:nd it" :
"Didn't find it");
,
,
Here is the output Irom this program.
Fo:nd it
The 7etu7n Statement
1he lasL of Lhe branchlng sLaLemenLs ls Lhe ret:rnsLaLemenL 1he ret:rn sLaLemenL
exlLs from Lhe currenL meLhod and conLrol flow reLurns Lo where Lhe meLhod was
lnvoked 1he ret:rn sLaLemenL has Lwo forms one LhaL reLurns a value and one LhaL
doesnL 1o reLurn a value slmply puL Lhe value (or an expresslon LhaL calculaLes Lhe
value) afLer Lhe ret:rn keyword
ret:rn ++co:nt;
1he daLa Lype of Lhe reLurned value musL maLch Lhe Lype of Lhe meLhods declared
reLurn value When a meLhod ls declared void use Lhe form of ret:rn LhaL doesnL
reLurn a value
ret:rn;
1he Classes and Cb[ecLs lesson wlll cover everyLhlng you need Lo know abouL wrlLlng
meLhods
20 Summary of ControI Iow Statements
The if-then statement is the most basic oI all the control Ilow statements. It tells your
program to execute a certain section oI code 4nly i1 a particular test evaluates to tr:e.
The if-then-else statement provides a secondary path oI execution when an "iI"
clause evaluates to false. Unlike if-then and if-then-else, the switch statement
allows Ior any number oI possible execution paths. The while and do-while statements
continually execute a block oI statements while a particular condition is tr:e. The
diIIerence between do-while and while is that do-while evaluates its expression at the
bottom oI the loop instead oI the top. ThereIore, the statements within thedo block are
33

always executed at least once. The forstatement provides a compact way to iterate
over a range oI values. It has two Iorms, one oI which was designed Ior looping
through collections and arrays.
21 Questions and ercises: ControI Iow
Statements
Questions
1. 1he mosL baslc conLrol flow sLaLemenL supporLed by Lhe !ava programmlng
language ls Lhe ___ sLaLemenL
1he ___ sLaLemenL allows for any number of posslble execuLlon paLhs
3 1he ___ sLaLemenL ls slmllar Lo Lhe whilesLaLemenL buL evaluaLes lLs
expresslon aL Lhe ___ of Lhe loop
4 Pow do you wrlLe an lnflnlLe loop uslng Lhe forsLaLemenL?
3 Pow do you wrlLe an lnflnlLe loop uslng Lhe whilesLaLemenL?
ercises
1. Consider the Iollowing code snippet.
2. if (a:2ber = 0)
3. if (a:2ber == 0) Syste2.o:t.println("first string");
4. else Syste2.o:t.println("second string");
5. Syste2.o:t.println("third string");
a WhaL ouLpuL do you Lhlnk Lhe code wlll produce lf a:2ber ls 3?
b WrlLe a LesL program conLalnlng Lhe prevlous code snlppeL
make a:2ber 3 WhaL ls Lhe ouLpuL of Lhe program? ls lL whaL you
predlcLed? Lxplaln why Lhe ouLpuL ls whaL lL ls ln oLher words whaL ls
Lhe conLrol flow for Lhe code snlppeL?
c Dslng only spaces and llne breaks reformaL Lhe code snlppeL Lo make
Lhe conLrol flow easler Lo undersLand
d Dse braces and Lo furLher clarlfy Lhe code
Check your answers







36

C CIasses and Objects

1. Classes
2. Declaring Classes
3. Declaring Member Variables
4. Defining Methods
5. Providing Constructors for Your Classes
6. Passing nformation to a Method or a Constructor
7. Objects
8. Creating Objects
9. Using Objects
10. More on Classes
11. Returning a Value from a Method
12. Using the this Keyword
13. Controlling Access to Members of a Class
14. Understanding nstance and Class Members
15. nitializing Fields
16. Summary of Creating and Using Classes and Objects
17. Questions and Exercises
18. Questions and Exercises
19. Nested Classes
20. nner Class Example
21. Summary of Nested Classes
22. Questions and Exercises
23. Enum Types
24. Questions and Exercises
25. Annotations
26. Questions and Exercises


Lesson: CIasses and Objects
WlLh Lhe knowledge you now have of Lhe baslcs of Lhe !ava programmlng language
you can learn Lo wrlLe your own classes ln Lhls lesson you wlll flnd lnformaLlon
abouL deflnlng your own classes lncludlng declarlng member varlables meLhods
and consLrucLors
You will learn to use your classes to create objects, and how to use the objects you
create.
37

This lesson also covers nesting classes within other classes, enumerations, and
annotations.
CIasses
1hls secLlon shows you Lhe anaLomy of a class and how Lo declare flelds meLhods
and consLrucLors
Objects
1hls secLlon covers creaLlng and uslng ob[ecLs ?ou wlll learn how Lo lnsLanLlaLe an
ob[ecL and once lnsLanLlaLed how Lo use Lhe dot operaLor Lo access Lhe ob[ecLs
lnsLance varlables and meLhods
ore on CIasses
1hls secLlon covers more aspecLs of classes LhaL depend on uslng ob[ecL references
and Lhe dot operaLor LhaL you learned abouL ln Lhe precedlng secLlon reLurnlng
values from meLhods Lhe this keyword class vs lnsLance members and access
conLrol
Nested CIasses
SLaLlc nesLed classes lnner classes anonymous lnner classes and local classes are
covered
num Types
1hls secLlon covers enumeraLlons speclallzed classes LhaL allow you Lo deflne and
use seLs of consLanLs
nnotations
nnoLaLlons allow you Lo add lnformaLlon Lo your program LhaL ls noL acLually parL of
Lhe program 1hls secLlon descrlbes Lhree bullLln annoLaLlons LhaL you should know
abouL
1 CIasses
The introduction to object-oriented concepts in the lesson titled Object-oriented
Programming Conceptsused a bicycle class as an example, with racing bikes,
mountain bikes, and tandem bikes as subclasses. Here is sample code Ior a possible
implementation oI a icycleclass, to give you an overview oI a class declaration.
$ubsequent sections oI this lesson will back up and explain class declarations step by
step. For the moment, don't concern yourselI with the details.
3

p:blic class icycle ,

// the Bi.y.le .l,ss h,s th7ee fields
p:blic int cadence;
p:blic int gear;
p:blic int speed;

// the Bi.y.le .l,ss h,s one .43stru.t4r
p:blic icycle(int startCadence, int startSpeed, int startGear) ,
gear = startGear;
cadence = startCadence;
speed = startSpeed;
,

// the Bi.y.le .l,ss h,s 1ou7 2eth4ds
p:blic void setCadence(int newVal:e) ,
cadence = newVal:e;
,

p:blic void setGear(int newVal:e) ,
gear = newVal:e;
,

p:blic void applyrake(int decre2ent) ,
speed -= decre2ent;
,

p:blic void speedUp(int incre2ent) ,
speed += incre2ent;
,

,
A class declaration Ior a o:ntainike class that is a subclass oI icycle might look
like this:
p:blic class o:ntainike extends icycle ,

// the Mount,inBie sub.l,ss h,s one field
p:blic int seatHeight;

// the Mount,inBie sub.l,ss h,s one .43stru.t4r
p:blic o:ntainike(int startHeight, int startCadence, int startSpeed,
int startGear) ,
s:per(startCadence, startSpeed, startGear);
seatHeight = startHeight;
,

// the Mount,inBie sub.l,ss h,s one 2eth4d
p:blic void setHeight(int newVal:e) ,
seatHeight = newVal:e;
,

,
39

o:ntainike inherits all the Iields and methods oIicycle and adds the
Iield seatHeight and a method to set it (mountain bikes have seats that can be moved
up and down as the terrain demands).
2 ecIaring CIasses
You've seen classes deIined in the Iollowing way:
class yClass ,
//field, constr:ctor, and 2ethod declarations
,
This is a class declarati4n. The class b4dy (the area between the braces) contains all
the code that provides Ior the liIe cycle oI the objects created Irom the class:
constructors Ior initializing new objects, declarations Ior the Iields that provide the
state oI the class and its objects, and methods to implement the behavior oI the class
and its objects.
The preceding class declaration is a minimal oneit contains only those components
oI a class declaration that are required. You can provide more inIormation about the
class, such as the name oI its superclass, whether it implements any interIaces, and so
on, at the start oI the class declaration. For example,
class yClass extends ySuperClass implements YourInterface ,
//field, constr:ctor, and 2ethod declarations
,
means that yClass is a subclass oI yS:perClassand that it implements
the Yo:rInterface interIace.
You can also add modiIiers like public or private at the very beginningso you can
see that the opening line oI a class declaration can become quite complicated. The
modiIiers public and private, which determine what other classes can access yClass,
are discussed later in this lesson. The lesson on interIaces and inheritance will explain
how and why you would use the extends andimplements keywords in a class
declaration. For the moment you do not need to worry about these extra
complications.
In general, class declarations can include these components, in order:
1. ModiIiers such as public, private, and a number oI others that you will
encounter later.
2. The class name, with the initial letter capitalized by convention.
3. The name oI the class's parent (superclass), iI any, preceded by the
keyword extends. A class can only extend (subclass) one parent.
60

4. A comma-separated list oI interIaces implemented by the class, iI any,
preceded by the keywordimplements. A class can implement more than one
interIace.
5. The class body, surrounded by braces, }.
3 ecIaring ember VariabIes
1here are several klnds of varlables
O ,ember varlables ln a classLhese are calledflelJs
O Iarlables ln a meLhod or block of codeLhese are called locol votlobles
O Iarlables ln meLhod declaraLlonsLhese are called potoetets
1he icycle class uses Lhe followlng llnes of code Lo deflne lLs flelds
p:blic int cadence;
p:blic int gear;
p:blic int speed;
leld declaraLlons are composed of Lhree componenLs ln order
1 ero or more modlflers such as p:blic orprivate
1he flelds Lype
3 1he flelds name
1he flelds of icycle are named cadence gear andspeed and are all of daLa Lype
lnLeger (int) 1hep:blic keyword ldenLlfles Lhese flelds as publlc members
accesslble by any ob[ecL LhaL can access Lhe class
ccess odifiers
1he flrsL (lefLmosL) modlfler used leLs you conLrol whaL oLher classes have access Lo
a member fleld or Lhe momenL conslder only p:blic and private CLher access
modlflers wlll be dlscussed laLer
O p:blic modlflerLhe fleld ls accesslble from all classes
O private modlflerLhe fleld ls accesslble only wlLhln lLs own class
In the spirit oI encapsulation, it is common to make Iields private. This means that
they can only be directlyaccessed Irom the Bicycle class. We still need access to these
values, however. This can be done indirectly by adding public methods that obtain the
Iield values Ior us:
p:blic class icycle ,
61


private int cadence;
private int gear;
private int speed;

p:blic icycle(int startCadence, int startSpeed, int startGear) ,
gear = startGear;
cadence = startCadence;
speed = startSpeed;
,

p:blic int getCadence() ,
ret:rn cadence;
,

p:blic void setCadence(int newVal:e) ,
cadence = newVal:e;
,

p:blic int getGear() ,
ret:rn gear;
,

p:blic void setGear(int newVal:e) ,
gear = newVal:e;
,

p:blic int getSpeed() ,
ret:rn speed;
,

p:blic void applyrake(int decre2ent) ,
speed -= decre2ent;
,

p:blic void speedUp(int incre2ent) ,
speed += incre2ent;
,

,
Types
ll varlables musL have a Lype ?ou can use prlmlLlve Lypes such
as int float boolean eLc Cr you can use reference Lypes such as sLrlngs arrays or
ob[ecLs
VariabIe Names
ll varlables wheLher Lhey are flelds local varlables or parameLers follow Lhe same
namlng rules and convenLlons LhaL were covered ln Lhe Language 8aslcs
lesson Iarlablesnamlng
6

In this lesson, be aware that the same naming rules and conventions are used Ior
method and class names, except that
O Lhe flrsL leLLer of a class name should be caplLallzed and
O Lhe flrsL (or only) word ln a meLhod name should be a verb

efining ethods
Pere ls an example of a Lyplcal meLhod declaraLlon
p:blic do:ble calc:latenswer(do:ble wingSpan, int n:2berJfngines, do:ble
length, do:ble grossTons) ,
//do the calc:lation here
,
1he only requlred elemenLs of a meLhod declaraLlon are Lhe meLhods reLurn Lype
name a palr of parenLheses() and a body beLween braces ,,
More generally, method declarations have six components, in order:
1 ,odlflerssuch as p:blic private and oLhers you wlll learn abouL laLer
1he reLurn LypeLhe daLa Lype of Lhe value reLurned by Lhe meLhod or void lf
Lhe meLhod does noL reLurn a value
3 1he meLhod nameLhe rules for fleld names apply Lo meLhod names as well
buL Lhe convenLlon ls a llLLle dlfferenL
4 1he parameLer llsL ln parenLheslsa commadellmlLed llsL of lnpuL
parameLers preceded by Lhelr daLa Lypes enclosed by parenLheses () lf
Lhere are no parameLers you musL use empLy parenLheses
3 n excepLlon llsLLo be dlscussed laLer
6 1he meLhod body enclosed beLween bracesLhe meLhods code lncludlng
Lhe declaraLlon of local varlables goes here
,odlflers reLurn Lypes and parameLers wlll be dlscussed laLer ln Lhls lesson
LxcepLlons are dlscussed ln a laLer lesson

ef|n|t|on 1wo of Lhe componenLs of a meLhod declaraLlon comprlse Lhe etboJ
slqoototeLhe meLhods name and Lhe parameLer Lypes

63

1he slgnaLure of Lhe meLhod declared above ls
calc:latenswer(do:ble, int, do:ble, do:ble)
Naming a ethod
lLhough a meLhod name can be any legal ldenLlfler code convenLlons resLrlcL
meLhod names 8y convenLlon meLhod names should be a verb ln lowercase or a
mulLlword name LhaL beglns wlLh a verb ln lowercase followed by ad[ecLlves
nouns eLc ln mulLlword names Lhe flrsL leLLer of each of Lhe second and followlng
words should be caplLallzed Pere are some examples
r:n
r:nFast
getackgro:nd
getFinalData
co2pareTo
setX
is2pty
1yplcally a meLhod has a unlque name wlLhln lLs class Powever a meLhod mlghL
have Lhe same name as oLher meLhods due Lo etboJ ovetlooJloq
OverIoading ethods
The Java programming language supports 4verl4adingmethods, and Java can
distinguish between methods with diIIerent meth4d signatures. This means that
methods within a class can have the same name iI they have diIIerent parameter lists
(there are some qualiIications to this that will be discussed in the lesson titled
"InterIaces and Inheritance").
$uppose that you have a class that can use calligraphy to draw various types oI data
(strings, integers, and so on) and that contains a method Ior drawing each data type. It
is cumbersome to use a new name Ior each methodIor
example, drawString, drawInteger, drawFloat, and so on. In the Java programming
language, you can use the same name Ior all the drawing methods but pass a diIIerent
argument list to each method. Thus, the data drawing class might declare Iour
methods named draw, each oI which has a diIIerent parameter list.
p:blic class Datartist ,
...
p:blic void draw(String s) ,
...
,
p:blic void draw(int i) ,
...
,
p:blic void draw(do:ble f) ,
64

...
,
p:blic void draw(int i, do:ble f) ,
...
,
,
Cverloaded meLhods are dlfferenLlaLed by Lhe number and Lhe Lype of Lhe
argumenLs passed lnLo Lhe meLhod ln Lhe code sample draw(String
s) and draw(int i)are dlsLlncL and unlque meLhods because Lhey requlre dlfferenL
argumenL Lypes
You cannot declare more than one method with the same name and the same number
and type oI arguments, because the compiler cannot tell them apart.
The compiler does not consider return type when diIIerentiating methods, so you
cannot declare two methods with the same signature even iI they have a diIIerent
return type.

Note Cverloaded meLhods should be used sparlngly as Lhey can make code much
less readable
5 Providing Constructors for Your CIasses
A class contains constructors that are invoked to create objects Irom the class
blueprint. Constructor declarations look like method declarationsexcept that they
use the name oI the class and have no return type. For example,icycle has one
constructor:
p:blic icycle(int startCadence, int startSpeed, int startGear) ,
gear = startGear;
cadence = startCadence;
speed = startSpeed;
,
To create a new icycle object called 2yike, a constructor is called by
the new operator:
icycle 2yike = new icycle(30, 0, 8);
new icycle(30, 0, 8) creates space in memory Ior the object and initializes its
Iields.
Although icycle only has one constructor, it could have others, including a no-
argument constructor:
p:blic icycle() ,
63

gear = 1;
cadence = 10;
speed = 0;
,
icycle yo:rike = new icycle(); invokes the no-argument constructor to create a
new icycleobject called yo:rike.
Both constructors could have been declared in icyclebecause they have diIIerent
argument lists. As with methods, the Java platIorm diIIerentiates constructors on the
basis oI the number oI arguments in the list and their types. You cannot write two
constructors that have the same number and type oI arguments Ior the same class,
because the platIorm would not be able to tell them apart. Doing so causes a compile-
time error.
You don't have to provide any constructors Ior your class, but you must be careIul
when doing this. The compiler automatically provides a no-argument, deIault
constructor Ior any class without constructors. This deIault constructor will call the
no-argument constructor oI the superclass. In this situation, the compiler will
complain iI the superclass doesn't have a no-argument constructor so you must veriIy
that it does. II your class has no explicit superclass, then it has an implicit superclass
oI Jbject, which d4es have a no-argument constructor.
You can use a superclass constructor yourselI. Theo:ntainike class at the beginning
oI this lesson did just that. This will be discussed later, in the lesson on interIaces and
inheritance.
You can use access modiIiers in a constructor's declaration to control which other
classes can call the constructor.

ote : II another class cannot call a yClassconstructor, it cannot directly
create yClassobjects.
6 Passing Information to a ethod or a Constructor
1he declaraLlon for a meLhod or a consLrucLor declares Lhe number and Lhe Lype of
Lhe argumenLs for LhaL meLhod or consLrucLor or example Lhe followlng ls a
meLhod LhaL compuLes Lhe monLhly paymenLs for a home loan based on Lhe amounL
of Lhe loan Lhe lnLeresL raLe Lhe lengLh of Lhe loan (Lhe number of perlods) and Lhe
fuLure value of Lhe loan
66

p:blic do:ble co2p:tePay2ent(do:ble lo,nAmt,
do:ble 7,te,
do:ble 1utu7eV,lue,
int numPe7iods) ,
do:ble interest = 7,te / 100.0;
do:ble partial1 = ath.pow((1 + interest), -numPe7iods);
do:ble deno2inator = (1 - partial1) / interest;
do:ble answer = (-lo,nAmt / deno2inator)
- ((1utu7eV,lue partial1) / deno2inator);
ret:rn answer;
,
1hls meLhod has four parameLers Lhe loan amounL Lhe lnLeresL raLe Lhe fuLure
value and Lhe number of perlods 1he flrsL Lhree are doublepreclslon floaLlng polnL
numbers and Lhe fourLh ls an lnLeger 1he parameLers are used ln Lhe meLhod body
and aL runLlme wlll Lake on Lhe values of Lhe argumenLs LhaL are passed ln

Note 9otoetets refers Lo Lhe llsL of varlables ln a meLhod
declaraLlon Atqoeotsare Lhe acLual values LhaL are passed ln when Lhe meLhod ls
lnvoked When you lnvoke a meLhod Lhe argumenLs used musL maLch Lhe
declaraLlons parameLers ln Lype and order

Parameter Types
?ou can use any daLa Lype for a parameLer of a meLhod or a consLrucLor 1hls
lncludes prlmlLlve daLa Lypes such as doubles floaLs and lnLegers as you saw ln
Lheco2p:tePay2ent meLhod and reference daLa Lypes such as ob[ecLs and arrays
Here's an example oI a method that accepts an array as an argument. In this example,
the method creates a newPolygon object and initializes it Irom an array oI Pointobjects
(assume that Point is a class that represents an x, y coordinate):
p:blic Polygon polygonFro2(Point, corners) ,
// 2ethod body goes here
,

Note 1he !ava programmlng language doesnL leL you pass meLhods lnLo meLhods
8uL you can pass an ob[ecL lnLo a meLhod and Lhen lnvoke Lhe ob[ecLs meLhods

67

rbitrary Number of rguments
?ou can use a consLrucL called vototqs Lo pass an arblLrary number of values Lo a
meLhod ?ou use varargs when you donL know how many of a parLlcular Lype of
argumenL wlll be passed Lo Lhe meLhod lLs a shorLcuL Lo creaLlng an array manually
(Lhe prevlous meLhod could have used varargs raLher Lhan an array)
To use varargs, you Iollow the type oI the last parameter by an ellipsis (three dots, ...),
then a space, and the parameter name. The method can then be called with any
number oI that parameter, including none.
p:blic Polygon polygonFro2(Point... corners) ,
int n:2berJfSides = corners.length;
do:ble sq:areJfSide1, lengthJfSide1;
sq:areJfSide1 = (corners1,.x - corners0,.x)(corners1,.x -
corners0,.x)
+ (corners1,.y - corners0,.y)(corners1,.y -
corners0,.y) ;
lengthJfSide1 = ath.sqrt(sq:areJfSide1);
// 2ore 2ethod body code follows that creates
// and ret:rns a polygon connecting the Points
,
?ou can see LhaL lnslde Lhe meLhod corners ls LreaLed llke an array 1he meLhod can
be called elLher wlLh an array or wlLh a sequence of argumenLs 1he code ln Lhe
meLhod body wlll LreaL Lhe parameLer as an array ln elLher case
You will most commonly see varargs with the printing methods; Ior example,
this printf method:
p:blic PrintStrea2 printf(String for2at, Jbject... args)
allows you Lo prlnL an arblLrary number of ob[ecLs lL can be called llke Lhls
Syste2.o:t.printf("%s: %d, %s%n", na2e, idn:2, address);
or llke Lhls
Syste2.o:t.printf("%s: %d, %s, %s, %s%n", na2e, idn:2, address, phone,
e2ail);
or wlLh yeL a dlfferenL number of argumenLs
Parameter Names
When you declare a parameLer Lo a meLhod or a consLrucLor you provlde a name for
LhaL parameLer 1hls name ls used wlLhln Lhe meLhod body Lo refer Lo Lhe passedln
argumenL
6

The name oI a parameter must be unique in its scope. It cannot be the same as the
name oI another parameter Ior the same method or constructor, and it cannot be the
name oI a local variable within the method or constructor.
A parameter can have the same name as one oI the class's Iields. II this is the case, the
parameter is said toshad4 the Iield. $hadowing Iields can make your code diIIicult to
read and is conventionally used only within constructors and methods that set a
particular Iield. For example, consider the Iollowing Circle class and
itssetJrigin method:
p:blic class Circle ,
private int x, y, radi:s;
p:blic void setJrigin(int x, int y) ,
...
,
,
1he Circle class has Lhree flelds x y and radi:s 1he setJrigin meLhod has Lwo
parameLers each of whlch has Lhe same name as one of Lhe flelds Lach meLhod
parameLer shadows Lhe fleld LhaL shares lLs name So uslng Lhe slmple
names x or y wlLhln Lhe body of Lhe meLhod refers Lo Lhe parameLer oot Lo Lhe fleld
1o access Lhe fleld you musL use a quallfled name 1hls wlll be dlscussed laLer ln Lhls
lesson ln Lhe secLlon LlLled Dslng Lhe this keyword
Passing Primitive ata Type rguments
rlmlLlve argumenLs such as an int or a do:ble are passed lnLo meLhods by voloe
1hls means LhaL any changes Lo Lhe values of Lhe parameLers exlsL only wlLhln Lhe
scope of Lhe meLhod When Lhe meLhod reLurns Lhe parameLers are gone and any
changes Lo Lhem are losL Pere ls an example
p:blic class PassPri2itiveyVal:e ,

p:blic static void 2ain(String, args) ,

int x = 3;

//invoke passethod() with x as arg:2ent
passethod(x);

// print x to see if its val:e has changed
Syste2.o:t.println("fter invoking passethod, x = " + x);

,

// change para2eter in passethod()
p:blic static void passethod(int p) ,
p = 10;
69

,
,
When you run Lhls program Lhe ouLpuL ls
fter invoking passethod, x = 3
Passing Reference ata Type rguments
eference daLa Lype parameLers such as ob[ecLs are also passed lnLo meLhods by
voloe 1hls means LhaL when Lhe meLhod reLurns Lhe passedln reference sLlll
references Lhe same ob[ecL as before nowevet Lhe values of Lhe ob[ecLs
flelds coo be changed ln Lhe meLhod lf Lhey have Lhe proper access level
For example, consider a method in an arbitrary class that moves Circle objects:
p:blic void 2oveCircle(Circle circle, int deltaX, int deltaY) ,
// code to 2ove origin of circle to x+deltaX, y+deltaY
circle.setX(circle.getX() + deltaX);
circle.setY(circle.getY() + deltaY);

//code to assign a new reference to circle
circle = new Circle(0, 0);
,
LeL Lhe meLhod be lnvoked wlLh Lhese argumenLs
2oveCircle(2yCircle, 23, 56)
lnslde Lhe meLhod circle lnlLlally refers Lo 2yCircle 1he meLhod changes Lhe x and
y coordlnaLes of Lhe ob[ecL LhaL circle references (le 2yCircle) by 3 and 36
respecLlvely 1hese changes wlll perslsL when Lhe meLhod reLurns 1hen circle ls
asslgned a reference Lo a new Circle ob[ecL wlLh x = y = 0 1hls reasslgnmenL has
no permanence however because Lhe reference was passed ln by value and cannoL
change WlLhln Lhe meLhod Lhe ob[ecL polnLed Lo by circle has changed buL when
Lhe meLhod reLurns 2yCircle sLlll references Lhe same Circle ob[ecL as before Lhe
meLhod was called
7 Objects
A typical Java program creates many objects, which as you know, interact by
invoking methods. Through these object interactions, a program can carry out various
tasks, such as implementing a GUI, running an animation, or sending and receiving
inIormation over a network. Once an object has completed the work Ior which it was
created, its resources are recycled Ior use by other objects.
70

Here's a small program, called CreateJbjectDe2o, that creates three objects:
one Point object and twoRectangle objects. You will need all three source Iiles to
compile this program.

p:blic class CreateJbjectDe2o ,

p:blic static void 2ain(String, args) ,

//Declare and create a point object
//and two rectangle objects.
Point originJne = new Point(23, 94);
Rectangle rectJne = new Rectangle(originJne, 100, 200);
Rectangle rectTwo = new Rectangle(50, 100);

//display rectJne's width, height, and area
Syste2.o:t.println("Width of rectJne: " +
rectJne.width);
Syste2.o:t.println("Height of rectJne: " +
rectJne.height);
Syste2.o:t.println("rea of rectJne: " + rectJne.getrea());

//set rectTwo's position
rectTwo.origin = originJne;

//display rectTwo's position
Syste2.o:t.println("X Position of rectTwo: "
+ rectTwo.origin.x);
Syste2.o:t.println("Y Position of rectTwo: "
+ rectTwo.origin.y);

//2ove rectTwo and display its new position
rectTwo.2ove(40, 72);
Syste2.o:t.println("X Position of rectTwo: "
+ rectTwo.origin.x);
Syste2.o:t.println("Y Position of rectTwo: "
+ rectTwo.origin.y);
,
,
This program creates, manipulates, and displays inIormation about various objects.
Here's the output:
Width of rectJne: 100
Height of rectJne: 200
rea of rectJne: 20000
X Position of rectTwo: 23
Y Position of rectTwo: 94
X Position of rectTwo: 40
Y Position of rectTwo: 72
The Iollowing three sections use the above example to describe the liIe cycle oI an
object within a program. From them, you will learn how to write code that creates and
uses objects in your own programs. You will also learn how the system cleans up aIter
an object when its liIe has ended.
71

Creating Objects
s you know a class provldes Lhe blueprlnL for ob[ecLs you creaLe an ob[ecL from a
class Lach of Lhe followlng sLaLemenLs Laken from Lhe CreateJbjectDe2oprogram
creaLes an ob[ecL and asslgns lL Lo a varlable
Point o7iginOne = new Point(23, 94);
#e.t,ngle 7e.tOne = new Rectangle(originJne, 100, 200);
#e.t,ngle 7e.tTwo = new Rectangle(50, 100);
1he flrsL llne creaLes an ob[ecL of Lhe Point class and Lhe second and Lhlrd llnes each
creaLe an ob[ecL of LheRectangle class
Each oI these statements has three parts (discussed in detail below):
1 ec|arat|on 1he code seL ln bo|d are all varlable declaraLlons LhaL assoclaLe a
varlable name wlLh an ob[ecL Lype
Instant|at|on 1he new keyword ls a !ava operaLor LhaL creaLes Lhe ob[ecL
3 In|t|a||zat|on 1he new operaLor ls followed by a call Lo a consLrucLor whlch
lnlLlallzes Lhe new ob[ecL
ecIaring a VariabIe to Refer to an Object
revlously you learned LhaL Lo declare a varlable you wrlLe
type name;
1hls noLlfles Lhe compller LhaL you wlll use ooe Lo refer Lo daLa whose Lype ls type
WlLh a prlmlLlve varlable Lhls declaraLlon also reserves Lhe proper amounL of
memory for Lhe varlable
You can also declare a reIerence variable on its own line. For example:
Point originJne;
lf you declare originJne llke Lhls lLs value wlll be undeLermlned unLll an ob[ecL ls
acLually creaLed and asslgned Lo lL Slmply declarlng a reference varlable does noL
creaLe an ob[ecL or LhaL you need Lo use Lhe newoperaLor as descrlbed ln Lhe nexL
secLlon ?ou musL asslgn an ob[ecL Lo originJne before you use lL ln your code
CLherwlse you wlll geL a compller error
A variable in this state, which currently reIerences no object, can be illustrated as
Iollows (the variable name,originJne, plus a reIerence pointing to nothing):
7


Instantiating a CIass
1he new operaLor lnsLanLlaLes a class by allocaLlng memory for a new ob[ecL and
reLurnlng a reference Lo LhaL memory 1he new operaLor also lnvokes Lhe ob[ecL
consLrucLor

Note 1he phrase lnsLanLlaLlng a class means Lhe same Lhlng as creaLlng an
ob[ecL When you creaLe an ob[ecL you are creaLlng an lnsLance of a class
Lherefore lnsLanLlaLlng a class

The new operator requires a single, postIix argument: a call to a constructor. The name
oI the constructor provides the name oI the class to instantiate.
The new operator returns a reIerence to the object it created. This reIerence is usually
assigned to a variable oI the appropriate type, like:
Point originJne = new Point(23, 94);
1he reference reLurned by Lhe new operaLor does noL have Lo be asslgned Lo a
varlable lL can also be used dlrecLly ln an expresslon or example
int height = new Rectangle().height;
1hls sLaLemenL wlll be dlscussed ln Lhe nexL secLlon
InitiaIizing an Object
Peres Lhe code for Lhe Point class
p:blic class Point ,
p:blic int x = 0;
p:blic int y = 0;
//.onst7u.to7
publi. Point(int , int b) {
x = ,;
y = b;
}
,
1hls class conLalns a slngle consLrucLor ?ou can recognlze a consLrucLor because lLs
declaraLlon uses Lhe same name as Lhe class and lL has no reLurn Lype 1he
consLrucLor ln Lhe Point class Lakes Lwo lnLeger argumenLs as declared by Lhe
73

code (int a, int b) 1he followlng sLaLemenL provldes 3 and 94 as values for Lhose
argumenLs
Point originJne = new Point(23, 94);
1he resulL of execuLlng Lhls sLaLemenL can be lllusLraLed ln Lhe nexL flgure

Peres Lhe code for Lhe Rectangle class whlch conLalns four consLrucLors
p:blic class Rectangle ,
p:blic int width = 0;
p:blic int height = 0;
p:blic Point origin;

// fo:r constr:ctors
p:blic Rectangle() ,
origin = new Point(0, 0);
,
p:blic Rectangle(Point p) ,
origin = p;
,
p:blic Rectangle(int w, int h) ,
origin = new Point(0, 0);
width = w;
height = h;
,
p:blic Rectangle(Point p, int w, int h) ,
origin = p;
width = w;
height = h;
,

// a 2ethod for 2oving the rectangle
p:blic void 2ove(int x, int y) ,
origin.x = x;
origin.y = y;
,

// a 2ethod for co2p:ting the area of the rectangle
p:blic int getrea() ,
ret:rn width height;
,
,
74


Lach consLrucLor leLs you provlde lnlLlal values for Lhe recLangles slze and wldLh
uslng boLh prlmlLlve and reference Lypes lf a class has mulLlple consLrucLors Lhey
musL have dlfferenL slgnaLures 1he !ava compller dlfferenLlaLes Lhe consLrucLors
based on Lhe number and Lhe Lype of Lhe argumenLs When Lhe !ava compller
encounLers Lhe followlng code lL knows Lo call Lhe consLrucLor ln Lhe Rectangle class
LhaL requlres aPoint argumenL followed by Lwo lnLeger argumenLs

Rectangle rectJne = new Rectangle(originJne, 100, 200);
1hls calls one of Rectangles consLrucLors LhaL lnlLlallzesorigin Lo originJne lso Lhe
consLrucLor seLswidth Lo 100 and height Lo 00 now Lhere are Lwo references Lo Lhe
same Point object an ob[ecL can have mulLlple references Lo lL as shown ln Lhe
nexL flgure

1he followlng llne of code calls Lhe RectangleconsLrucLor LhaL requlres Lwo lnLeger
argumenLs whlch provlde Lhe lnlLlal values for width and height lf you lnspecL Lhe
code wlLhln Lhe consLrucLor you wlll see LhaL lL creaLes a new Point ob[ecL
whose x and y values are lnlLlallzed Lo 0
Rectangle rectTwo = new Rectangle(50, 100);
1he Rectangle consLrucLor used ln Lhe followlng sLaLemenL doesnL Lake any
argumenLs so lLs called a oootqoeot coosttoctot
Rectangle rect = new Rectangle();
73

ll classes have aL leasL one consLrucLor lf a class does noL expllclLly declare any Lhe
!ava compller auLomaLlcally provldes a noargumenL consLrucLor called Lhe Jefoolt
coosttoctot 1hls defaulL consLrucLor calls Lhe class parenLs noargumenL
consLrucLor or Lhe JbjectconsLrucLor lf Lhe class has no oLher parenL lf Lhe parenL
has no consLrucLor (Jbject does have one) Lhe compller wlll re[ecL Lhe program
&sing Objects
Cnce youve creaLed an ob[ecL you probably wanL Lo use lL for someLhlng ?ou may
need Lo use Lhe value of one of lLs flelds change one of lLs flelds or call one of lLs
meLhods Lo perform an acLlon
Referencing an Object's ieIds
Cb[ecL flelds are accessed by Lhelr name ?ou musL use a name LhaL ls unamblguous
You may use a simple name Ior a Iield within its own class. For example, we can add
a statement ithin theRectangle class that prints the width and height:
Syste2.o:t.println("Width and height are: " + width + ", " + height);
ln Lhls case width and height are slmple names
Code that is outside the object's class must use an object reIerence or expression,
Iollowed by the dot (.) operator, Iollowed by a simple Iield name, as in:
objectReference.fielda2e
For example, the code in the CreateJbjectDe2o class is outside the code Ior
the Rectangle class. $o to reIer to the origin, width, and height Iields within
theRectangle object named rectJne, theCreateJbjectDe2o class must use the
namesrectJne.origin, rectJne.width, andrectJne.height, respectively. The program
uses two oI these names to display the width and the height oIrectJne:
Syste2.o:t.println("Width of rectJne: " + rectJne.width);
Syste2.o:t.println("Height of rectJne: " + rectJne.height);
LLempLlng Lo use Lhe slmple names width and heightfrom Lhe code ln
Lhe CreateJbjectDe2o class doesnL make sense Lhose flelds exlsL only wlLhln an
ob[ecL and resulLs ln a compller error
Later, the program uses similar code to display inIormation about rectTwo. Objects oI
the same type have their own copy oI the same instance Iields. Thus,
76

each Rectangle object has Iields named origin,width, and height. When you access an
instance Iield through an object reIerence, you reIerence that particular object's Iield.
The two objects rectJne and rectTwo in the CreateJbjectDe2o program have
diIIerentorigin, width, and height Iields.
To access a Iield, you can use a named reIerence to an object, as in the previous
examples, or you can use any expression that returns an object reIerence. Recall that
the new operator returns a reIerence to an object. $o you could use the value returned
Irom new to access a new object's Iields:
int height = new Rectangle().height;
1hls sLaLemenL creaLes a new Rectangle ob[ecL and lmmedlaLely geLs lLs helghL ln
essence Lhe sLaLemenL calculaLes Lhe defaulL helghL of a Rectangle noLe LhaL afLer
Lhls sLaLemenL has been execuLed Lhe program no longer has a reference Lo Lhe
creaLed Rectangle because Lhe program never sLored Lhe reference anywhere 1he
ob[ecL ls unreferenced and lLs resources are free Lo be recycled by Lhe !ava IlrLual
,achlne
CaIIing an Object's ethods
?ou also use an ob[ecL reference Lo lnvoke an ob[ecLs meLhod ?ou append Lhe
meLhods slmple name Lo Lhe ob[ecL reference wlLh an lnLervenlng doL operaLor ()
lso you provlde wlLhln encloslng parenLheses any argumenLs Lo Lhe meLhod lf Lhe
meLhod does noL requlre any argumenLs use empLy parenLheses
objectReference.2ethoda2e(arg:2entList);
or
objectReference.2ethoda2e();
1he Rectangle class has Lwo meLhods getrea() Lo compuLe Lhe recLangles area
and 2ove() Lo change Lhe recLangles orlgln Peres Lhe CreateJbjectDe2o code LhaL
lnvokes Lhese Lwo meLhods
Syste2.o:t.println("rea of rectJne: " + rectJne.getrea());
...
rectTwo.2ove(40, 72);
1he flrsL sLaLemenL lnvokes rectJnes getrea()meLhod and dlsplays Lhe resulLs 1he
second llne movesrectTwo because Lhe 2ove() meLhod asslgns new values Lo Lhe
ob[ecLs origin.x and origin.y
As with instance Iields, 4bfectRe1erence must be a reIerence to an object. You can use
a variable name, but you also can use any expression that returns an object reIerence.
77

The new operator returns an object reIerence, so you can use the value returned Irom
new to invoke a new object's methods:
new Rectangle(100, 50).getrea()
1he expresslon new Rectangle(100, 50) reLurns an ob[ecL reference LhaL refers Lo
a Rectangle ob[ecL s shown you can use Lhe doL noLaLlon Lo lnvoke Lhe
newRectangles getrea() meLhod Lo compuLe Lhe area of Lhe new recLangle
$ome methods, such as getrea(), return a value. For methods that return a value, you
can use the method invocation in expressions. You can assign the return value to a
variable, use it to make decisions, or control a loop. This code assigns the value
returned bygetrea() to the variable areaJfRectangle:
int areaJfRectangle = new Rectangle(100, 50).getrea();
emember lnvoklng a meLhod on a parLlcular ob[ecL ls Lhe same as sendlng a
message Lo LhaL ob[ecL ln Lhls case Lhe ob[ecL LhaL getrea() ls lnvoked on ls Lhe
recLangle reLurned by Lhe consLrucLor
The Garbage CoIIector
Some ob[ecLorlenLed languages requlre LhaL you keep Lrack of all Lhe ob[ecLs you
creaLe and LhaL you expllclLly desLroy Lhem when Lhey are no longer needed
,anaglng memory expllclLly ls Ledlous and errorprone 1he !ava plaLform allows you
Lo creaLe as many ob[ecLs as you wanL (llmlLed of course by whaL your sysLem can
handle) and you donL have Lo worry abouL desLroylng Lhem 1he !ava runLlme
envlronmenL deleLes ob[ecLs when lL deLermlnes LhaL Lhey are no longer belng used
1hls process ls called qotboqe collectloo
An object is eligible Ior garbage collection when there are no more reIerences to that
object. ReIerences that are held in a variable are usually dropped when the variable
goes out oI scope. Or, you can explicitly drop an object reIerence by setting the
variable to the special value n:ll. Remember that a program can have multiple
reIerences to the same object; all reIerences to an object must be dropped beIore the
object is eligible Ior garbage collection.
The Java runtime environment has a garbage collector that periodically Irees the
memory used by objects that are no longer reIerenced. The garbage collector does its
job automatically when it determines that the time is right.

7

10 ore on CIasses
This section covers more aspects oI classes that depend on using object reIerences and
the dot operator that you learned about in the preceding sections on objects:
O Returning values Irom methods.
O The this keyword.
O Class vs. instance members.
O Access control.
11 Returning a VaIue from a ethod
meLhod reLurns Lo Lhe code LhaL lnvoked lL when lL
O compleLes all Lhe sLaLemenLs ln Lhe meLhod
O reaches a ret:rn sLaLemenL or
O Lhrows an excepLlon (covered laLer)
whlchever occurs flrsL
You declare a method's return type in its method declaration. Within the body oI the
method, you use theret:rn statement to return the value.
Any method declared void doesn't return a value. It does not need to contain
a ret:rn statement, but it may do so. In such a case, a ret:rn statement can be used to
branch out oI a control Ilow block and exit the method and is simply used like this:
ret:rn;

lf you Lry Lo reLurn a value from a meLhod LhaL ls declaredvoid you wlll geL a
compller error
Any method that is not declared void must contain aret:rn statement with a
corresponding return value, like this:
ret:rn ret:rnVal:e;

1he daLa Lype of Lhe reLurn value musL maLch Lhe meLhods declared reLurn Lype
you canL reLurn an lnLeger value from a meLhod declared Lo reLurn a boolean
The getrea() method in the Rectangle Rectangleclass that was discussed in the
sections on objects returns an integer:
79

// a 2ethod for co2p:ting the area of the rectangle
p:blic int getrea() ,
ret:rn width height;
,
1hls meLhod reLurns Lhe lnLeger LhaL Lhe expresslonwidthheight evaluaLes Lo
The getrea method returns a primitive type. A method can also return a reIerence
type. For example, in a program to manipulate icycle objects, we might have a
method like this:
p:blic icycle seeWhosFastest(icycle 2yike, icycle yo:rike, nviron2ent
env) ,
icycle fastest;
// code to calc:late which bike is faster, given
// each bike's gear and cadence and given
// the environ2ent (terrain and wind)
ret:rn fastest;
,
Returning a CIass or Interface
lf Lhls secLlon confuses you sklp lL and reLurn Lo lL afLer you have flnlshed Lhe lesson
on lnLerfaces and lnherlLance
When a method uses a class name as its return type, such as whosFastest does, the
class oI the type oI the returned object must be either a subclass oI, or the exact class
oI, the return type. $uppose that you have a class hierarchy in
which I2aginary:2ber is a subclass oIjava.lang.:2ber, which is in turn a subclass
oIJbject, as illustrated in the Iollowing Iigure.

The class hierarchy for maginaryNumber
now suppose LhaL you have a meLhod declared Lo reLurn a :2ber
p:blic :2ber ret:rn:2ber() ,
...
,
0

1he ret:rn:2ber meLhod can reLurn anI2aginary:2ber buL noL
an JbjectI2aginary:2ber ls a :2ber because lLs a subclass of :2ber Powever
an Jbject ls noL necessarlly a:2ber lL could be a String or anoLher Lype
You can override a method and deIine it to return a subclass oI the original method,
like this:
p:blic I2aginary:2ber ret:rn:2ber() ,
...
,
1hls Lechnlque called covotloot tetoto type means LhaL Lhe reLurn Lype ls allowed Lo
vary ln Lhe same dlrecLlon as Lhe subclass

Note ?ou also can use lnLerface names as reLurn Lypes ln Lhls case Lhe ob[ecL
reLurned musL lmplemenL Lhe speclfled lnLerface
12 &sing the this Keyword
WlLhln an lnsLance meLhod or a consLrucLor this ls a reference Lo Lhe cotteot
object Lhe ob[ecL whose meLhod or consLrucLor ls belng called ?ou can refer Lo
any member of Lhe currenL ob[ecL from wlLhln an lnsLance meLhod or a consLrucLor
by uslng this
&sing this with a ieId
1he mosL common reason for uslng Lhe this keyword ls because a fleld ls shadowed
by a meLhod or consLrucLor parameLer
For example, the Point class was written like this
p:blic class Point ,
p:blic int x = 0;
p:blic int y = 0;

//.onst7u.to7
publi. Point(int , int b) {
x = ,;
y = b;
}
,
buL lL could have been wrlLLen llke Lhls
1

p:blic class Point ,
p:blic int x = 0;
p:blic int y = 0;

//.onst7u.to7
publi. Point(int x int y) {
this.x = x;
this.y = y;
}
,
Lach argumenL Lo Lhe consLrucLor shadows one of Lhe ob[ecLs flelds lnslde Lhe
consLrucLor x ls a local copy of Lhe consLrucLors flrsL argumenL 1o refer Lo
Lhe Pointfleld x Lhe consLrucLor musL use this.x
&sing this with a Constructor
rom wlLhln a consLrucLor you can also use Lhe thiskeyword Lo call anoLher
consLrucLor ln Lhe same class uolng so ls called an expllclt coosttoctot lovocotloo
Peres anoLher Rectangle class wlLh a dlfferenL lmplemenLaLlon from Lhe one ln
Lhe CeLLlng SLarLedsecLlon
p:blic class Rectangle ,
private int x, y;
private int width, height;

p:blic Rectangle() ,
this(0 0 0 0);
,
p:blic Rectangle(int width, int height) ,
this(0 0 width height);
,
p:blic Rectangle(int x, int y, int width, int height) ,
this.x = x;
this.y = y;
this.width = width;
this.height = height;
,
...
,
1hls class conLalns a seL of consLrucLors Lach consLrucLor lnlLlallzes some or all of Lhe
recLangles member varlables 1he consLrucLors provlde a defaulL value for any
member varlable whose lnlLlal value ls noL provlded by an argumenL or example
Lhe noargumenL consLrucLor calls Lhe fourargumenL consLrucLor wlLh four 0 values
and Lhe LwoargumenL consLrucLor calls Lhe fourargumenL consLrucLor wlLh Lwo 0
values s before Lhe compller deLermlnes whlch consLrucLor Lo call based on Lhe
number and Lhe Lype of argumenLs


II present, the invocation oI another constructor must be the Iirst line in the
constructor.
13 ControIIing ccess to embers of a CIass
Access level modiIiers determine whether other classes can use a particular Iield or
invoke a particular method. There are two levels oI access control:
O At the top levelp:blic, or package-private(no explicit modiIier).
O At the member levelp:blic, private,protected, or package-private (no
explicit modiIier).
A class may be declared with the modiIier p:blic, in which case that class is visible to
all classes everywhere. II a class has no modiIier (the deIault, also known aspackage-
private), it is visible only within its own package (packages are named groups oI
related classesyou will learn about them in a later lesson.)
At the member level, you can also use the p:blicmodiIier or no modiIier (package-
private) just as with top-level classes, and with the same meaning. For members, there
are two additional access modiIiers:private and protected. The private modiIier
speciIies that the member can only be accessed in its own class.
The protected modiIier speciIies that the member can only be accessed within its own
package (as with package-private) and, in addition, by a subclass oI its class in
another package.
The Iollowing table shows the access to members permitted by each modiIier.
Access Levels
Modifier Class Package Subclass World
p:blic
Y Y Y Y
protected
Y Y Y N
n4 m4di1ier Y Y N N
private
Y N N N
The Iirst data column indicates whether the class itselI has access to the member
deIined by the access level. As you can see, a class always has access to its own
members. The second column indicates whether classes in the same package as the
class (regardless oI their parentage) have access to the member. The third column
indicates whether subclasses oI the class declared outside this package have
access to the member. The Iourth column indicates whether all classes have access to
the member.
3

Access levels aIIect you in two ways. First, when you use classes that come Irom
another source, such as the classes in the Java platIorm, access levels determine which
members oI those classes your own classes can use. $econd, when you write a class,
you need to decide what access level every member variable and every method in
your class should have.
Let's look at a collection oI classes and see how access levels aIIect visibility. The
Iollowing Iigure shows the Iour classes in this example and how they are related.

Classes and Packages of the Example Used to llustrate
Access Levels
The Iollowing table shows where the members oI the Alpha class are visible Ior each
oI the access modiIiers that can be applied to them.
'isibility
Modifier Alpha Beta Alphasub amma
p:blic
Y Y Y Y
protected
Y Y Y N
n4 m4di1ier Y Y N N
private
Y N N N

%ips on Choosing an Access Level: II other programmers use your class, you want to
ensure that errors Irom misuse cannot happen. Access levels can help you do this.
O Use the most restrictive access level that makes sense Ior a particular
member. Use private unless you have a good reason not to.
O Avoid p:blic Iields except Ior constants. (Many oI the examples in the
tutorial use public Iields. This may help to illustrate some points
concisely, but is not recommended Ior production code.) Public Iields
tend to link you to a particular implementation and limit your Ilexibility
in changing your code.

4

1 &nderstanding Instance and CIass embers
ln Lhls secLlon we dlscuss Lhe use of Lhe statickeyword Lo creaLe flelds and meLhods
LhaL belong Lo Lhe class raLher Lhan Lo an lnsLance of Lhe class
CIass VariabIes
When a number of ob[ecLs are creaLed from Lhe same class blueprlnL Lhey each have
Lhelr own dlsLlncL coples of lostooce votlobles ln Lhe case of Lhe icycle class Lhe
lnsLance varlables are cadence gear and speed Lach icycle ob[ecL has lLs own
values for Lhese varlables sLored ln dlfferenL memory locaLlons
$ometimes, you want to have variables that are common to all objects. This is
accomplished with the staticmodiIier. Fields that have the static modiIier in their
declaration are called static 1ields or class variables. They are associated with the
class, rather than with any object. Every instance oI the class shares a class variable,
which is in one Iixed location in memory. Any object can change the value oI a class
variable, but class variables can also be manipulated without creating an instance oI
the class.
For example, suppose you want to create a number oIicycle objects and assign each
a serial number, beginning with 1 Ior the Iirst object. This ID number is unique to
each object and is thereIore an instance variable. At the same time, you need a Iield to
keep track oI how many icycle objects have been created so that you know what ID
to assign to the next one. $uch a Iield is not related to any individual object, but to the
class as a whole. For this you need a class variable,n:2berJficycles, as Iollows:
p:blic class icycle,

private int cadence;
private int gear;
private int speed;

// ,dd ,n inst,n.e v,7i,ble 1o7 the obje.t ID
private int id;

// ,dd , .l,ss v,7i,ble 1o7 the numbe7 o1 Bi.y.le obje.ts inst,nti,ted
private st,ti. int n:2berJficycles = 0;
......
,
Class varlables are referenced by Lhe class name lLself as ln
icycle.n:2berJficycles
1hls makes lL clear LhaL Lhey are class varlables
3


Note ?ou can also refer Lo sLaLlc flelds wlLh an ob[ecL reference llke
2yike.n:2berJficycles
buL Lhls ls dlscouraged because lL does noL make lL clear LhaL Lhey are class varlables

You can use the icycle constructor to set the idinstance variable and increment
the n:2berJficyclesclass variable:
p:blic class icycle,

private int cadence;
private int gear;
private int speed;
private int id;
private static int n:2berJficycles = 0;

p:blic icycle(int startCadence, int startSpeed, int startGear),
gear = startGear;
cadence = startCadence;
speed = startSpeed;

// in.7ement numbe7 o1 Bi.y.les ,nd ,ssign ID numbe7
id = ++numbe7O1Bi.y.les;
,

// new method to 7etu7n the ID inst,n.e v,7i,ble
p:blic int getID() ,
ret:rn id;
,
.....
,
CIass ethods
1he !ava programmlng language supporLs sLaLlc meLhods as well as sLaLlc varlables
SLaLlc meLhods whlch have Lhe static modlfler ln Lhelr declaraLlons should be
lnvoked wlLh Lhe class name wlLhouL Lhe need for creaLlng an lnsLance of Lhe class
as ln
Classa2e.2ethoda2e(args)

Note ?ou can also refer Lo sLaLlc meLhods wlLh an ob[ecL reference llke
instancea2e.2ethoda2e(args)
buL Lhls ls dlscouraged because lL does noL make lL clear LhaL Lhey are class meLhods
6


A common use Ior static methods is to access static Iields. For example, we could add
a static method to theicycle class to access the n:2berJficycles static Iield:
p:blic st,ti. int get:2berJficycles() ,
ret:rn n:2berJficycles;
,
noL all comblnaLlons of lnsLance and class varlables and meLhods are allowed
O lnsLance meLhods can access lnsLance varlables and lnsLance meLhods dlrecLly
O lnsLance meLhods can access class varlables and class meLhods dlrecLly
O Class meLhods can access class varlables and class meLhods dlrecLly
O Class meLhods nf access lnsLance varlables or lnsLance meLhods dlrecLly
Lhey musL use an ob[ecL reference lso class meLhods cannoL use
Lhe this keyword as Lhere ls no lnsLance forthis Lo refer Lo
Constants
1he static modlfler ln comblnaLlon wlLh Lhe finalmodlfler ls also used Lo deflne
consLanLs 1he finalmodlfler lndlcaLes LhaL Lhe value of Lhls fleld cannoL change
For example, the Iollowing variable declaration deIines a constant named PI, whose
value is an approximation oI pi (the ratio oI the circumIerence oI a circle to its
diameter):
static final do:ble PI = 3.141592653589793;
ConsLanLs deflned ln Lhls way cannoL be reasslgned and lL ls a complleLlme error lf
your program Lrles Lo do so 8y convenLlon Lhe names of consLanL values are spelled
ln uppercase leLLers lf Lhe name ls composed of more Lhan one word Lhe words are
separaLed by an underscore (_)

Note lf a prlmlLlve Lype or a sLrlng ls deflned as a consLanL and Lhe value ls known aL
complle Llme Lhe compller replaces Lhe consLanL name everywhere ln Lhe code wlLh
lLs value 1hls ls called a coplletle coostoot lf Lhe value of Lhe consLanL ln Lhe
ouLslde world changes (for example lf lL ls leglslaLed LhaL pl acLually should be
3973) you wlll need Lo recomplle any classes LhaL use Lhls consLanL Lo geL Lhe
currenL value
7


The Bi.y.le CIass
fLer all Lhe modlflcaLlons made ln Lhls secLlon Lheicycle class ls now
p:blic class icycle,

private int cadence;
private int gear;
private int speed;

private int id;

private st,ti. int n:2berJficycles = 0;


p:blic icycle(int startCadence, int startSpeed, int startGear),
gear = startGear;
cadence = startCadence;
speed = startSpeed;

id = ++n:2berJficycles;
,

p:blic int getID() ,
ret:rn id;
,

p:blic static int get:2berJficycles() ,
ret:rn n:2berJficycles;
,

p:blic int getCadence(),
ret:rn cadence;
,

p:blic void setCadence(int newVal:e),
cadence = newVal:e;
,

p:blic int getGear(),
ret:rn gear;
,

p:blic void setGear(int newVal:e),
gear = newVal:e;
,

p:blic int getSpeed(),
ret:rn speed;
,

p:blic void applyrake(int decre2ent),
speed -= decre2ent;
,



p:blic void speedUp(int incre2ent),
speed += incre2ent;
,

,
15 InitiaIizing ieIds
s you have seen you can ofLen provlde an lnlLlal value for a fleld ln lLs declaraLlon
p:blic class edndreakfast ,

p:blic static int capacity = 10; //initialize to 10

private boolean f:ll = false; //initialize to false
,
1hls works well when Lhe lnlLlallzaLlon value ls avallable and Lhe lnlLlallzaLlon can be
puL on one llne Powever Lhls form of lnlLlallzaLlon has llmlLaLlons because of lLs
slmpllclLy lf lnlLlallzaLlon requlres some loglc (for example error handllng or
a for loop Lo flll a complex array) slmple asslgnmenL ls lnadequaLe lnsLance
varlables can be lnlLlallzed ln consLrucLors where error handllng or oLher loglc can be
used 1o provlde Lhe same capablllLy for class varlables Lhe !ava programmlng
language lncludes stotlc loltlollzotloo blocks

Note lL ls noL necessary Lo declare flelds aL Lhe beglnnlng of Lhe class deflnlLlon
alLhough Lhls ls Lhe mosL common pracLlce lL ls only necessary LhaL Lhey be declared
and lnlLlallzed before Lhey are used

Static InitiaIization Iocks
stotlc loltlollzotloo block ls a normal block of code enclosed ln braces , , and
preceded by Lhe statickeyword Pere ls an example
static ,

// whatever code is needed for initialization goes here
,
class can have any number of sLaLlc lnlLlallzaLlon blocks and Lhey can appear
anywhere ln Lhe class body 1he runLlme sysLem guaranLees LhaL sLaLlc lnlLlallzaLlon
blocks are called ln Lhe order LhaL Lhey appear ln Lhe source code
9

There is an alternative to static blocks you can write a private static method:
class Whatever ,
p:blic static varType 2yVar = initializeClassVariable();

private static varType initializeClassVariable() ,

//initialization code goes here
,
,
1he advanLage of prlvaLe sLaLlc meLhods ls LhaL Lhey can be reused laLer lf you need
Lo relnlLlallze Lhe class varlable
InitiaIizing Instance embers
normally you would puL code Lo lnlLlallze an lnsLance varlable ln a consLrucLor
1here are Lwo alLernaLlves Lo uslng a consLrucLor Lo lnlLlallze lnsLance varlables
lnlLlallzer blocks and flnal meLhods
Initializer blocks Ior instance variables look just like static initializer blocks, but
without the static keyword:
,

// whatever code is needed for initialization goes here
,
1he !ava compller coples lnlLlallzer blocks lnLo every consLrucLor 1herefore Lhls
approach can be used Lo share a block of code beLween mulLlple consLrucLors
A 1inal meth4d cannot be overridden in a subclass. This is discussed in the lesson on
interIaces and inheritance. Here is an example oI using a Iinal method Ior initializing
an instance variable:
class Whatever ,
private varType 2yVar = initializeInstanceVariable();

protected final varType initializeInstanceVariable() ,

//initialization code goes here
,
,

1hls ls especlally useful lf subclasses mlghL wanL Lo reuse Lhe lnlLlallzaLlon meLhod
1he meLhod ls flnal because calllng nonflnal meLhods durlng lnsLance lnlLlallzaLlon
can cause problems !oshua 8loch descrlbes Lhls ln more deLall ln LffecLlve !ava
90

16 Summary of Creating and &sing CIasses and Objects
A class declaration names the class and encloses the class body between braces. The
class name can be preceded by modiIiers. The class body contains Iields, methods,
and constructors Ior the class. A class uses Iields to contain state inIormation and uses
methods to implement behavior. Constructors that initialize a new instance oI a class
use the name oI the class and look like methods without a return type.
You control access to classes and members in the same way: by using an access
modiIier such as p:blic in their declaration.
You speciIy a class variable or a class method by using the static keyword in the
member's declaration. A member that is not declared as static is implicitly an
instance member. Class variables are shared by all instances oI a class and can be
accessed through the class name as well as an instance reIerence. Instances oI a class
get their own copy oI each instance variable, which must be accessed through an
instance reIerence.
You create an object Irom a class by using the newoperator and a constructor. The new
operator returns a reIerence to the object that was created. You can assign the
reIerence to a variable or use it directly.
Instance variables and methods that are accessible to code outside oI the class that
they are declared in can be reIerred to by using a qualiIied name. The qualiIied name
oI an instance variable looks like this:
objectReference.variableName
The qualiIied name oI a method looks like this:
objectReference.methodName(argumentList)

or

objectReference.methodName()
The garbage collector automatically cleans up unused objects. An object is unused iI
the program holds no more reIerences to it. You can explicitly drop a reIerence by
setting the variable holding the reIerence to n:ll.
17 Questions and ercises: CIasses
Questions
1. Consider the Iollowing class:
2. p:blic class IdentifyyParts ,
3. p:blic static int x = 7;
91

4. p:blic int y = 3;
5. ,
a. What are the class variables?
b. What are the instance variables?
c. What is the output Irom the Iollowing code:
d. IdentifyyParts a = new IdentifyyParts();
e. IdentifyyParts b = new IdentifyyParts();
f. a.y = 5;
g. b.y = 6;
h. a.x = 1;
i. b.x = 2;
j. Syste2.o:t.println("a.y = " + a.y);
k. Syste2.o:t.println("b.y = " + b.y);
l. Syste2.o:t.println("a.x = " + a.x);
2. Syste2.o:t.println("b.x = " + b.x);
n. Syste2.o:t.println("IdentifyyParts.x = " + IdentifyyParts.x);
ercises
1. Write a class whose instances represent a single playing card Irom a deck oI
cards. Playing cards have two distinguishing properties: rank and suit. Be sure
to keep your solution as you will be asked to rewrite it in Enum Types.

|nt
You can use the assert statement to check your assignments. You write:
assert (boolean expression to test);
II the boolean expression is Ialse, you will get an error message. For example,
assert toString(C) == "ce";
should return tr:e, so there will be no error message.
II you use the assert statement, you must run your program with the ea Ilag:
java -ea Yo:rProgra2.class

2. Write a class whose instances represent a fulldeck oI cards. You should also
keep this solution.
9

3. 3. Write a small program to test your deck and card classes. The program can
be as simple as creating a deck oI cards and displaying its cards.
Check your answers.
1 Questions and ercises: Objects
Questions
1. What's wrong with the Iollowing program?
2. p:blic class So2ethingIsWrong ,
3. p:blic static void 2ain(String, args) ,
4. Rectangle 2yRect;
5. 2yRect.width = 40;
6. 2yRect.height = 50;
7. Syste2.o:t.println("2yRect's area is " + 2yRect.area());
8. ,
9. ,
The Iollowing code creates one array and one string object. How many
reIerences to those objects exist aIter the code executes? Is either object eligible
Ior garbage collection?
1. ...
2. String, st:dents = new String10,;
3. String st:denta2e = "Peter Parker";
4. st:dents0, = st:denta2e;
5. st:denta2e = n:ll;
6. ...
How does a program destroy an object that it creates?
ercises
1. Fix the program called So2ethingIsWrongshown in Question 1.
2. Given the Iollowing class, called :2berHolder, write some code that creates an
instance oI the class, initializes its two member variables, and then displays the
value oI each member variable.
3. p:blic class :2berHolder ,
4. p:blic int anInt;
5. p:blic float aFloat;
6. ,
Check your answers


93

1 Nested CIasses
1he !ava programmlng language allows you Lo deflne a class wlLhln anoLher class
Such a class ls called a oesteJ closs and ls lllusLraLed here
class J:terClass ,
...
class estedClass ,
...
,
,

1erm|no|ogy nesLed classes are dlvlded lnLo Lwo caLegorles sLaLlc and nonsLaLlc
nesLed classes LhaL are declared static are slmply called stotlc oesteJ closses non
sLaLlc nesLed classes are called looet closses

class J:terClass ,
...
static class StaticestedClass ,
...
,
class InnerClass ,
...
,
,
nesLed class ls a member of lLs encloslng class nonsLaLlc nesLed classes (lnner
classes) have access Lo oLher members of Lhe encloslng class even lf Lhey are
declared prlvaLe SLaLlc nesLed classes do noL have access Lo oLher members of Lhe
encloslng class s a member of LheJ:terClass a nesLed class can be
declared privatep:blic protected or pockoqe ptlvote (ecall LhaL ouLer classes can
only be declared p:blic or pockoqe ptlvote)
Why &se Nested CIasses?
1here are several compelllng reasons for uslng nesLed classes among Lhem
O lL ls a way of loglcally grouplng classes LhaL are only used ln one place
O lL lncreases encapsulaLlon
O nesLed classes can lead Lo more readable and malnLalnable code
94

Logical grouping of classesII a class is useIul to only one other class, then it is
logical to embed it in that class and keep the two together. Nesting such "helper
classes" makes their package more streamlined.
Increased encapsulationConsider two top-level classes, A and B, where B needs
access to members oI A that would otherwise be declared private. By hiding class B
within class A, A's members can be declared private and B can access them. In
addition, B itselI can be hidden Irom the outside world.
More readable, maintainable codeNesting small classes within top-level classes
places the code closer to where it is used.
Static Nested CIasses
s wlLh class meLhods and varlables a sLaLlc nesLed class ls assoclaLed wlLh lLs ouLer
class nd llke sLaLlc class meLhods a sLaLlc nesLed class cannoL refer dlrecLly Lo
lnsLance varlables or meLhods deflned ln lLs encloslng class lL can use Lhem only
Lhrough an ob[ecL reference

Note sLaLlc nesLed class lnLeracLs wlLh Lhe lnsLance members of lLs ouLer class
(and oLher classes) [usL llke any oLher Loplevel class ln effecL a sLaLlc nesLed class ls
behavlorally a Loplevel class LhaL has been nesLed ln anoLher Loplevel class for
packaglng convenlence

$tatic nested classes are accessed using the enclosing class name:
J:terClass.StaticestedClass
or example Lo creaLe an ob[ecL for Lhe sLaLlc nesLed class use Lhls synLax
J:terClass.StaticestedClass nestedJbject = new
J:terClass.StaticestedClass();
Inner CIasses
s wlLh lnsLance meLhods and varlables an lnner class ls assoclaLed wlLh an lnsLance
of lLs encloslng class and has dlrecL access Lo LhaL ob[ecLs meLhods and flelds lso
because an lnner class ls assoclaLed wlLh an lnsLance lL cannoL deflne any sLaLlc
members lLself
93

Objects that are instances oI an inner class exist ithinan instance oI the outer class.
Consider the Iollowing classes:
class J:terClass ,
...
class InnerClass ,
...
,
,

n lnsLance of InnerClass can exlsL only wlLhln an lnsLance of J:terClass and has
dlrecL access Lo Lhe meLhods and flelds of lLs encloslng lnsLance 1he nexL
flgure lllusLraLes Lhls ldea

An nstance of nnerClass Exists Within an nstance of
OuterClass
To instantiate an inner class, you must Iirst instantiate the outer class. Then, create the
inner object within the outer object with this syntax:
J:terClass.InnerClass innerJbject = o:terJbject.new InnerClass();
ddlLlonally Lhere are Lwo speclal klnds of lnner classes local classes and
anonymous classes (also called anonymous lnner classes) 8oLh of Lhese wlll be
dlscussed brlefly ln Lhe nexL secLlon

Note lf you wanL more lnformaLlon on Lhe Laxonomy of Lhe dlfferenL klnds of classes
ln Lhe !ava programmlng language (whlch can be Lrlcky Lo descrlbe conclsely clearly
and correcLly) you mlghL wanL Lo read !oseph uarcys blog nesLed lnner ,ember
and 1opLevel Classes


96

20 Inner CIass ampIe
1o see an lnner class ln use leLs flrsL conslder an array ln Lhe followlng example we
wlll creaLe an array flll lL wlLh lnLeger values and Lhen ouLpuL only values of even
lndlces of Lhe array ln ascendlng order
The DataStr:ct:re class below consists oI:
O 1he DataStr:ct:re ouLer class whlch lncludes meLhods Lo add an lnLeger onLo
Lhe array and prlnL ouL values of even lndlces of Lhe array
O 1he InnervenIterator lnner class whlch ls slmllar Lo a sLandard !ava ltetotot
lLeraLors are used Lo sLep Lhrough a daLa sLrucLure and Lyplcally have meLhods
Lo LesL for Lhe lasL elemenL reLrleve Lhe currenL elemenL and move Lo Lhe
nexL elemenL
O 2ain meLhod LhaL lnsLanLlaLes aDataStr:ct:re ob[ecL (ds) and uses lL Lo flll
LhearrayJfInts array wlLh lnLeger values (0 1 3 eLc) Lhen calls
a printven meLhod Lo prlnL ouL values of even lndlces of arrayJfInts

p:blic class DataStr:ct:re ,
//create an array
private final static int SIZ = 15;
private int, arrayJfInts = new intSIZ,;

p:blic DataStr:ct:re() ,
//fill the array with ascending integer val:es
for (int i = 0; i < SIZ; i++) ,
arrayJfIntsi, = i;
,
,

p:blic void printven() ,
//print o:t val:es of even indices of the array
InnervenIterator iterator = this.new InnervenIterator();
while (iterator.hasext()) ,
Syste2.o:t.println(iterator.getext() + " ");
,
,

//inner class i2ple2ents the Iterator pattern
private class InnervenIterator ,
//start stepping thro:gh the array fro2 the beginning
private int next = 0;

p:blic boolean hasext() ,
//check if a c:rrent ele2ent is the last in the array
ret:rn (next <= SIZ - 1);
,

97

p:blic int getext() ,
//record a val:e of an even index of the array
int retVal:e = arrayJfIntsnext,;
//get the next even ele2ent
next += 2;
ret:rn retVal:e;
,
,

p:blic static void 2ain(String s,) ,
//fill the array with integer val:es and print o:t only val:es of
even indices
DataStr:ct:re ds = new DataStr:ct:re();
ds.printven();
,
,
1he ouLpuL ls
0 2 4 6 8 10 12 14
noLe LhaL Lhe InnervenIterator class refers dlrecLly Lo Lhe arrayJfInts lnsLance
varlable of LheDataStr:ct:re ob[ecL
Inner classes can be used to implement helper classes like the one shown in the
example above. II you plan on handling user-interIace events, you will need to know
how to use inner classes because the event-handling mechanism makes extensive use
oI them.
LocaI and nonymous Inner CIasses
1here are Lwo addlLlonal Lypes of lnner classes ?ou can declare an lnner class wlLhln
Lhe body of a meLhod Such a class ls known as a locol looet closs ?ou can also
declare an lnner class wlLhln Lhe body of a meLhod wlLhouL namlng lL 1hese classes
are known asooooyoos looet closses ?ou wlll encounLer such classes ln advanced
!ava programmlng
odifiers
?ou can use Lhe same modlflers for lnner classes LhaL you use for oLher members of
Lhe ouLer class or example you can use Lhe access speclflers private p:blic
and protected Lo resLrlcL access Lo lnner classes [usL as you do Lo oLher class
members



9

21 Summary of Nested CIasses
A class deIined within another class is called a nested class. Like other members oI a
class, a nested class can be declared static or not. A nonstatic nested class is called an
inner class. An instance oI an inner class can exist only within an instance oI its
enclosing class and has access to its enclosing class's members even iI they are
declared private.
The Iollowing table shows the types oI nested classes:
%ypes of ested Classes
%ype Scope Inner
static nested class member no
inner |non-static| class member yes
local class local yes
anonymous class only the point where it is deIined yes

22 Questions and ercises: Nested CIasses
Questions
1. The program Proble2.java doesn't compile. What do you need to do to make it
compile? Why?
2. Use the Java API documentation Ior the ox class (in the javax.swing package)
to help you answer the Iollowing questions.
a. What static nested class does ox deIine?
b. What inner class does ox deIine?
c. What is the superclass oI ox's inner class?
d. Which oI ox's nested classes can you use Irom any class?
e. How do you create an instance oI ox'sFiller class?
ercises
1. Get the Iile Class1.java. Compile and runClass1. What is the output?
Check your answers.


99

23 num Types
An enum type is a type whose 1ields consist oI a Iixed set oI constants. Common
examples include compass directions (values oI NORTH, $OUTH, EA$T, and
WE$T) and the days oI the week.
Because they are constants, the names oI an enum type's Iields are in uppercase
letters.
In the Java programming language, you deIine an enum type by using
the en:2 keyword. For example, you would speciIy a days-oI-the-week enum type as:

p:blic en:2 Day ,
SUDY, JDY, TUSDY, WDSDY,
THURSDY, FRIDY, STURDY
,
You should use enum types any time you need to represent a Iixed set oI constants.
That includes natural enum types such as the planets in our solar system and data sets
where you know all possible values at compile timeIor example, the choices on a
menu, command line Ilags, and so on.
Here is some code that shows you how to use the Dayenum deIined above:

p:blic class n:2Test ,
Day day;

p:blic n:2Test(Day day) ,
this.day = day;
,

p:blic void tellItLikeItIs() ,
switch (day) ,
case JDY: Syste2.o:t.println("ondays are bad.");
break;

case FRIDY: Syste2.o:t.println("Fridays are better.");
break;

case STURDY:
case SUDY: Syste2.o:t.println("Weekends are best.");
break;

defa:lt: Syste2.o:t.println("idweek days are
so-so.");
break;
,
,

100

p:blic static void 2ain(String, args) ,
n:2Test firstDay = new n:2Test(Day.JDY);
firstDay.tellItLikeItIs();
n:2Test thirdDay = new n:2Test(Day.WDSDY);
thirdDay.tellItLikeItIs();
n:2Test fifthDay = new n:2Test(Day.FRIDY);
fifthDay.tellItLikeItIs();
n:2Test sixthDay = new n:2Test(Day.STURDY);
sixthDay.tellItLikeItIs();
n:2Test seventhDay = new n:2Test(Day.SUDY);
seventhDay.tellItLikeItIs();


,
,

The output is:
ondays are bad.
idweek days are so-so.
Fridays are better.
Weekends are best.
Weekends are best.
Java programming language enum types are much more powerIul than their
counterparts in other languages. Theen:2 declaration deIines a class (called an enum
type). The enum class body can include methods and other Iields. The compiler
automatically adds some special methods when it creates an enum. For example, they
have a static val:es method that returns an array containing all oI the values oI the
enum in the order they are declared. This method is commonly used in combination
with the Ior-each construct to iterate over the values oI an enum type. For example,
this code Irom the Planet class example below iterates over all the planets in the solar
system.
for (Planet p : Planet.val:es()) ,
Syste2.o:t.printf("Yo:r weight on %s is %f%n",
p, p.s:rfaceWeight(2ass));
,

ote: All enums implicitly extendjava.lang.n:2. $ince Java does not support
multiple inheritance, an enum cannot extend anything else.

In the Iollowing example, Planet is an enum type that represents the planets in the
solar system. They are deIined with constant mass and radius properties.
Each enum constant is declared with values Ior the mass and radius parameters. These
values are passed to the constructor when the constant is created. Java requires that
the constants be deIined Iirst, prior to any Iields or methods. Also, when there are
Iields and methods, the list oI enum constants must end with a semicolon.
101


ote: The constructor Ior an enum type must be package-private or private access. It
automatically creates the constants that are deIined at the beginning oI the enum body.
You cannot invoke an enum constructor yourselI.

In addition to its properties and constructor, Planet has methods that allow you to
retrieve the surIace gravity and weight oI an object on each planet. Here is a sample
program that takes your weight on earth (in any unit) and calculates and prints your
weight on all oI the planets (in the same unit):

p:blic en:2 Planet ,
RCURY (3.303e+23, 2.4397e6),
VUS (4.869e+24, 6.0518e6),
RTH (5.976e+24, 6.37814e6),
RS (6.421e+23, 3.3972e6),
JUPITR (1.9e+27, 7.1492e7),
STUR (5.688e+26, 6.0268e7),
URUS (8.686e+25, 2.5559e7),
PTU (1.024e+26, 2.4746e7);

private final do:ble 2ass; // in kilogra2s
private final do:ble radi:s; // in 2eters
Planet(do:ble 2ass, do:ble radi:s) ,
this.2ass = 2ass;
this.radi:s = radi:s;
,
private do:ble 2ass() , ret:rn 2ass; ,
private do:ble radi:s() , ret:rn radi:s; ,

// :niversal gravitational constant (23 kg-1 s-2)
p:blic static final do:ble G = 6.67300-11;

do:ble s:rfaceGravity() ,
ret:rn G 2ass / (radi:s radi:s);
,
do:ble s:rfaceWeight(do:ble otherass) ,
ret:rn otherass s:rfaceGravity();
,
p:blic static void 2ain(String, args) ,
if (args.length != 1) ,
Syste2.err.println("Usage: java Planet <earth*weight");
Syste2.exit(-1);
,
do:ble earthWeight = Do:ble.parseDo:ble(args0,);
do:ble 2ass = earthWeight/RTH.s:rfaceGravity();
for (Planet p : Planet.val:es())
Syste2.o:t.printf("Yo:r weight on %s is %f%n",
p, p.s:rfaceWeight(2ass));
,
,
II you run Planet.class Irom the command line with an argument oI 175, you get this
output:
java Planet 175
10

Yo:r weight on RCURY is 66.107583
Yo:r weight on VUS is 158.374842
Yo:r weight on RTH is 175.000000
Yo:r weight on RS is 66.279007
Yo:r weight on JUPITR is 442.847567
Yo:r weight on STUR is 186.552719
Yo:r weight on URUS is 158.397260
Yo:r weight on PTU is 199.207413
2 Questions and ercises: num Types
ercises
1. Rewrite the class Card Irom the exercise inQuestions and Exercises: Classes so
that it represents the rank and suit oI a card with enum types.
2. Rewrite the Deck class.
Check your answers.
25 nnotations
Aooototloos provlde daLa abouL a program LhaL ls noL parL of Lhe program lLself
1hey have no dlrecL effecL on Lhe operaLlon of Lhe code Lhey annoLaLe
Annotations have a number oI uses, among them:
O Informat|on for the comp||er nnoLaLlons can be used by Lhe compller Lo
deLecL errors or suppress warnlngs
O omp||ert|me and dep|oymentt|me process|ng SofLware Lools can
process annoLaLlon lnformaLlon Lo generaLe code x,L flles and so forLh
O unt|me process|ng Some annoLaLlons are avallable Lo be examlned aL
runLlme
nnoLaLlons can be applled Lo a programs declaraLlons of classes flelds meLhods
and oLher program elemenLs
The annotation appears Iirst, oIten (by convention) on its own line, and may
include elements with named or unnamed values:
:thor(
na2e = "enja2in Franklin",
date = "3/27/2003"
)
class yClass() , ,
103

or
S:ppressWarnings(val:e = ":nchecked")
void 2yethod() , ,
lf Lhere ls [usL one elemenL named value Lhen Lhe name may be omlLLed as ln
S:ppressWarnings(":nchecked")
void 2yethod() , ,
lso lf an annoLaLlon has no elemenLs Lhe parenLheses may be omlLLed as ln
Jverride
void 2yS:perethod() , ,
ocumentation
,any annoLaLlons replace whaL would oLherwlse have been commenLs ln code
$uppose that a soItware group has traditionally begun the body oI every class with
comments providing important inIormation:
p:blic class Generation3List extends Generation2List ,

// :thor: John Doe
// Date: 3/17/2002
// C:rrent revision: 6
// Last 2odified: 4/12/2004
// y: Jane Doe
// Reviewers: lice, ill, Cindy

// class code goes here

,
1o add Lhls same meLadaLa wlLh an annoLaLlon you musL flrsL deflne Lhe oooototloo
type 1he synLax for dolng Lhls ls
interface ClassPrea2ble ,
String a:thor();
String date();
int c:rrentRevision() defa:lt 1;
String lastodified() defa:lt "/";
String lastodifiedy() defa:lt "/";
String, reviewers(); // ote :se of array
,
1he annoLaLlon Lype deflnlLlon looks somewhaL llke an lnLerface deflnlLlon where Lhe
keyword interface ls preceded by Lhe [ characLer ([ 1 as ln nnoLaLlon 1ype)
nnoLaLlon Lypes are ln facL a form of lotetfoce whlch wlll be covered ln a laLer
lesson or Lhe momenL you do noL need Lo undersLand lnLerfaces
104

The body oI the annotation deIinition above containsann4tati4n type
element declarations, which look a lot like methods. Note that they may deIine
optional deIault values.
Once the annotation type has been deIined, you can use annotations oI that type, with
the values Iilled in, like this:
ClassPrea2ble (
a:thor = "John Doe",
date = "3/17/2002",
c:rrentRevision = 6,
lastodified = "4/12/2004",
lastodifiedy = "Jane Doe",
reviewers = ,"lice", "ob", "Cindy", // ote array notation
)
p:blic class Generation3List extends Generation2List ,

// class code goes here

,

Note 1o make Lhe lnformaLlon lnClassPrea2ble appear ln !avadocgeneraLed
documenLaLlon you musL annoLaLe Lhe ClassPrea2ble deflnlLlon lLself wlLh
LheDoc:2ented annoLaLlon
i2port java.lang.annotation.; // i2port this to :se Doc:2ented

Doc:2ented
interface ClassPrea2ble ,

// nnotation ele2ent definitions

,

nnotations &sed by the CompiIer
1here are Lhree annoLaLlon Lypes LhaL are predeflned by Lhe language speclflcaLlon
lLself DeprecatedJverride and S:ppressWarnings
eprecatedthe Deprecated annotation indicates that the marked element
is deprecated and should no longer be used. The compiler generates a warning
whenever a program uses a method, class, or Iield with the Deprecated annotation.
When an element is deprecated, it should also be documented using the
Javadoc deprecated tag, as shown in the Iollowing example. The use oI the ""
symbol in both Javadoc comments and in annotations is not coincidentalthey are
103

related conceptually. Also, note that the Javadoc tag starts with a lowercase "d" and
the annotation starts with an uppercase "D".
// Javadoc co22ent follows
/
deprecated
* explanation of why it was deprecated
/
Dep7e.,ted
static void deprecatedethod() , ,
,
Overridethe Jverride annotation inIorms the compiler that the element is meant
to override an element declared in a superclass (overriding methods will be discussed
in the the lesson titled "InterIaces and Inheritance").
// mark method as a superclass method
// that has been overridden
Ove77ide
int overriddenethod() , ,
Whlle lLs noL requlred Lo use Lhls annoLaLlon when overrldlng a meLhod lL helps Lo
prevenL errors lf a meLhod marked wlLh Jverride falls Lo correcLly overrlde a
meLhod ln one of lLs superclasses Lhe compller generaLes an error
SuppressWarningsthe S:ppressWarningsannotation tells the compiler to
suppress speciIic warnings that it would otherwise generate. In the example below, a
deprecated method is used and the compiler would normally generate a warning. In
this case, however, the annotation causes the warning to be suppressed.
// use a deprecated method and tell
// compiler not to generate a warning
Supp7essW,7nings("dep7e.,tion")
void :seDeprecatedethod() ,
objectJne.deprecatedethod(); //deprecation warning - s:ppressed
,
Every compiler warning belongs to a category. The Java Language $peciIication lists
two categories: "deprecation" and "unchecked." The "unchecked" warning can occur
when interIacing with legacy code written beIore the advent oI generics (discussed in
the lesson titled "Generics"). To suppress more than one category oI warnings, use the
Iollowing syntax:
S:ppressWarnings(,":nchecked", "deprecation",)
106

nnotation Processing
1he more advanced uses of annoLaLlons lnclude wrlLlng an oooototloo ptocessot LhaL
can read a !ava program and Lake acLlons based on lLs annoLaLlons lL mlghL for
example generaLe auxlllary source code rellevlng Lhe programmer of havlng Lo
creaLe bollerplaLe code LhaL always follows predlcLable paLLerns 1o faclllLaLe Lhls
Lask release 30 of Lhe !uk lncludes an annoLaLlon processlng Lool called apt ln
release 6 of Lhe !uk Lhe funcLlonallLy of apt ls a sLandard parL of Lhe !ava compller
To make annotation inIormation available at runtime, the annotation type itselI must
be annotated withRetention(RetentionPolicy.RUTI), as Iollows:
i2port java.lang.annotation.;

Retention(RetentionPolicy.RUTI)
interface nnotationForR:nti2e ,

// le2ents that give infor2ation
// for r:nti2e processing

,
26 Questions and ercises: nnotations
Questions
What is wrong with the Iollowing interIace?
1. p:blic interface Ho:se ,
2. Deprecated
3. void open();
4. void openFrontDoor();
5. void openackDoor();
6. ,
Consider this implementation oI the Ho:seinterIace, shown in Question 1.
1. p:blic class yHo:se i2ple2ents Ho:se ,
2. p:blic void open() ,,
3. p:blic void openFrontDoor() ,,
4. p:blic void openackDoor() ,,
5. ,
II you compile this program, the compiler complains that open has been
deprecated (in the interIace). What can you do to get rid oI that warning?
Check your answers
107

Interfaces and Inheritance

1. nterfaces
2. Defining an nterface
3. mplementing an nterface
4. Using an nterface as a Type
5. Rewriting nterfaces
6. Summary of nterfaces
7. Questions and Exercises
8. nheritance
9. Overriding and Hiding Methods
10. Polymorphism
11. Hiding Fields
12. Using the Keyword super
13. Object as a Superclass
14. Writing Final Classes and Methods
15. Abstract Methods and Classes
16. Summary of nheritance
17. Questions and Exercises











10

Lesson: Interfaces and Inheritance
Interfaces
?ou saw an example of lmplemenLlng an lnLerface ln Lhe prevlous lesson ?ou can
read more abouL lnLerfaces herewhaL Lhey are for why you mlghL wanL Lo wrlLe
one and how Lo wrlLe one
Inheritance
1hls secLlon descrlbes Lhe way ln whlch you can derlve one class from anoLher 1haL
ls how a sobcloss can lnherlL flelds and meLhods from a sopetcloss ?ou wlll learn
LhaL all classes are derlved from Lhe Jbject class and how Lo modlfy Lhe meLhods
LhaL a subclass lnherlLs from superclasses 1hls secLlon also covers lnLerface
llkeobsttoct closses
1 Interfaces
1here are a number of slLuaLlons ln sofLware englneerlng when lL ls lmporLanL for
dlsparaLe groups of programmers Lo agree Lo a conLracL LhaL spells ouL how Lhelr
sofLware lnLeracLs Lach group should be able Lo wrlLe Lhelr code wlLhouL any
knowledge of how Lhe oLher groups code ls wrlLLen Cenerally
speaklng lotetfoces are such conLracLs
For example, imagine a Iuturistic society where computer-controlled robotic cars
transport passengers through city streets without a human operator. Automobile
manuIacturers write soItware (Java, oI course) that operates the automobilestop,
start, accelerate, turn leIt, and so Iorth. Another industrial group, electronic guidance
instrument manuIacturers, make computer systems that receive GP$ (Global
Positioning $ystem) position data and wireless transmission oI traIIic conditions and
use that inIormation to drive the car.
The auto manuIacturers must publish an industry-standard interIace that spells out in
detail what methods can be invoked to make the car move (any car, Irom any
manuIacturer). The guidance manuIacturers can then write soItware that invokes the
methods described in the interIace to command the car. Neither industrial group needs
to know h4 the other group's soItware is implemented. In Iact, each group considers
its soItware highly proprietary and reserves the right to modiIy it at any time, as long
as it continues to adhere to the published interIace.
109

Interfaces in Java
ln Lhe !ava programmlng language an lotetfoce ls a reference Lype slmllar Lo a class
LhaL can conLaln oolyconsLanLs meLhod slgnaLures and nesLed Lypes 1here are no
meLhod bodles lnLerfaces cannoL be lnsLanLlaLedLhey can only be lpleeoteJ by
classes orexteoJeJ by oLher lnLerfaces LxLenslon ls dlscussed laLer ln Lhls lesson
DeIining an interIace is similar to creating a new class:
p:blic interface JperateCar ,

// constant declarations, if any

// 2ethod signat:res
int t:rn(Direction direction, // n en:2 with val:es RIGHT, LFT
do:ble radi:s, do:ble startSpeed, do:ble endSpeed);
int changeLanes(Direction direction, do:ble startSpeed, do:ble endSpeed);
int signalT:rn(Direction direction, boolean signalJn);
int getRadarFront(do:ble distanceToCar, do:ble speedJfCar);
int getRadarRear(do:ble distanceToCar, do:ble speedJfCar);
......
// 2ore 2ethod signat:res
,
noLe LhaL Lhe meLhod slgnaLures have no braces and are LermlnaLed wlLh a
semlcolon
To use an interIace, you write a class that implementsthe interIace. When an
instantiable class implements an interIace, it provides a method body Ior each oI the
methods declared in the interIace. For example,
p:blic class JperateW760i i2ple2ents JperateCar ,

// the JperateCar 2ethod signat:res, with i2ple2entation --
// for exa2ple:
int signalT:rn(Direction direction, boolean signalJn) ,
//code to t:rn W's LFT t:rn indicator lights on
//code to t:rn W's LFT t:rn indicator lights off
//code to t:rn W's RIGHT t:rn indicator lights on
//code to t:rn W's RIGHT t:rn indicator lights off
,

// other 2e2bers, as needed -- for exa2ple, helper classes
// not visible to clients of the interface

,
ln Lhe roboLlc car example above lL ls Lhe auLomoblle manufacLurers who wlll
lmplemenL Lhe lnLerface ChevroleLs lmplemenLaLlon wlll be subsLanLlally dlfferenL
from LhaL of 1oyoLa of course buL boLh manufacLurers wlll adhere Lo Lhe same
110

lnLerface 1he guldance manufacLurers who are Lhe cllenLs of Lhe lnLerface wlll bulld
sysLems LhaL use CS daLa on a cars locaLlon dlglLal sLreeL maps and Lrafflc daLa Lo
drlve Lhe car ln so dolng Lhe guldance sysLems wlll lnvoke Lhe lnLerface meLhods
Lurn change lanes brake acceleraLe and so forLh
Interfaces as PIs
1he roboLlc car example shows an lnLerface belng used as an lndusLry
sLandard Appllcotloo 9toqtoloq lotetfoce (A9l) ls are also common ln
commerclal sofLware producLs 1yplcally a company sells a sofLware package LhaL
conLalns complex meLhods LhaL anoLher company wanLs Lo use ln lLs own sofLware
producL n example would be a package of dlglLal lmage processlng meLhods LhaL
are sold Lo companles maklng enduser graphlcs programs 1he lmage processlng
company wrlLes lLs classes Lo lmplemenL an lnLerface whlch lL makes publlc Lo lLs
cusLomers 1he graphlcs company Lhen lnvokes Lhe lmage processlng meLhods uslng
Lhe slgnaLures and reLurn Lypes deflned ln Lhe lnLerface Whlle Lhe lmage processlng
companys l ls made publlc (Lo lLs cusLomers) lLs lmplemenLaLlon of Lhe l ls kepL
as a closely guarded secreLln facL lL may revlse Lhe lmplemenLaLlon aL a laLer daLe
as long as lL conLlnues Lo lmplemenL Lhe orlglnal lnLerface LhaL lLs cusLomers have
relled on
Interfaces and uItipIe Inheritance
lnLerfaces have anoLher very lmporLanL role ln Lhe !ava programmlng language
lnLerfaces are noL parL of Lhe class hlerarchy alLhough Lhey work ln comblnaLlon
wlLh classes 1he !ava programmlng language does noL permlL mulLlple lnherlLance
(lnherlLance ls dlscussed laLer ln Lhls lesson) buL lnLerfaces provlde an alLernaLlve
In Java, a class can inherit Irom only one class but it can implement more than one
interIace. ThereIore, objects can have multiple types: the type oI their own class and
the types oI all the interIaces that they implement. This means that iI a variable is
declared to be the type oI an interIace, its value can reIerence any object that is
instantiated Irom any class that implements the interIace. This is discussed later in this
lesson, in the section titled "Using an InterIace as a Type."


111

2 efining an Interface
n lnLerface declaraLlon conslsLs of modlflers Lhe keyword interface Lhe lnLerface
name a commaseparaLed llsL of parenL lnLerfaces (lf any) and Lhe lnLerface body
or example
p:blic interface Gro:pedInterface extends Interface1,
Interface2, Interface3 ,

// constant declarations
do:ble = 2.718282; // base of nat:ral logarith2s

// 2ethod signat:res
void doSo2ething (int i, do:ble x);
int doSo2ethinglse(String s);

,
1he p:blic access speclfler lndlcaLes LhaL Lhe lnLerface can be used by any class ln
any package lf you do noL speclfy LhaL Lhe lnLerface ls publlc your lnLerface wlll be
accesslble only Lo classes deflned ln Lhe same package as Lhe lnLerface
An interIace can extend other interIaces, just as a class can extend or subclass another
class. However, whereas a class can extend only one other class, an interIace can
extend any number oI interIaces. The interIace declaration includes a comma-
separated list oI all the interIaces that it extends.
The Interface ody
1he lnLerface body conLalns meLhod declaraLlons for all Lhe meLhods lncluded ln Lhe
lnLerface meLhod declaraLlon wlLhln an lnLerface ls followed by a semlcolon buL
no braces because an lnLerface does noL provlde lmplemenLaLlons for Lhe meLhods
declared wlLhln lL ll meLhods declared ln an lnLerface are lmpllclLlyp:blic so Lhe
publlc modlfler can be omlLLed
An interIace can contain constant declarations in addition to method declarations. All
constant values deIined in an interIace are implicitly p:blic, static, and final. Once
again, these modiIiers can be omitted.
3 ImpIementing an Interface
1o declare a class LhaL lmplemenLs an lnLerface you lnclude an i2ple2ents clause ln
Lhe class declaraLlon ?our class can lmplemenL more Lhan one lnLerface so
11

Lhei2ple2ents keyword ls followed by a commaseparaLed llsL of Lhe lnLerfaces
lmplemenLed by Lhe class
8y convenLlon Lhe i2ple2ents clause follows Lheextends clause lf Lhere ls one
SampIe Interface, ReIatabIe
Conslder an lnLerface LhaL deflnes how Lo compare Lhe slze of ob[ecLs
p:blic interface Relatable ,

// this (object calling isLargerThan) and
// other 2:st be instances of the sa2e class
// ret:rns 1, 0, -1 if this is greater
// than, eq:al to, or less than other
p:blic int isLargerThan(Relatable other);
,
lf you wanL Lo be able Lo compare Lhe slze of slmllar ob[ecLs no maLLer whaL Lhey
are Lhe class LhaL lnsLanLlaLes Lhem should lmplemenL Relatable
Any class can implement Relatable iI there is some way to compare the relative
"size" oI objects instantiated Irom the class. For strings, it could be number oI
characters; Ior books, it could be number oI pages; Ior students, it could be weight;
and so Iorth. For planar geometric objects, area would be a good choice (see
theRectanglePl:s class that Iollows), while volume would work Ior three-dimensional
geometric objects. All such classes can implement the isLargerThan() method.
II you know that a class implements Relatable, then you know that you can compare
the size oI the objects instantiated Irom that class.
ImpIementing the ReIatabIe Interface
Pere ls Lhe Rectangle class LhaL was presenLed ln LheCreaLlng Cb[ecLs secLlon
rewrlLLen Lo lmplemenLRelatable
p:blic class RectanglePl:s i2ple2ents Relatable ,
p:blic int width = 0;
p:blic int height = 0;
p:blic Point origin;

// fo:r constr:ctors
p:blic RectanglePl:s() ,
origin = new Point(0, 0);
,
p:blic RectanglePl:s(Point p) ,
origin = p;
,
p:blic RectanglePl:s(int w, int h) ,
origin = new Point(0, 0);
113

width = w;
height = h;
,
p:blic RectanglePl:s(Point p, int w, int h) ,
origin = p;
width = w;
height = h;
,

// a 2ethod for 2oving the rectangle
p:blic void 2ove(int x, int y) ,
origin.x = x;
origin.y = y;
,

// a 2ethod for co2p:ting the area of the rectangle
p:blic int getrea() ,
ret:rn width height;
,

// a 2ethod req:ired to i2ple2ent the Relatable interface
p:blic int isLargerThan(Relatable other) ,
#e.t,nglePlus othe7#e.t = (#e.t,nglePlus)othe7;
if (this.getrea() < otherRect.getrea())
ret:rn -1;
else if (this.getrea() otherRect.getrea())
ret:rn 1;
else
ret:rn 0;
,
,
8ecause RectanglePl:s lmplemenLs Relatable Lhe slze of any
Lwo RectanglePl:s ob[ecLs can be compared

Note 1he isLargerThan meLhod as deflned ln Lhe Relatable lnLerface Lakes an
ob[ecL of Lype Relatable 1he llne of code shown ln bold ln Lhe prevlous example
casLs other Lo aRectanglePl:s lnsLance 1ype casLlng Lells Lhe compller whaL Lhe
ob[ecL really ls lnvoklnggetrea dlrecLly on Lhe other lnsLance (other.getrea())
would fall Lo complle because Lhe compller does noL undersLand LhaLother ls acLually
an lnsLance ofRectanglePl:s
&sing an Interface as a Type
When you deIine a new interIace, you are deIining a new reIerence data type. You can
use interIace names anywhere you can use any other data type name. II you deIine a
114

reIerence variable whose type is an interIace, any object you assign to it must be an
instance oI a class that implements the interIace.
As an example, here is a method Ior Iinding the largest object in a pair oI objects,
Ior any objects that are instantiated Irom a class that implements Relatable:
p:blic Jbject findLargest(Jbject object1, Jbject object2) ,
Relatable obj1 = (Relatable)object1;
Relatable obj2 = (Relatable)object2;
if ( (obj1).isLargerThan(obj2) 0)
ret:rn object1;
else
ret:rn object2;
,
By casting object1 to a Relatable type, it can invoke the isLargerThan method.
II you make a point oI implementing Relatable in a wide variety oI classes, the
objects instantiated Irom anyoI those classes can be compared with
thefindLargest() methodprovided that both objects are oI the same class. $imilarly,
they can all be compared with the Iollowing methods:
p:blic Jbject findS2allest(Jbject object1, Jbject object2) ,
Relatable obj1 = (Relatable)object1;
Relatable obj2 = (Relatable)object2;
if ( (obj1).isLargerThan(obj2) < 0)
ret:rn object1;
else
ret:rn object2;
,

p:blic boolean isq:al(Jbject object1, Jbject object2) ,
Relatable obj1 = (Relatable)object1;
Relatable obj2 = (Relatable)object2;
if ( (obj1).isLargerThan(obj2) == 0)
ret:rn tr:e;
else
ret:rn false;
,
These methods work Ior any "relatable" objects, no matter what their class inheritance
is. When they implement Relatable, they can be oI both their own class (or
superclass) type and a Relatable type. This gives them some oI the advantages oI
multiple inheritance, where they can have behavior Irom both a superclass and an
interIace.
5 Rewriting Interfaces
Consider an interIace that you have developed calledDoIt:
p:blic interface DoIt ,
void doSo2ething(int i, do:ble x);
113

int doSo2ethinglse(String s);
,
$uppose that, at a later time, you want to add a third method to DoIt, so that the
interIace now becomes:
p:blic interface DoIt ,

void doSo2ething(int i, do:ble x);
int doSo2ethinglse(String s);
boolean didItWork(int i, do:ble x, String s);

,
II you make this change, all classes that implement the old DoIt interIace will break
because they don't implement the interIace anymore. Programmers relying on this
interIace will protest loudly.
Try to anticipate all uses Ior your interIace and to speciIy it completely Irom the
beginning. Given that this is oIten impossible, you may need to create more interIaces
later. For example, you could create a DoItPl:s interIace that extends DoIt:
p:blic interface DoItPl:s extends DoIt ,

boolean didItWork(int i, do:ble x, String s);

,
Now users oI your code can choose to continue to use the old interIace or to upgrade
to the new interIace.
6 Summary of Interfaces
An interIace deIines a protocol oI communication between two objects.
An interIace declaration contains signatures, but no implementations, Ior a set oI
methods, and might also contain constant deIinitions.
A class that implements an interIace must implement all the methods declared in the
interIace.
An interIace name can be used anywhere a type can be used.
7 Questions and ercises: Interfaces
Questions
1. WhaL meLhods would a class LhaL lmplemenLs
Lhejava.lang.CharSeq:ence lnLerface have Lo lmplemenL?
116

WhaL ls wrong wlLh Lhe followlng lnLerface?
3. p:blic interface So2ethingIsWrong ,
4. void aethod(int aVal:e),
5. Syste2.o:t.println("Hi o2");
6. ,
7. ,
lx Lhe lnLerface ln quesLlon
9 ls Lhe followlng lnLerface valld?
10. p:blic interface arker ,
11. ,
ercises
1. WrlLe a class LhaL lmplemenLs Lhe CharSeq:encelnLerface found ln
Lhe java.lang package ?our lmplemenLaLlon should reLurn Lhe sLrlng
backwards SelecL one of Lhe senLences from Lhls book Lo use as Lhe daLa
WrlLe a small 2ainmeLhod Lo LesL your class make sure Lo call all four
meLhods
Suppose you have wrlLLen a Llme server LhaL perlodlcally noLlfles lLs cllenLs of
Lhe currenL daLe and Llme WrlLe an lnLerface Lhe server could use Lo enforce a
parLlcular proLocol on lLs cllenLs
Check your answers.
Inheritance
ln Lhe precedlng lessons you have seen lobetltoocemenLloned several Llmes ln Lhe
!ava language classes can be JetlveJ from oLher classes Lhereby lobetltloqflelds and
meLhods from Lhose classes

ef|n|t|ons
A class that is derived Irom another class is called a subclass (also a derived
class,extended class, or child class). The class Irom which the subclass is derived is
called asuperclass (also a base class or a parent class).
Excepting Jbject, which has no superclass, every class has one and only one direct
superclass (single inheritance). In the absence oI any other explicit superclass, every
class is implicitly a subclass oI Jbject.
117

Classes can be derived Irom classes that are derived Irom classes that are derived Irom
classes, and so on, and ultimately derived Irom the topmost class, Jbject. $uch a class
is said to be descended Irom all the classes in the inheritance chain stretching back
to Jbject.

1he ldea of lnherlLance ls slmple buL powerful When you wanL Lo creaLe a new class
and Lhere ls already a class LhaL lncludes some of Lhe code LhaL you wanL you can
derlve your new class from Lhe exlsLlng class ln dolng Lhls you can reuse Lhe flelds
and meLhods of Lhe exlsLlng class wlLhouL havlng Lo wrlLe (and debug!) Lhem
yourself
A subclass inherits all the members (Iields, methods, and nested classes) Irom its
superclass. Constructors are not members, so they are not inherited by subclasses, but
the constructor oI the superclass can be invoked Irom the subclass.
The Java PIatform CIass Hierarchy
1he Jbject class deflned ln Lhe java.lang package deflnes and lmplemenLs behavlor
common Lo all classeslncludlng Lhe ones LhaL you wrlLe ln Lhe !ava plaLform many
classes derlve dlrecLly from Jbject oLher classes derlve from some of Lhose classes
and so on formlng a hlerarchy of classes

All Classes in the Java Platform are Descendants of Object
L Lhe Lop of Lhe hlerarchy Jbject ls Lhe mosL general of all classes Classes near Lhe
boLLom of Lhe hlerarchy provlde more speclallzed behavlor
11

n ampIe of Inheritance
Pere ls Lhe sample code for a posslble lmplemenLaLlon of a icycle class LhaL was
presenLed ln Lhe Classes and Cb[ecLs lesson
p:blic class icycle ,

// the Bi.y.le .l,ss h,s th7ee fields
p:blic int cadence;
p:blic int gear;
p:blic int speed;

// the Bi.y.le .l,ss h,s one .43stru.t4r
p:blic icycle(int startCadence, int startSpeed, int startGear) ,
gear = startGear;
cadence = startCadence;
speed = startSpeed;
,

// the Bi.y.le .l,ss h,s 1ou7 2eth4ds
p:blic void setCadence(int newVal:e) ,
cadence = newVal:e;
,

p:blic void setGear(int newVal:e) ,
gear = newVal:e;
,

p:blic void applyrake(int decre2ent) ,
speed -= decre2ent;
,

p:blic void speedUp(int incre2ent) ,
speed += incre2ent;
,

,
A class declaration Ior a o:ntainike class that is a subclass oI icycle might look
like this:
p:blic class o:ntainike extends icycle ,

// the Mount,inBie sub.l,ss ,dds one field
p:blic int seatHeight;

// the Mount,inBie sub.l,ss h,s one .43stru.t4r
p:blic o:ntainike(int startHeight, int startCadence, int startSpeed,
int startGear) ,
s:per(startCadence, startSpeed, startGear);
seatHeight = startHeight;
,

// the Mount,inBie sub.l,ss ,dds one 2eth4d
p:blic void setHeight(int newVal:e) ,
119

seatHeight = newVal:e;
,

,
o:ntainike lnherlLs all Lhe flelds and meLhods oficycle and adds Lhe
fleld seatHeight and a meLhod Lo seL lL LxcepL for Lhe consLrucLor lL ls as lf you had
wrlLLen a new o:ntainike class enLlrely from scraLch wlLh four flelds and flve
meLhods Powever you dldnL have Lo do all Lhe work 1hls would be especlally
valuable lf Lhe meLhods ln Lhe icycle class were complex and had Laken subsLanLlal
Llme Lo debug
What You Can o in a SubcIass
subclass lnherlLs all of Lhe pobllc and ptotecteJmembers of lLs parenL no maLLer
whaL package Lhe subclass ls ln lf Lhe subclass ls ln Lhe same package as lLs parenL lL
also lnherlLs Lhe pockoqeptlvote members of Lhe parenL ?ou can use Lhe lnherlLed
members as ls replace Lhem hlde Lhem or supplemenL Lhem wlLh new members
O 1he lnherlLed flelds can be used dlrecLly [usL llke any oLher flelds
O ?ou can declare a fleld ln Lhe subclass wlLh Lhe same name as Lhe one ln Lhe
superclass LhusblJloq lL (noL recommended)
O ?ou can declare new flelds ln Lhe subclass LhaL are noL ln Lhe superclass
O 1he lnherlLed meLhods can be used dlrecLly as Lhey are
O ?ou can wrlLe a new lostooce meLhod ln Lhe subclass LhaL has Lhe same
slgnaLure as Lhe one ln Lhe superclass Lhus ovettlJloq lL
O ?ou can wrlLe a new stotlc meLhod ln Lhe subclass LhaL has Lhe same slgnaLure
as Lhe one ln Lhe superclass Lhus blJloq lL
O ?ou can declare new meLhods ln Lhe subclass LhaL are noL ln Lhe superclass
O ?ou can wrlLe a subclass consLrucLor LhaL lnvokes Lhe consLrucLor of Lhe
superclass elLher lmpllclLly or by uslng Lhe keyword s:per
1he followlng secLlons ln Lhls lesson wlll expand on Lhese Loplcs
Private embers in a SupercIass
subclass does noL lnherlL Lhe private members of lLs parenL class Powever lf Lhe
superclass has publlc or proLecLed meLhods for accesslng lLs prlvaLe flelds Lhese can
also be used by Lhe subclass
10

A nested class has access to all the private members oI its enclosing classboth Iields
and methods. ThereIore, a public or protected nested class inherited by a subclass has
indirect access to all oI the private members oI the superclass.
Casting Objects
We have seen LhaL an ob[ecL ls of Lhe daLa Lype of Lhe class from whlch lL was
lnsLanLlaLed or example lf we wrlLe
p:blic o:ntainike 2yike = new o:ntainike();
Lhen 2yike ls of Lype o:ntainike
o:ntainike is descended Irom icycle andJbject. ThereIore, a o:ntainike is
a icycle and is also an Jbject, and it can be used wherevericycle or Jbject objects
are called Ior.
The reverse is not necessarily true: a icycle may be ao:ntainike, but it isn't
necessarily. $imilarly, anJbject may be a icycle or a o:ntainike, but it isn't
necessarily.
asting shows the use oI an object oI one type in place oI another type, among the
objects permitted by inheritance and implementations. For example, iI we write
Jbject obj = new o:ntainike();
Lhen obj ls boLh an Jbject and a o:ntainbike (unLll such Llme as obj ls asslgned
anoLher ob[ecL LhaL ls oot ao:ntainbike) 1hls ls called lpllclt costloq
II, on the other hand, we write
o:ntainike 2yike = obj;
we would geL a complleLlme error because obj ls noL known Lo Lhe compller Lo be
a o:ntainike Powever we can tell Lhe compller LhaL we promlse Lo asslgn
a o:ntainike Lo obj by expllclt costloq
o:ntainike 2yike = (o:ntainike)obj;
1hls casL lnserLs a runLlme check LhaL obj ls asslgned ao:ntainike so LhaL Lhe
compller can safely assume LhaL obj ls a o:ntainike lf obj ls noL ao:ntainbike aL
runLlme an excepLlon wlll be Lhrown

11

Note ?ou can make a loglcal LesL as Lo Lhe Lype of a parLlcular ob[ecL uslng
Lheinstanceof operaLor 1hls can save you from a runLlme error owlng Lo an
lmproper casL or example
if (obj instanceof o:ntainike) ,
o:ntainike 2yike = (o:ntainike)obj;
,
Pere Lhe instanceof operaLor verlfles LhaLobj refers Lo a o:ntainike so LhaL we can
make Lhe casL wlLh knowledge LhaL Lhere wlll be no runLlme excepLlon Lhrown
Overriding and Hiding ethods
Instance ethods
n lnsLance meLhod ln a subclass wlLh Lhe same slgnaLure (name plus Lhe number
and Lhe Lype of lLs parameLers) and reLurn Lype as an lnsLance meLhod ln Lhe
superclassovettlJes Lhe superclasss meLhod
The ability oI a subclass to override a method allows a class to inherit Irom a
superclass whose behavior is "close enough" and then to modiIy behavior as needed.
The overriding method has the same name, number and type oI parameters, and return
type as the method it overrides. An overriding method can also return a subtype oI the
type returned by the overridden method. This is called a c4variant return type.
When overriding a method, you might want to use theJverride annotation that
instructs the compiler that you intend to override a method in the superclass. II, Ior
some reason, the compiler detects that the method does not exist in one oI the
superclasses, it will generate an error. For more inIormation on Jverride,
seennotations.
CIass ethods
lf a subclass deflnes a class meLhod wlLh Lhe same slgnaLure as a class meLhod ln Lhe
superclass Lhe meLhod ln Lhe subclass blJes Lhe one ln Lhe superclass
The distinction between hiding and overriding has important implications. The
version oI the overridden method that gets invoked is the one in the subclass. The
version oI the hidden method that gets invoked depends on whether it is invoked Irom
the superclass or the subclass. Let's look at an example that contains two classes. The
Iirst is ni2al, which contains one instance method and one class method:
p:blic class ni2al ,
p:blic static void testClassethod() ,
1

Syste2.o:t.println("The class 2ethod in ni2al.");
,
p:blic void testInstanceethod() ,
Syste2.o:t.println("The instance 2ethod in ni2al.");
,
,
1he second class a subclass of ni2al ls called Cat
p:blic class Cat extends ni2al ,
p:blic static void testClassethod() ,
Syste2.o:t.println("The class 2ethod in Cat.");
,
p:blic void testInstanceethod() ,
Syste2.o:t.println("The instance 2ethod in Cat.");
,

p:blic static void 2ain(String, args) ,
Cat 2yCat = new Cat();
ni2al 2yni2al = 2yCat;
ni2al.testClassethod();
2yni2al.testInstanceethod();
,
,
1he Cat class overrldes Lhe lnsLance meLhod ln ni2aland hldes Lhe class meLhod
ln ni2al 1he 2ain meLhod ln Lhls class creaLes an lnsLance of Cat and
callstestClassethod() on Lhe class andtestInstanceethod() on Lhe lnsLance
The output Irom this program is as Iollows:
The class 2ethod in ni2al.
The instance 2ethod in Cat.
s promlsed Lhe verslon of Lhe hldden meLhod LhaL geLs lnvoked ls Lhe one ln Lhe
superclass and Lhe verslon of Lhe overrldden meLhod LhaL geLs lnvoked ls Lhe one ln
Lhe subclass
odifiers
1he access speclfler for an overrldlng meLhod can allow more buL noL less access
Lhan Lhe overrldden meLhod or example a proLecLed lnsLance meLhod ln Lhe
superclass can be made publlc buL noL prlvaLe ln Lhe subclass
You will get a compile-time error iI you attempt to change an instance method in the
superclass to a class method in the subclass, and vice versa.
Summary
1he followlng Lable summarlzes whaL happens when you deflne a meLhod wlLh Lhe
same slgnaLure as a meLhod ln a superclass
13

ef|n|ng a Method w|th the Same S|gnature as a Superc|asss Method
Superc|ass Instance Method Superc|ass Stat|c Method
Subc|ass Instance Method Cverrldes CeneraLes a complleLlme error
Subc|ass Stat|c Method CeneraLes a complleLlme error Pldes

Note ln a subclass you can overload Lhe meLhods lnherlLed from Lhe superclass
Such overloaded meLhods nelLher hlde nor overrlde Lhe superclass meLhodsLhey
are new meLhods unlque Lo Lhe subclass
10 PoIymorphism
The dictionary deIinition oI p4lym4rphism reIers to a principle in biology in which an
organism or species can have many diIIerent Iorms or stages. This principle can also
be applied to object-oriented programming and languages like the Java language.
$ubclasses oI a class can deIine their own unique behaviors and yet share some oI the
same Iunctionality oI the parent class.
Polymorphism can be demonstrated with a minor modiIication to the icycle class.
For example, aprintDescription method could be added to the class that displays all
the data currently stored in an instance.
p:blic void printDescription(),
Syste2.o:t.println("\nike is in gear " + this.gear + " with a cadence of "
+
this.cadence + " and travelling at a speed of " + this.speed + ". ");
,
To demonstrate polymorphic Ieatures in the Java language, extend the icycle class
with ao:ntainike and a Roadike class. Foro:ntainike, add a Iield Ior s:spension,
which is aString value that indicates iI the bike has a Iront shock absorber, Front. Or,
the bike has a Iront and back shock absorber, D:al.
Here is the updated class:
p:blic class o:ntainike extends icycle,
private String s:spension;

p:blic o:ntainike(int startCadence, int startSpeed, int startGear, String
s:spensionType),
14

s:per(startCadence, startSpeed, startGear);
this.setS:spension(s:spensionType);
,

p:blic String getS:spension(),
ret:rn this.s:spension;
,

p:blic void setS:spension(String s:spensionType),
this.s:spension = s:spensionType;
,

p:blic void printDescription(),
s:per.printDescription();
Syste2.o:t.println("The o:ntainike has a " + getS:spension()
+ " s:spension.");
,
,
Note the overridden printDescription method. In addition to the inIormation
provided beIore, additional data about the suspension is included to the output.
Next, create the Roadike class. Because road or racing bikes have skinny tires, add an
attribute to track the tire width. Here is the Roadike class:
p:blic class Roadike extends icycle,
private int tireWidth; // In 2illi2eters (22)

p:blic Roadike(int startCadence, int startSpeed, int startGear, int
newTireWidth),
s:per(startCadence, startSpeed, startGear);
this.setTireWidth(newTireWidth);
,

p:blic int getTireWidth(),
ret:rn this.tireWidth;
,

p:blic void setTireWidth(int newTireWidth),
this.tireWidth = newTireWidth;
,

p:blic void printDescription(),
s:per.printDescription();
Syste2.o:t.println("The Roadike has " + getTireWidth()
+ " tires.");
,

,
Note that once again, the printDescription method has been overridden. This time,
inIormation about the tire width is displayed.
13

To summarize, there are three classes: icycle,o:ntainike, and Roadike. The two
subclasses override the printDescription method and print unique inIormation.
Here is a test program that creates three icyclevariables. Each variable is assigned to
one oI the three bicycle classes. Each variable is then printed.
p:blic class Testikes ,
p:blic static void 2ain(String, args),
icycle bike01, bike02, bike03;

bike01 = new icycle(20, 10, 1);
bike02 = new o:ntainike(20, 10, 5, "D:al");
bike03 = new Roadike(40, 20, 8, 23);

bike01.printDescription();
bike02.printDescription();
bike03.printDescription();

,
,
The Iollowing is the output Irom the test program:
ike is in gear 1 with a cadence of 20 and travelling at a speed of 10.

ike is in gear 5 with a cadence of 20 and travelling at a speed of 10.
The o:ntainike has a D:al s:spension.

ike is in gear 8 with a cadence of 40 and travelling at a speed of 20.
The Roadike has 23 tires.
The Java virtual machine (JVM) calls the appropriate method Ior the object that is
reIerred to in each variable. It does not call the method that is deIined by the variable's
type. This behavior is reIerred to as virtual meth4d inv4cati4n and demonstrates an
aspect oI the important polymorphism Ieatures in the Java language.
11 Hiding ieIds
Within a class, a Iield that has the same name as a Iield in the superclass hides the
superclass's Iield, even iI their types are diIIerent. Within the subclass, the Iield in the
superclass cannot be reIerenced by its simple name. Instead, the Iield must be
accessed through s:per, which is covered in the next section. Generally speaking, we
don't recommend hiding Iields as it makes code diIIicult to read.
12 &sing the Keyword super
ccessing SupercIass embers
lf your meLhod overrldes one of lLs superclasss meLhods you can lnvoke Lhe
overrldden meLhod Lhrough Lhe use of Lhe keyword s:per ?ou can also use s:per Lo
16

refer Lo a hldden fleld (alLhough hldlng flelds ls dlscouraged) Conslder Lhls
class S:perclass
p:blic class S:perclass ,

p:blic void printethod() ,
Syste2.o:t.println("Printed in S:perclass.");
,
,
Pere ls a subclass called S:bclass LhaL overrldesprintethod()
p:blic class S:bclass extends S:perclass ,

p:blic void printethod() , //overrides printethod in S:perclass
s:per.printethod();
Syste2.o:t.println("Printed in S:bclass");
,
p:blic static void 2ain(String, args) ,

S:bclass s = new S:bclass();
s.printethod();
,

,
WlLhln S:bclass Lhe slmple name printethod()refers Lo Lhe one declared
ln S:bclass whlch overrldes Lhe one ln S:perclass So Lo refer
Lo printethod()lnherlLed from S:perclass S:bclass musL use a quallfled name
uslng s:per as shown Complllng and execuLlng S:bclass prlnLs Lhe followlng
Printed in S:perclass.
Printed in S:bclass
SubcIass Constructors
1he followlng example lllusLraLes how Lo use Lhe s:perkeyword Lo lnvoke a
superclasss consLrucLor ecall from Lhe icycle example LhaL o:ntainike ls a
subclass of icycle Pere ls Lhe o:ntainike(subclass) consLrucLor LhaL calls Lhe
superclass consLrucLor and Lhen adds lnlLlallzaLlon code of lLs own
p:blic o:ntainike(int startHeight, int startCadence, int startSpeed,
int startGear) ,
s:per(startCadence, startSpeed, startGear);
seatHeight = startHeight;
,
lnvocaLlon of a superclass consLrucLor musL be Lhe flrsL llne ln Lhe subclass
consLrucLor
The syntax Ior calling a superclass constructor is
17

s:per();
--or--
s:per(para2eter list);
WlLh s:per() Lhe superclass noargumenL consLrucLor ls called
WlLh s:per(para2eter list) Lhe superclass consLrucLor wlLh a maLchlng parameLer
llsL ls called

Note lf a consLrucLor does noL expllclLly lnvoke a superclass consLrucLor Lhe !ava
compller auLomaLlcally lnserLs a call Lo Lhe noargumenL consLrucLor of Lhe
superclass lf Lhe super class does noL have a noargumenL consLrucLor you wlll geL a
complleLlme errorJbject Joes have such a consLrucLor so lfJbject ls Lhe only
superclass Lhere ls no problem

lf a subclass consLrucLor lnvokes a consLrucLor of lLs superclass elLher expllclLly or
lmpllclLly you mlghL Lhlnk LhaL Lhere wlll be a whole chaln of consLrucLors called all
Lhe way back Lo Lhe consLrucLor of Jbject ln facL Lhls ls Lhe case lL ls
called coosttoctot cbololoq and you need Lo be aware of lL when Lhere ls a long llne
of class descenL
13 Object as a SupercIass
1he Jbject class ln Lhe java.lang package slLs aL Lhe Lop of Lhe class hlerarchy Lree
Lvery class ls a descendanL dlrecL or lndlrecL of Lhe Jbject class Lvery class you use
or wrlLe lnherlLs Lhe lnsLance meLhods of Jbject ?ou need noL use any of Lhese
meLhods buL lf you choose Lo do so you may need Lo overrlde Lhem wlLh code LhaL
ls speclflc Lo your class 1he meLhods lnherlLed from Jbject LhaL are dlscussed ln Lhls
secLlon are
O protected Jbject clone() throws CloneotS:pportedxception
CreaLes and reLurns a copy of Lhls ob[ecL
O p:blic boolean eq:als(Jbject obj)
lndlcaLes wheLher some oLher ob[ecL ls equal Lo Lhls one
O protected void finalize() throws Throwable
Called by Lhe garbage collecLor on an ob[ecL when garbage
collecLlon deLermlnes LhaL Lhere are no more references Lo Lhe ob[ecL
1

O p:blic final Class getClass()
eLurns Lhe runLlme class of an ob[ecL
O p:blic int hashCode()
eLurns a hash code value for Lhe ob[ecL
O p:blic String toString()
eLurns a sLrlng represenLaLlon of Lhe ob[ecL
The notify, notifyll, and wait methods oIJbject all play a part in synchronizing the
activities oI independently running threads in a program, which is discussed in a later
lesson and won't be covered here. There are Iive oI these methods:
O p:blic final void notify()
O p:blic final void notifyll()
O p:blic final void wait()
O p:blic final void wait(long ti2eo:t)
O p:blic final void wait(long ti2eo:t, int nanos)

Note 1here are some subLle aspecLs Lo a number of Lhese meLhods especlally
Lhe clonemeLhod ?ou can geL lnformaLlon on Lhe correcL usage of Lhese meLhods ln
Lhe bookLffecLlve !ava by !osh 8loch

The cIone() ethod
lf a class or one of lLs superclasses lmplemenLs LheCloneable lnLerface you can use
Lhe clone() meLhod Lo creaLe a copy from an exlsLlng ob[ecL 1o creaLe a clone you
wrlLe
aCloneableObject.clone();
Jbjects lmplemenLaLlon of Lhls meLhod checks Lo see wheLher Lhe ob[ecL on
whlch clone() was lnvoked lmplemenLs Lhe Cloneable lnLerface lf Lhe ob[ecL does
noL Lhe meLhod Lhrows aCloneotS:pportedxception excepLlon LxcepLlon handllng
wlll be covered ln a laLer lesson or Lhe momenL you need Lo know
LhaL clone() musL be declared as
protected Jbject clone() throws CloneotS:pportedxception
-- or --
p:blic Jbject clone() throws CloneotS:pportedxception
lf you are golng Lo wrlLe a clone() meLhod Lo overrlde Lhe one ln Jbject
19

II the object on which clone() was invoked does implement
the Cloneable interIace, Jbject's implementation oI the clone() method creates an
object oI the same class as the original object and initializes the new object's member
variables to have the same values as the original object's corresponding member
variables.
The simplest way to make your class cloneable is to addi2ple2ents Cloneable to your
class's declaration. then your objects can invoke the clone() method.
For some classes, the deIault behavior oI Jbject'sclone() method works just Iine. II,
however, an object contains a reIerence to an external object, sayJbjxternal, you
may need to override clone() to get correct behavior. Otherwise, a change
inJbjxternal made by one object will be visible in its clone also. This means that the
original object and its clone are not independentto decouple them, you must
override clone() so that it clones the object andJbjxternal. Then the original object
reIerencesJbjxternal and the clone reIerences a clone oIJbjxternal, so that the
object and its clone are truly independent.
The e6uaIs() ethod
1he eq:als() meLhod compares Lwo ob[ecLs for equallLy and reLurns tr:e lf Lhey are
equal 1heeq:als() meLhod provlded ln Lhe Jbject class uses Lhe ldenLlLy operaLor
(==) Lo deLermlne wheLher Lwo ob[ecLs are equal or prlmlLlve daLa Lypes Lhls glves
Lhe correcL resulL or ob[ecLs however lL does noL 1heeq:als() meLhod provlded
by Jbject LesLs wheLher Lhe ob[ecL tefeteoces are equalLhaL ls lf Lhe ob[ecLs
compared are Lhe exacL same ob[ecL
To test whether two objects are equal in the sense oIequivalency (containing the same
inIormation), you must override the eq:als() method. Here is an example oI
aook class that overrides eq:als():
p:blic class ook ,
...
p:blic boolean eq:als(Jbject obj) ,
if (obj instanceof ook)
ret:rn IS.eq:als((ook)obj.getIS());
else
ret:rn false;
,
,
Conslder Lhls code LhaL LesLs Lwo lnsLances of Lhe ookclass for equallLy
ook firstook = new ook("0201914670"); //Swing T:torial, 2nd edition
ook secondook = new ook("0201914670");
if (firstook.eq:als(secondook)) ,
130

Syste2.o:t.println("objects are eq:al");
, else ,
Syste2.o:t.println("objects are not eq:al");
,
1hls program dlsplays objects are eq:al even
Lhough firstook and secondook reference Lwo dlsLlncL ob[ecLs 1hey are consldered
equal because Lhe ob[ecLs compared conLaln Lhe same lS8n number
You should always override the eq:als() method iI the identity operator is not
appropriate Ior your class.

Note lf you overrlde eq:als() you musL overrlde hashCode() as well

The finaIize() ethod
1he Jbject class provldes a callback meLhodfinalize() LhaL oy be lnvoked on an
ob[ecL when lL becomes garbage Jbjects lmplemenLaLlon offinalize() does
noLhlngyou can overrldefinalize() Lo do cleanup such as freelng resources
The finalize() method may be called automatically by the system, but when it is
called, or even iI it is called, is uncertain. ThereIore, you should not rely on this
method to do your cleanup Ior you. For example, iI you don't close Iile descriptors in
your code aIter perIorming I/O and you expect finalize() to close them Ior you, you
may run out oI Iile descriptors.
The getCIass() ethod
?ou cannoL overrlde getClass
The getClass() method returns a Class object, which has methods you can use to get
inIormation about the class, such as its name (getSi2plea2e()), its superclass
(getS:perclass()), and the interIaces it implements (getInterfaces()). For example,
the Iollowing method gets and displays the class name oI an object:
void printClassa2e(Jbject obj) ,
Syste2.o:t.println("The object's class is "
obj.getClass().getSi2plea2e());
,
1he Class class ln Lhe java.lang package has a large number of meLhods (more Lhan
30) or example you can LesL Lo see lf Lhe class ls an annoLaLlon (isnnotation())
131

an lnLerface (isInterface()) or an enumeraLlon (isn:2()) ?ou can see whaL Lhe
ob[ecLs flelds are (getFields()) or whaL lLs meLhods are (getethods()) and so on
The hashCode() ethod
1he value reLurned by hashCode() ls Lhe ob[ecLs hash code whlch ls Lhe ob[ecLs
memory address ln hexadeclmal
By deIinition, iI two objects are equal, their hash codemust als4 be equal. II you
override the eq:als()method, you change the way two objects are equated
and Jbject's implementation oI hashCode() is no longer valid. ThereIore, iI you
override the eq:als()method, you must also override the hashCode()method as well.
The toString() ethod
?ou should always conslder overrldlng Lhe toString()meLhod ln your classes
The Jbject's toString() method returns a Stringrepresentation oI the object, which is
very useIul Ior debugging. The String representation Ior an object depends entirely on
the object, which is why you need to override toString() in your classes.
You can use toString() along withSyste2.o:t.println() to display a text
representation oI an object, such as an instance oI ook:
Syste2.o:t.println(firstook.toString());
whlch would for a properly overrldden toString()meLhod prlnL someLhlng useful
llke Lhls
IS: 0201914670; The JFC Swing T:torial; G:ide to Constr:cting GUIs, 2nd
dition
1 Writing inaI CIasses and ethods
You can declare some or all oI a class's methods 1inal. You use the final keyword in
a method declaration to indicate that the method cannot be overridden by subclasses.
The Jbject class does thisa number oI its methods are final.
You might wish to make a method Iinal iI it has an implementation that should not be
changed and it is critical to the consistent state oI the object. For example, you might
want to make the getFirstPlayer method in this Chesslgorith2 class Iinal:
class Chesslgorith2 ,
en:2 ChessPlayer , WHIT, LCK ,
...
1in,l ChessPlayer getFirstPlayer() ,
13

ret:rn ChessPlayer.WHIT;
,
...
,
Methods called Irom constructors should generally be declared Iinal. II a constructor
calls a non-Iinal method, a subclass may redeIine that method with surprising or
undesirable results.
Note that you can also declare an entire class Iinal this prevents the class Irom
being subclassed. This is particularly useIul, Ior example, when creating an immutable
class like the String class.
15 bstract ethods and CIasses
n obsttoct closs ls a class LhaL ls declaredabstractlL may or may noL lnclude
absLracL meLhods bsLracL classes cannoL be lnsLanLlaLed buL Lhey can be
subclassed
An abstract meth4d is a method that is declared without an implementation (without
braces, and Iollowed by a semicolon), like this:
abstract void 2oveTo(do:ble deltaX, do:ble deltaY);
lf a class lncludes absLracL meLhods Lhe class lLself ostbe declared abstract as ln
p:blic abstract class GraphicJbject ,
// declare fields
// declare non-abstract 2ethods
abstract void draw();
,
When an absLracL class ls subclassed Lhe subclass usually provldes lmplemenLaLlons
for all of Lhe absLracL meLhods ln lLs parenL class Powever lf lL does noL Lhe
subclass musL also be declared abstract

Note ll of Lhe meLhods ln an lotetfoce (see Lhe Interfaces secLlon)
are lpllcltlyabsLracL so Lhe abstract modlfler ls noL used wlLh lnLerface meLhods (lL
could belLs [usL noL necessary)

133

bstract CIasses versus Interfaces
Dnllke lnLerfaces absLracL classes can conLaln flelds LhaL are noL static and final
and Lhey can conLaln lmplemenLed meLhods Such absLracL classes are slmllar Lo
lnLerfaces excepL LhaL Lhey provlde a parLlal lmplemenLaLlon leavlng lL Lo subclasses
Lo compleLe Lhe lmplemenLaLlon lf an absLracL class conLalns oolyabsLracL meLhod
declaraLlons lL should be declared as an lnLerface lnsLead
Multiple interIaces can be implemented by classes anywhere in the class hierarchy,
whether or not they are related to one another in any way. Think
oI Co2parableor Cloneable, Ior example.
By comparison, abstract classes are most commonly subclassed to share pieces oI
implementation. A single abstract class is subclassed by similar classes that have a lot
in common (the implemented parts oI the abstract class), but also have some
diIIerences (the abstract methods).
n bstract CIass ampIe
ln an ob[ecLorlenLed drawlng appllcaLlon you can draw clrcles recLangles llnes
8ezler curves and many oLher graphlc ob[ecLs 1hese ob[ecLs all have cerLaln sLaLes
(for example poslLlon orlenLaLlon llne color flll color) and behavlors (for example
move1o roLaLe reslze draw) ln common Some of Lhese sLaLes and behavlors are
Lhe same for all graphlc ob[ecLsfor example poslLlon flll color and move1o
CLhers requlre dlfferenL lmplemenLaLlonsfor example reslze or draw
llGraphicJbjects musL know how Lo draw or reslze Lhemselves Lhey [usL dlffer ln
how Lhey do lL 1hls ls a perfecL slLuaLlon for an absLracL superclass ?ou can Lake
advanLage of Lhe slmllarlLles and declare all Lhe graphlc ob[ecLs Lo lnherlL from Lhe
same absLracL parenL ob[ecLfor example GraphicJbject as shown ln Lhe followlng
flgure

Classes Rectangle, Line, Bezier, and Circle inherit from
GraphicObject
134

First, you declare an abstract class, GraphicJbject, to provide member variables and
methods that are wholly shared by all subclasses, such as the current position and
the 2oveTo method. GraphicJbject also declares abstract methods Ior methods, such
as draw or resize, that need to be implemented by all subclasses but must be
implemented in diIIerent ways. The GraphicJbjectclass can look something like this:
abstract class GraphicJbject ,
int x, y;
...
void 2oveTo(int newX, int newY) ,
...
,
abstract void draw();
abstract void resize();
,
Lach nonabsLracL subclass of GraphicJbject such asCircle and Rectangle musL
provlde lmplemenLaLlons for Lhe draw and resize meLhods
class Circle extends GraphicJbject ,
void draw() ,
...
,
void resize() ,
...
,
,
class Rectangle extends GraphicJbject ,
void draw() ,
...
,
void resize() ,
...
,
,
When an bstract CIass ImpIements an Interface
ln Lhe secLlon on Interfaces lL was noLed LhaL a class LhaL lmplemenLs an lnLerface
musL lmplemenL oll of Lhe lnLerfaces meLhods lL ls posslble however Lo deflne a
class LhaL does noL lmplemenL all of Lhe lnLerface meLhods provlded LhaL Lhe class ls
declared Lo beabstract or example
abstract class X i2ple2ents Y ,
// i2ple2ents all b:t one 2ethod of Y
,

class XX extends X ,
// i2ple2ents the re2aining 2ethod in Y
,
133

ln Lhls case class X musL be abstract because lL does noL fully lmplemenL Y buL
class XX does ln facL lmplemenL Y
CIass embers
n absLracL class may have static flelds and staticmeLhods ?ou can use Lhese
sLaLlc members wlLh a class referencefor
examplebstractClass.staticethod()as you would wlLh any oLher class
16 Summary of Inheritance
Except Ior the Jbject class, a class has exactly one direct superclass. A class inherits
Iields and methods Irom all its superclasses, whether direct or indirect. A subclass can
override methods that it inherits, or it can hide Iields or methods that it inherits. (Note
that hiding Iields is generally bad programming practice.)
The table in Overriding and Hiding Methods section shows the eIIect oI declaring a
method with the same signature as a method in the superclass.
The Jbject class is the top oI the class hierarchy. All classes are descendants Irom this
class and inherit methods Irom it. UseIul methods inherited
Irom Jbjectinclude toString(), eq:als(), clone(), andgetClass().
You can prevent a class Irom being subclassed by using the final keyword in the
class's declaration. $imilarly, you can prevent a method Irom being overridden by
subclasses by declaring it as a Iinal method.
An abstract class can only be subclassed; it cannot be instantiated. An abstract class
can contain abstract methodsmethods that are declared but not implemented.
$ubclasses then provide the implementations Ior the abstract methods.
17 Questions and ercises: Inheritance
Questions
1 Conslder Lhe followlng Lwo classes
p:blic class Class ,
p:blic void 2ethodJne(int i) ,
,
p:blic void 2ethodTwo(int i) ,
,
p:blic static void 2ethodThree(int i) ,
,
p:blic static void 2ethodFo:r(int i) ,
136

,
,

p:blic class Class extends Class ,
p:blic static void 2ethodJne(int i) ,
,
p:blic void 2ethodTwo(int i) ,
,
p:blic void 2ethodThree(int i) ,
,
p:blic static void 2ethodFo:r(int i) ,
,
,
a Whlch meLhod overrldes a meLhod ln Lhe superclass?
b Whlch meLhod hldes a meLhod ln Lhe superclass?
c WhaL do Lhe oLher meLhods do?

Conslder Lhe Card Deck and DisplayDeck classes you wroLe ln Q:estions and
xercises: Classes WhaL Jbject meLhods should each of Lhese classes overrlde?
ercises
1 WrlLe Lhe lmplemenLaLlons for Lhe meLhods LhaL you answered ln quesLlon

Check your answers.

















137

Numbers and Strings

1. Numbers
2. The Numbers Classes
3. Formatting Numeric Print Output
4. Beyond Basic Arithmetic
5. Summary of Numbers
6. Questions and Exercises
7. Characters
8. Strings
9. Converting Between Numbers and Strings
10. Manipulating Characters in a String
11. Comparing Strings and Portions of Strings
12. The StringBuilder Class
13. Summary of Characters and Strings
14. Questions and Exercises












13

Lesson: Numbers and Strings
Numbers
1hls secLlon beglns wlLh a dlscusslon of Lhe :2ber class (ln Lhe java.lang package)
and lLs subclasses ln parLlcular Lhls secLlon Lalks abouL Lhe slLuaLlons where you
would use lnsLanLlaLlons of Lhese classes raLher Lhan Lhe prlmlLlve daLa Lypes
ddlLlonally Lhls secLlon Lalks abouL oLher classes you mlghL need Lo work wlLh
numbers such as formaLLlng or uslng maLhemaLlcal funcLlons Lo complemenL Lhe
operaLors bullL lnLo Lhe language
Strings
SLrlngs whlch are wldely used ln !ava programmlng are a sequence of characLers ln
Lhe !ava programmlng language sLrlngs are ob[ecLs 1hls secLlon descrlbes uslng
Lhe String class Lo creaLe and manlpulaLe sLrlngs lL also compares
Lhe String and String:ilder classes
1 Numbers
This section begins with a discussion oI the :2ber class in the java.lang package, its
subclasses, and the situations where you would use instantiations oI these classes
rather than the primitive number types.
This section also presents the PrintStrea2 andDeci2alFor2at classes, which provide
methods Ior writing Iormatted numerical output.
Finally, the ath class in java.lang is discussed. It contains mathematical Iunctions to
complement the operators built into the language. This class has methods Ior the
trigonometric Iunctions, exponential Iunctions, and so Iorth.
2 The Numbers CIasses
When working with numbers, most oI the time you use the primitive types in your
code. For example:
int i = 500;
float gpa = 3.65f;
byte 2ask = 0xff;
There are, however, reasons to use objects in place oI primitives, and the Java
platIorm provides rapperclasses Ior each oI the primitive data types. These classes
"wrap" the primitive in an object. OIten, the wrapping is done by the compileriI you
use a primitive where an object is expected, the compiler b4xes the primitive in its
139

wrapper class Ior you. $imilarly, iI you use a number object when a primitive is
expected, the compiler unb4xes the object Ior you.
Here is an example oI boxing and unboxing:
Integer x, y;
x = 12;
y = 15;
Syste2.o:t.println(x+y);
When x and y are assigned integer values, the compiler boxes the integers
because x and y are integer objects. In the println() statement, x and y are unboxed so
that they can be added as integers.
All oI the numeric wrapper classes are subclasses oI the abstract class :2ber:


ote: There are Iour other subclasses oI:2ber that are not discussed
here.igDeci2al and igInteger are used Ior high-precision
calculations. to2icIntegerand to2icLong are used Ior multi-threaded applications.

There are three reasons that you might use a :2berobject rather than a primitive:
1. As an argument oI a method that expects an object (oIten used when
manipulating collections oI numbers).
2. To use constants deIined by the class, such asI*VLU and X*VLU, that
provide the upper and lower bounds oI the data type.
3. To use class methods Ior converting values to and Irom other primitive
types, Ior converting to and Irom strings, and Ior converting between
number systems (decimal, octal, hexadecimal, binary).
The Iollowing table lists the instance methods that all the subclasses oI
the :2ber class implement.
Methods Implemented by all Subclasses of umber
Method escription
140

byte byteVal:e()
short shortVal:e()
int intVal:e()
long longVal:e()
float floatVal:e()
do:ble do:bleVal:e()
Converts the value oI this:2ber object to the
primitive data type returned.
int co2pareTo(yte anotheryte)
int co2pareTo(Do:ble anotherDo:ble)
int co2pareTo(Float anotherFloat)
int co2pareTo(Integer anotherInteger)
int co2pareTo(Long anotherLong)
int co2pareTo(Short anotherShort)
Compares this :2berobject to the argument.
boolean eq:als(Jbject obj)
Determines whether this number object is
equal to the argument.
The methods return tr:eiI the argument is
notn:ll and is an object oI the same type and
with the same numeric value.
There are some extra requirements
Ior Do:bleand Float objects that are described
in the Java API documentation.
Each :2ber class contains other methods that are useIul Ior converting numbers to
and Irom strings and Ior converting between number systems. The Iollowing table
lists these methods in the Integer class. Methods Ior the other :2ber subclasses are
similar:
Conversion Methods, Intege7 Class
Method escription
static Integer decode(String s)
Decodes a string into an integer. Can accept
string representations oI decimal, octal, or
hexadecimal numbers as input.
static int parseInt(String s)
Returns an integer (decimal only).
static int parseInt(String s, int
radix)
Returns an integer, given a string
representation oI decimal, binary, octal, or
hexadecimal (radixequals 10, 2, 8, or 16
respectively) numbers as input.
String toString()
Returns a String object representing the
value oI this Integer.
static String toString(int i)
Returns a String object representing the
speciIied integer.
141

static Integer val:eJf(int i)
Returns an Integerobject holding the value
oI the speciIied primitive.
static Integer val:eJf(String s)
Returns an Integerobject holding the value
oI the speciIied string representation.
static Integer val:eJf(String s, int
radix)
Returns an Integerobject holding the integer
value oI the speciIied string representation,
parsed with the value oI radix. For example, iI
s "333" and radix 8, the method returns
the base-ten integer equivalent oI the octal
number 333.
3 ormatting Numeric Print Output
Larller you saw Lhe use of Lhe print and printlnmeLhods for prlnLlng sLrlngs Lo
sLandard ouLpuL (Syste2.o:t) Slnce all numbers can be converLed Lo sLrlngs (as you
wlll see laLer ln Lhls lesson) you can use Lhese meLhods Lo prlnL ouL an arblLrary
mlxLure of sLrlngs and numbers 1he !ava programmlng language has oLher meLhods
however LhaL allow you Lo exerclse much more conLrol over your prlnL ouLpuL when
numbers are lncluded
The printf and format ethods
1he java.io package lncludes a PrintStrea2 class LhaL has Lwo formaLLlng meLhods
LhaL you can use Lo replace print and println 1hese meLhods for2atand printf are
equlvalenL Lo one anoLher 1he famlllarSyste2.o:t LhaL you have been uslng happens
Lo be aPrintStrea2 ob[ecL so you can lnvoke PrintStrea2meLhods on Syste2.o:t
1hus you can use for2at orprintf anywhere ln your code where you have prevlously
been uslng print or println or example
Syste2.o:t.for2at(.....);
The syntax Ior these two java.io.PrintStrea2methods is the same:
p:blic PrintStrea2 for2at(String for2at, Jbject... args)
where for2at ls a sLrlng LhaL speclfles Lhe formaLLlng Lo be used and args ls a llsL of
Lhe varlables Lo be prlnLed uslng LhaL formaLLlng slmple example would be
Syste2.o:t.for2at("The val:e of the float variable is %f, while the val:e of
the " +
"integer variable is %d, and the string is %s", floatVar,
intVar, stringVar);
14

1he flrsL parameLer for2at ls a formaL sLrlng speclfylng how Lhe ob[ecLs ln Lhe
second parameLer args are Lo be formaLLed 1he formaL sLrlng conLalns plaln LexL as
well as fotot speclflets whlch are speclal characLers LhaL formaL Lhe argumenLs
of Jbject... args (1he noLaLlon Jbject... args ls called vototqs whlch means LhaL
Lhe number of argumenLs may vary)
Format speciIiers begin with a percent sign () and end with a c4nverter. The
converter is a character indicating the type oI argument to be Iormatted. In between
the percent sign () and the converter you can have optional Ilags and speciIiers.
There are many converters, Ilags, and speciIiers, which are documented
injava.:til.For2atter
Here is a basic example:
int i = 461012;
Syste2.o:t.for2at("The val:e of i is: %d%n", i);
1he %d speclfles LhaL Lhe slngle varlable ls a declmal lnLeger 1he %n ls a plaLform
lndependenL newllne characLer 1he ouLpuL ls
The val:e of i is: 461012
The printf and for2at methods are overloaded. Each has a version with the Iollowing
syntax:
p:blic PrintStrea2 for2at(Locale l, String for2at, Jbject... args)
1o prlnL numbers ln Lhe rench sysLem (where a comma ls used ln place of Lhe
declmal place ln Lhe Lngllsh represenLaLlon of floaLlng polnL numbers) for example
you would use
Syste2.o:t.for2at(Locale.FRC, "The val:e of the float variable is %f,
while the val:e of the " +
"integer variable is %d, and the string is %s%n",
floatVar, intVar, stringVar);
n ampIe
1he followlng Lable llsLs some of Lhe converLers and flags LhaL are used ln Lhe sample
programTestFor2at.java LhaL follows Lhe Lable
onverters and I|ags Used |nTestFo7m,t.j,v,
onverter I|ag Lxp|anat|on
143

d declmal lnLeger
f floaL
n
new llne characLer approprlaLe Lo Lhe plaLform
runnlng Lhe appllcaLlon ?ou should always
use %n raLher Lhan \n
L8
daLe Llme converslonlocalespeclflc full
name of monLh
Ld Le
daLe Llme converslondlglL day of monLh
Ld has leadlng zeroes as needed Le does noL
Ly L?
daLe Llme converslonLy dlglL year L?
4dlglL year
Ll daLe Llme converslonhour ln 1hour clock
L,
daLe Llme converslonmlnuLes ln dlglLs
wlLh leadlng zeroes as necessary
Lp
daLe Llme converslonlocalespeclflc am/pm
(lower case)
Lm
daLe Llme converslonmonLhs ln dlglLs
wlLh leadlng zeroes as necessary
Lu daLe Llme converslondaLe as LmLdLy
0
LlghL characLers ln wldLh wlLh leadlng zeroes as
necessary
+ lncludes slgn wheLher poslLlve or negaLlve
lncludes localespeclflc grouplng characLers
144

LefL[usLlfled
3 1hree places afLer declmal polnL
103
1en characLers ln wldLh rlghL [usLlfled wlLh Lhree
places afLer declmal polnL
The Iollowing program shows some oI the Iormatting that you can do with for2at.
The output is shown within double quotes in the embedded comment:
i2port java.:til.Calendar;
i2port java.:til.Locale;

p:blic class TestFor2at ,

p:blic static void 2ain(String, args) ,
long n = 461012;
Syste2.o:t.for2at("%d%n", n); // -- "461012"
Syste2.o:t.for2at("%08d%n", n); // -- "00461012"
Syste2.o:t.for2at("%+8d%n", n); // -- " +461012"
Syste2.o:t.for2at("%,8d%n", n); // -- " 461,012"
Syste2.o:t.for2at("%+,8d%n%n", n); // -- "+461,012"

do:ble pi = ath.PI;
Syste2.o:t.for2at("%f%n", pi); // -- "3.141593"
Syste2.o:t.for2at("%.3f%n", pi); // -- "3.142"
Syste2.o:t.for2at("%10.3f%n", pi); // -- " 3.142"
Syste2.o:t.for2at("%-10.3f%n", pi); // -- "3.142"
Syste2.o:t.for2at(Locale.FRC,
"%-10.4f%n%n", pi); // -- "3,1416"

Calendar c = Calendar.getInstance();
Syste2.o:t.for2at("%t %te, %tY%n", c, c, c); // -- "ay 29, 2006"
Syste2.o:t.for2at("%tl:%t %tp%n", c, c, c); // -- "2:34 a2"
Syste2.o:t.for2at("%tD%n", c); // -- "05/29/06"
,
,

Note 1he dlscusslon ln Lhls secLlon covers [usL Lhe baslcs of
Lhe for2at and printfmeLhods urLher deLall can be found ln Lheasic I/J secLlon
LlLled ormaLLlng DslngString.for2at Lo creaLe sLrlngs ls covered lnSLrlngs

143

The ecimaIormat CIass
?ou can use Lhe java.text.Deci2alFor2at class Lo conLrol Lhe dlsplay of leadlng and
Lralllng zeros preflxes and sufflxes grouplng (Lhousands) separaLors and Lhe
declmal separaLor Deci2alFor2at offers a greaL deal of flexlblllLy ln Lhe formaLLlng of
numbers buL lL can make your code more complex
The example that Iollows creates a Deci2alFor2atobject, 2yFor2atter, by passing a
pattern string to theDeci2alFor2at constructor. The for2at() method,
which Deci2alFor2at inherits Irom :2berFor2at, is then invoked by 2yFor2atterit
accepts a do:blevalue as an argument and returns the Iormatted number in a string:
Here is a sample program that illustrates the use oIDeci2alFor2at:

i2port java.text.;

p:blic class Deci2alFor2atDe2o ,

static p:blic void c:sto2For2at(String pattern, do:ble val:e ) ,
Deci2alFor2at 2yFor2atter = new Deci2alFor2at(pattern);
String o:tp:t = 2yFor2atter.for2at(val:e);
Syste2.o:t.println(val:e + " " + pattern + " " + o:tp:t);
,

static p:blic void 2ain(String, args) ,

c:sto2For2at("###,###.###", 123456.789);
c:sto2For2at("###.##", 123456.789);
c:sto2For2at("000000.000", 123.78);
c:sto2For2at("###,###.###", 12345.67);
,
,

The output is:
123456.789 ###,###.### 123,456.789
123456.789 ###.## 123456.79
123.78 000000.000 000123.780
12345.67 ###,###.### 12,345.67
The Iollowing table explains each line oI output.
De.im,lFo7m,t.j,v, Cutput
Va|ue 9attern Cutput Lxp|anat|on
146

1343679 ######### 1343679
1he pound slgn (#) denoLes a dlglL Lhe comma ls a
placeholder for Lhe grouplng separaLor and Lhe perlod ls a
placeholder for Lhe declmal separaLor
1343679 ##### 1343679
1he val:ehas Lhree dlglLs Lo Lhe rlghL of Lhe declmal polnL
buL Lhepatternhas only Lwo 1hefor2atmeLhod handles
Lhls by roundlng up
137 000000000 0001370
1hepatternspeclfles leadlng and Lralllng zeros because
Lhe 0 characLer ls used lnsLead of Lhe pound slgn (#)
134367 $######### $134367
1he flrsL characLer ln Lhe patternls Lhe dollar slgn ($)
noLe LhaL lL lmmedlaLely precedes Lhe lefLmosL dlglL ln Lhe
formaLLedo:tp:t
eyond asic rithmetic
1he !ava programmlng language supporLs baslc arlLhmeLlc wlLh lLs arlLhmeLlc
operaLors + * / and 1he ath class ln Lhe java.lang package provldes meLhods
and consLanLs for dolng more advanced maLhemaLlcal compuLaLlon
The methods in the ath class are all static, so you call them directly Irom the class,
like this:
ath.cos(angle);

Note Dslng Lhe static i2port language feaLure you donL have Lo wrlLe ath ln
fronL of every maLh funcLlon
i2port static java.lang.ath.;
1hls allows you Lo lnvoke Lhe ath class meLhods by Lhelr slmple names or
example
cos(angle);

Constants and asic ethods
1he ath class lncludes Lwo consLanLs
147

O ath. whlch ls Lhe base of naLural logarlLhms and
O ath.PI whlch ls Lhe raLlo of Lhe clrcumference of a clrcle Lo lLs dlameLer
1he ath class also lncludes more Lhan 40 sLaLlc meLhods 1he followlng Lable llsLs a
number of Lhe baslc meLhods
as|c Math Methods
Method escr|pt|on
do:ble abs(do:ble d)
float abs(float f)
int abs(int i)
long abs(long lng)
eLurns Lhe absoluLe value of Lhe argumenL
do:ble ceil(do:ble d)
eLurns Lhe smallesL lnLeger LhaL ls greaLer Lhan or
equal Lo Lhe argumenL eLurned as a double
do:ble floor(do:ble d)
eLurns Lhe largesL lnLeger LhaL ls less Lhan or
equal Lo Lhe argumenL eLurned as a double
do:ble rint(do:ble d)
eLurns Lhe lnLeger LhaL ls closesL ln value Lo Lhe
argumenL eLurned as a double
long ro:nd(do:ble d)
int ro:nd(float f)
eLurns Lhe closesL long or lnL as lndlcaLed by Lhe
meLhods reLurn Lype Lo Lhe argumenL
do:ble 2in(do:ble arg1, do:ble arg2)
float 2in(float arg1, float arg2)
int 2in(int arg1, int arg2)
long 2in(long arg1, long arg2)
eLurns Lhe smaller of Lhe Lwo argumenLs
do:ble 2ax(do:ble arg1, do:ble arg2)
float 2ax(float arg1, float arg2)
int 2ax(int arg1, int arg2)
long 2ax(long arg1, long arg2)
eLurns Lhe larger of Lhe Lwo argumenLs
The Iollowing program, asicathDe2o , illustrates how to use some oI these methods:

p:blic class asicathDe2o ,
14

p:blic static void 2ain(String, args) ,
do:ble a = -191.635;
do:ble b = 43.74;
int c = 16, d = 45;

Syste2.o:t.printf("The absol:te val:e of %.3f is %.3f%n", a,
ath.abs(a));
Syste2.o:t.printf("The ceiling of %.2f is %.0f%n", b, ath.ceil(b));
Syste2.o:t.printf("The floor of %.2f is %.0f%n", b, ath.floor(b));
Syste2.o:t.printf("The rint of %.2f is %.0f%n", b, ath.rint(b));
Syste2.o:t.printf("The 2ax of %d and %d is %d%n",c, d, ath.2ax(c,
d));
Syste2.o:t.printf("The 2in of of %d and %d is %d%n",c, d, ath.2in(c,
d));


,
,
Peres Lhe ouLpuL from Lhls program
The absol:te val:e of -191.635 is 191.635
The ceiling of 43.74 is 44
The floor of 43.74 is 43
The rint of 43.74 is 44
The 2ax of 16 and 45 is 45
The 2in of 16 and 45 is 16
ponentiaI and Logarithmic ethods
1he nexL Lable llsLs exponenLlal and logarlLhmlc meLhods of Lhe ath class
Lxponent|a| and ogar|thm|c Methods
Method escr|pt|on
do:ble exp(do:ble d)
eLurns Lhe base of Lhe naLural logarlLhms e Lo
Lhe power of Lhe argumenL
do:ble log(do:ble d)
eLurns Lhe naLural logarlLhm of Lhe argumenL
do:ble pow(do:ble base, do:ble
exponent)
eLurns Lhe value of Lhe flrsL argumenL ralsed Lo
Lhe power of Lhe second argumenL
do:ble sqrt(do:ble d)
eLurns Lhe square rooL of Lhe argumenL
The Iollowing program, xponentialDe2o, displays the value oI e, then calls each oI
the methods listed in the previous table on arbitrarily chosen numbers:
149


p:blic class xponentialDe2o ,
p:blic static void 2ain(String, args) ,
do:ble x = 11.635;
do:ble y = 2.76;

Syste2.o:t.printf("The val:e of e is %.4f%n", ath.);
Syste2.o:t.printf("exp(%.3f) is %.3f%n", x, ath.exp(x));
Syste2.o:t.printf("log(%.3f) is %.3f%n", x, ath.log(x));
Syste2.o:t.printf("pow(%.3f, %.3f) is %.3f%n", x, y, ath.pow(x, y));
Syste2.o:t.printf("sqrt(%.3f) is %.3f%n", x, ath.sqrt(x));
,
,
Peres Lhe ouLpuL youll see when you runxponentialDe2o
The val:e of e is 2.7183
exp(11.635) is 112983.831
log(11.635) is 2.454
pow(11.635, 2.760) is 874.008
sqrt(11.635) is 3.411
Trigonometric ethods
1he ath class also provldes a collecLlon of LrlgonomeLrlc funcLlons whlch are
summarlzed ln Lhe followlng Lable 1he value passed lnLo each of Lhese meLhods ls
an angle expressed ln radlans ?ou can use Lhe toRadians meLhod Lo converL from
degrees Lo radlans
1r|gonometr|c Methods
Method escr|pt|on
do:ble sin(do:ble d)
eLurns Lhe slne of Lhe speclfled double value
do:ble cos(do:ble d)
eLurns Lhe coslne of Lhe speclfled double value
do:ble tan(do:ble d)
eLurns Lhe LangenL of Lhe speclfled double value
do:ble asin(do:ble d)
eLurns Lhe arcslne of Lhe speclfled double value
do:ble acos(do:ble d)
eLurns Lhe arccoslne of Lhe speclfled double
value
do:ble atan(do:ble d)
eLurns Lhe arcLangenL of Lhe speclfled double
130

value
do:ble atan2(do:ble y, do:ble x)
ConverLs recLangular coordlnaLes (x, y) Lo polar
coordlnaLe (r, theta) and reLurnstheta
do:ble toDegrees(do:ble d)
do:ble toRadians(do:ble d)
ConverLs Lhe argumenL Lo degrees or radlans
Here's a program, Trigono2etricDe2o , that uses each oI these methods to compute
various trigonometric values Ior a 45-degree angle:

p:blic class Trigono2etricDe2o ,
p:blic static void 2ain(String, args) ,
do:ble degrees = 45.0;
do:ble radians = ath.toRadians(degrees);

Syste2.o:t.for2at("The val:e of pi is %.4f%n", ath.PI);
Syste2.o:t.for2at("The sine of %.1f degrees is %.4f%n", degrees,
ath.sin(radians));
Syste2.o:t.for2at("The cosine of %.1f degrees is %.4f%n", degrees,
ath.cos(radians));
Syste2.o:t.for2at("The tangent of %.1f degrees is %.4f%n", degrees,
ath.tan(radians));
Syste2.o:t.for2at("The arcsine of %.4f is %.4f degrees %n",
ath.sin(radians),
ath.toDegrees(ath.asin(ath.sin(radians))));
Syste2.o:t.for2at("The arccosine of %.4f is %.4f degrees %n",
ath.cos(radians),
ath.toDegrees(ath.acos(ath.cos(radians))));
Syste2.o:t.for2at("The arctangent of %.4f is %.4f degrees %n",
ath.tan(radians),
ath.toDegrees(ath.atan(ath.tan(radians))));

,
,


1he ouLpuL of Lhls program ls as follows
The val:e of pi is 3.1416
The sine of 45.0 degrees is 0.7071
The cosine of 45.0 degrees is 0.7071
The tangent of 45.0 degrees is 1.0000
The arcsine of 0.7071 is 45.0000 degrees
The arccosine of 0.7071 is 45.0000 degrees
The arctangent of 1.0000 is 45.0000 degrees
131

Random Numbers
1he rando2() meLhod reLurns a pseudorandomly selecLed number beLween 00 and
10 1he range lncludes 00 buL noL 10 ln oLher words 0.0 <= ath.rando2() < 1.0
1o geL a number ln a dlfferenL range you can perform arlLhmeLlc on Lhe value
reLurned by Lhe random meLhod or example Lo generaLe an lnLeger beLween 0 and
9 you would wrlLe
int n:2ber = (int)(ath.rando2() 10);
8y mulLlplylng Lhe value by 10 Lhe range of posslble values becomes 0.0 <= n:2ber
< 10.0
Using ath.rando2 works well when you need to generate a single random number. II
you need to generate a series oI random numbers, you should create an instance
oI java.:til.Rando2 and invoke methods on that object to generate numbers.
5 Summary of Numbers
You use one oI the wrapper classes yte, Do:ble,Float, Integer, Long, or Short to
wrap a number oI primitive type in an object. The Java compiler automatically wraps
(boxes) primitives Ior you when necessary and unboxes them, again when necessary.
The :2ber classes include constants and useIul class methods.
The I*VLU and X*VLU constants contain the smallest and largest values that
can be contained by an object oI that type. The byteVal:e,shortVal:e, and similar
methods convert one numeric type to another. The val:eJf method converts a string to
a number, and the toString method converts a number to a string.
To Iormat a string containing numbers Ior output, you can use
the printf() or for2at() methods in thePrintStrea2 class. Alternatively, you can use
the:2berFor2at class to customize numerical Iormats using patterns.
The ath class contains a variety oI class methods Ior perIorming mathematical
Iunctions, including exponential, logarithmic, and trigonometric methods. ath also
includes basic arithmetic Iunctions, such as absolute value and rounding, and a
method, rando2(), Ior generating random numbers.
6 Questions and ercises: Numbers
Questions
1. Use the API documentation to Iind the answers to the Iollowing questions:
13

a. What Integer method can you use to convert an int into a string that
expresses the number in hexadecimal? For example, what method
converts the integer 65 into the string "41"?
b. What Integer method would you use to convert a string expressed in
base 5 into the equivalent int? For example, how would you convert the
string "230" into the integer value 65? $how the code you would use to
accomplish this task.
c. What Double method can you use to detect whether a Iloating-point
number has the special value Not a Number (a)?
2. What is the value oI the Iollowing expression, and why?
3. Integer.val:eJf(1).eq:als(Long.val:eJf(1))
ercises
1. Change axVariablesDe2o to show minimum values instead oI maximum
values. You can delete all code related to the variables aChar andaoolean.
What is the output?
2. Create a program that reads an unspeciIied number oI integer arguments Irom
the command line and adds them together. For example, suppose that you enter
the Iollowing:
3. java dder 1 3 2 10
The program should display 16 and then exit. The program should display an
error message iI the user enters only one argument. You can base your program
on Val:eJfDe2o .
4. Create a program that is similar to the previous one but has the Iollowing
diIIerences:
o lnsLead of readlng lnLeger argumenLs lL reads floaLlngpolnL argumenLs
o lL dlsplays Lhe sum of Lhe argumenLs uslng exacLly Lwo dlglLs Lo Lhe
rlghL of Lhe declmal polnL
For example, suppose that you enter the Iollowing:
java FPdder 1 1e2 3.0 4.754
The program would display 108.75. Depending on your locale, the decimal
point might be a comma (,) instead oI a period (.).
Check your answers.

133

7 Characters
,osL of Lhe Llme lf you are uslng a slngle characLer value you wlll use Lhe
prlmlLlve char Lype or example
char ch = 'a';
char :niChar = '\:039'; // Unicode for :ppercase Greek o2ega character
char, charrray =, 'a', 'b', 'c', 'd', 'e' ,; // an array of chars
1here are Llmes however when you need Lo use a char as an ob[ecLfor example
as a meLhod argumenL where an ob[ecL ls expecLed 1he !ava programmlng language
provldes a wtoppet class LhaL wraps Lhe char ln aCharacter ob[ecL for Lhls purpose
n ob[ecL of LypeCharacter conLalns a slngle fleld whose Lype ls char
1hls Character class also offers a number of useful class (le sLaLlc) meLhods for
manlpulaLlng characLers
You can create a Character object with theCharacter constructor:
Character ch = new Character('a');
1he !ava compller wlll also creaLe a Character ob[ecL for you under some
clrcumsLances or example lf you pass a prlmlLlve char lnLo a meLhod LhaL expecLs
an ob[ecL Lhe compller auLomaLlcally converLs Lhe char Lo aCharacter for you 1hls
feaLure ls called ootoboxloqor ooboxloq lf Lhe converslon goes Lhe oLher way
Here is an example oI boxing,
Character ch = 'a'; // the pri2itive char 'a' is boxed into the Character
object ch
and here ls an example of boLh boxlng and unboxlng
Character test(Character c) ,..., // 2ethod para2eter and ret:rn type =
Character object

char c = test('x'); // pri2itive 'x' is boxed for 2ethod test, ret:rn is
:nboxed to char 'c'

Note 1he Character class ls lmmuLable so LhaL once lL ls creaLed a Character ob[ecL
cannoL be changed

134

1he followlng Lable llsLs some of Lhe mosL useful meLhods ln Lhe Character class buL
ls noL exhausLlve or a compleLe llsLlng of all meLhods ln Lhls class (Lhere are more
Lhan 30) refer Lo Lhe java.lang.Characterl speclflcaLlon
Usefu| Methods |n the h,7,.te7 |ass
Method escr|pt|on
boolean isLetter(char
ch)
boolean isDigit(char
ch)
ueLermlnes wheLher Lhe speclfled char value ls a leLLer or a dlglL
respecLlvely
boolean
isWhitespace(char ch)
ueLermlnes wheLher Lhe speclfled char value ls whlLe space
boolean
isUpperCase(char ch)
boolean
isLowerCase(char ch)
ueLermlnes wheLher Lhe speclfled char value ls uppercase or lowercase
respecLlvely
char toUpperCase(char
ch)
char toLowerCase(char
ch)
eLurns Lhe uppercase or lowercase form of Lhe speclfled char value
toString(char ch)
eLurns a String ob[ecL represenLlng Lhe speclfled characLer value
LhaL ls a onecharacLer sLrlng
scape Se6uences
characLer preceded by a backslash () ls an escope sepoeoce and has speclal
meanlng Lo Lhe compller 1he followlng Lable shows Lhe !ava escape sequences
Lscape Sequences
Lscape
Sequence
escr|pt|on
\t
lnserL a Lab ln Lhe LexL aL Lhls
133

polnL
\b
lnserL a backspace ln Lhe LexL aL
Lhls polnL
\n
lnserL a newllne ln Lhe LexL aL Lhls
polnL
\r
lnserL a carrlage reLurn ln Lhe LexL
aL Lhls polnL
\f
lnserL a formfeed ln Lhe LexL aL
Lhls polnL
\'
lnserL a slngle quoLe characLer ln
Lhe LexL aL Lhls polnL
\"
lnserL a double quoLe characLer ln
Lhe LexL aL Lhls polnL
\\
lnserL a backslash characLer ln Lhe
LexL aL Lhls polnL
When an escape sequence is encountered in a print statement, the compiler interprets
it accordingly. For example, iI you want to put quotes within quotes you must use the
escape sequence, \", on the interior quotes. To print the sentence
She said "Hello!" to 2e.
you would wrlLe
Syste2.o:t.println("She said \"Hello!\" to 2e.");
Strings
SLrlngs whlch are wldely used ln !ava programmlng are a sequence of characLers ln
Lhe !ava programmlng language sLrlngs are ob[ecLs
The Java platIorm provides the String class to create and manipulate strings.
136

Creating Strings
1he mosL dlrecL way Lo creaLe a sLrlng ls Lo wrlLe
String greeting = "Hello world!";
ln Lhls case Pello world! ls a sttloq lltetola serles of characLers ln your code LhaL
ls enclosed ln double quoLes Whenever lL encounLers a sLrlng llLeral ln your code
Lhe compller creaLes a String ob[ecL wlLh lLs valueln Lhls case Hello world!
As with any other object, you can create Stringobjects by using the new keyword and
a constructor. The String class has thirteen constructors that allow you to provide the
initial value oI the string using diIIerent sources, such as an array oI characters:
char, hellorray = , 'h', 'e', 'l', 'l', 'o', '.',;
String helloString = new String(hellorray);
Syste2.o:t.println(helloString);
1he lasL llne of Lhls code snlppeL dlsplays hello

Note 1he String class ls lmmuLable so LhaL once lL ls creaLed a String ob[ecL
cannoL be changed 1he String class has a number of meLhods some of whlch wlll
be dlscussed below LhaL appear Lo modlfy sLrlngs Slnce sLrlngs are lmmuLable whaL
Lhese meLhods really do ls creaLe and reLurn a new sLrlng LhaL conLalns Lhe resulL of
Lhe operaLlon

String Length
,eLhods used Lo obLaln lnformaLlon abouL an ob[ecL are known as occessot
etboJs Cne accessor meLhod LhaL you can use wlLh sLrlngs ls Lhe length() meLhod
whlch reLurns Lhe number of characLers conLalned ln Lhe sLrlng ob[ecL fLer Lhe
followlng Lwo llnes of code have been execuLed len equals 17
String palindro2e = "Dot saw I was Tod";
int len = palindro2e.length();
polloJtoe ls a word or senLence LhaL ls symmeLrlclL ls spelled Lhe same forward
and backward lgnorlng case and puncLuaLlon Pere ls a shorL and lnefflclenL program
Lo reverse a pallndrome sLrlng lL lnvokes LheString meLhod chart(i) whlch reLurns
Lhe l
Lh
characLer ln Lhe sLrlng counLlng from 0

137

p:blic class StringDe2o ,
p:blic static void 2ain(String, args) ,
String palindro2e = "Dot saw I was Tod";
int len = palindro2e.length();
char, te2pCharrray = new charlen,;
char, charrray = new charlen,;

// p:t original string in an array of chars
for (int i = 0; i < len; i++) ,
te2pCharrrayi, = palindro2e.chart(i);
,

// reverse array of chars
for (int j = 0; j < len; j++) ,
charrrayj, = te2pCharrraylen - 1 - j,;
,

String reversePalindro2e = new String(charrray);
Syste2.o:t.println(reversePalindro2e);
,
,
unnlng Lhe program produces Lhls ouLpuL
doT saw I was toD
1o accompllsh Lhe sLrlng reversal Lhe program had Lo converL Lhe sLrlng Lo an array
of characLers (flrsL forloop) reverse Lhe array lnLo a second array (second forloop)
and Lhen converL back Lo a sLrlng 1he Stringclass lncludes a meLhod getChars() Lo
converL a sLrlng or a porLlon of a sLrlng lnLo an array of characLers so we could
replace Lhe flrsL for loop ln Lhe program above wlLh
palindro2e.getChars(0, len, te2pCharrray, 0);
Concatenating Strings
1he String class lncludes a meLhod for concaLenaLlng Lwo sLrlngs
string1.concat(string2);
1hls reLurns a new sLrlng LhaL ls sLrlng1 wlLh sLrlng added Lo lL aL Lhe end
You can also use the concat() method with string literals, as in:
"y na2e is ".concat("R:2plestiltskin");
SLrlngs are more commonly concaLenaLed wlLh Lhe +operaLor as ln
"Hello," + " world" + "!"
whlch resulLs ln
"Hello, world!"
1he + operaLor ls wldely used ln print sLaLemenLs or example
13

String string1 = "saw I was ";
Syste2.o:t.println("Dot " + string1 + "Tod");
whlch prlnLs
Dot saw I was Tod
Such a concaLenaLlon can be a mlxLure of any ob[ecLs or each ob[ecL LhaL ls noL
a String lLs toString()meLhod ls called Lo converL lL Lo a String

Note 1he !ava programmlng language does noL permlL llLeral sLrlngs Lo span llnes ln
source flles so you musL use Lhe + concaLenaLlon operaLor aL Lhe end of each llne ln
a mulLlllne sLrlng or example
String q:ote = "ow is the ti2e for all good " +
"2en to co2e to the aid of their co:ntry.";
8reaklng sLrlngs beLween llnes uslng Lhe +concaLenaLlon operaLor ls once agaln very
common ln print sLaLemenLs

Creating ormat Strings
?ou have seen Lhe use of Lhe printf() and for2at()meLhods Lo prlnL ouLpuL wlLh
formaLLed numbers 1heString class has an equlvalenL class meLhodfor2at() LhaL
reLurns a String ob[ecL raLher Lhan aPrintStrea2 ob[ecL
Using String's static for2at() method allows you to create a Iormatted string that
you can reuse, as opposed to a one-time print statement. For example, instead oI
Syste2.o:t.printf("The val:e of the float variable is %f, while the val:e of
the " +
"integer variable is %d, and the string is %s", floatVar,
intVar, stringVar);
you can wrlLe
String fs;
fs = String.for2at("The val:e of the float variable is %f, while the val:e of
the " +
"integer variable is %d, and the string is %s", floatVar,
intVar, stringVar);
Syste2.o:t.println(fs);


139

Converting etween Numbers and Strings
Converting Strings to Numbers
requenLly a program ends up wlLh numerlc daLa ln a sLrlng ob[ecLa value enLered
by Lhe user for example
The :2ber subclasses that wrap primitive numeric types
( yte, Integer, Do:ble, Float, Long, andShort) each provide a class method
named val:eJfthat converts a string to an object oI that type. Here is an
example, Val:eJfDe2o , that gets two strings Irom the command line, converts them to
numbers, and perIorms arithmetic operations on the values:

p:blic class Val:eJfDe2o ,
p:blic static void 2ain(String, args) ,

//this progra2 req:ires two arg:2ents on the co22and line
if (args.length == 2) ,
//convert strings to n:2bers
float a = (Float.val:eJf(args0,) ).floatVal:e();
float b = (Float.val:eJf(args1,) ).floatVal:e();

//do so2e arith2etic
Syste2.o:t.println("a + b = " + (a + b) );
Syste2.o:t.println("a - b = " + (a - b) );
Syste2.o:t.println("a b = " + (a b) );
Syste2.o:t.println("a / b = " + (a / b) );
Syste2.o:t.println("a % b = " + (a % b) );
, else ,
Syste2.o:t.println("This progra2 req:ires two co22and-line
arg:2ents.");
,
,
,
1he followlng ls Lhe ouLpuL from Lhe program when you use 4.5 and 87.2 for Lhe
commandllne argumenLs
a + b = 91.7
a - b = -82.7
a b = 392.4
a / b = 0.0516055
a % b = 4.5

Note Lach of Lhe :2ber subclasses LhaL wrap prlmlLlve numerlc Lypes also provldes
aparseXXXX() meLhod (for exampleparseFloat()) LhaL can be used Lo converL sLrlngs
Lo prlmlLlve numbers Slnce a prlmlLlve Lype ls reLurned lnsLead of an ob[ecL
160

LheparseFloat() meLhod ls more dlrecL Lhan Lheval:eJf() meLhod or example ln
LheVal:eJfDe2o program we could use
float a = Float.parseFloat(args0,);
float b = Float.parseFloat(args1,);

Converting Numbers to Strings
SomeLlmes you need Lo converL a number Lo a sLrlng because you need Lo operaLe
on Lhe value ln lLs sLrlng form 1here are several easy ways Lo converL a number Lo a
sLrlng
int i;
String s1 = "" + i; //Concatenate "i" with an e2pty string;
//conversion is handled for yo:.
or
String s2 = String.val:eJf(i); //The val:eJf class 2ethod.
Lach of Lhe :2ber subclasses lncludes a class meLhodtoString() LhaL wlll converL
lLs prlmlLlve Lype Lo a sLrlng or example
int i;
do:ble d;
String s3 = Integer.toString(i);
String s4 = Do:ble.toString(d);
1he ToStringDe2o example uses Lhe toStringmeLhod Lo converL a number Lo a sLrlng
1he program Lhen uses some sLrlng meLhods Lo compuLe Lhe number of dlglLs before
and afLer Lhe declmal polnL

p:blic class ToStringDe2o ,

p:blic static void 2ain(String, args) ,
do:ble d = 858.48;
String s = Do:ble.toString(d);

int dot = s.indexJf('.');

Syste2.o:t.println(dot + " digits before deci2al point.");
Syste2.o:t.println( (s.length() - dot - 1) +
" digits after deci2al point.");
,
,
1he ouLpuL of Lhls program ls
3 digits before deci2al point.
2 digits after deci2al point.
161

10 anipuIating Characters in a String
1he String class has a number of meLhods for examlnlng Lhe conLenLs of sLrlngs
flndlng characLers or subsLrlngs wlLhln a sLrlng changlng case and oLher Lasks
Getting Characters and Substrings by Inde
?ou can geL Lhe characLer aL a parLlcular lndex wlLhln a sLrlng by lnvoklng
Lhe chart() accessor meLhod 1he lndex of Lhe flrsL characLer ls 0 whlle Lhe lndex of
Lhe lasL characLer ls length()-1 or example Lhe followlng code geLs Lhe characLer
aL lndex 9 ln a sLrlng
String anotherPalindro2e = "iagara. J roar again!";
char aChar = anotherPalindro2e.chart(9);
lndlces begln aL 0 so Lhe characLer aL lndex 9 ls C as lllusLraLed ln Lhe followlng
flgure

II you want to get more than one consecutive character Irom a string, you can use
the s:bstring method. Thes:bstring method has two versions, as shown in the
Iollowing table:
1he subst7ing Methods |n the St7ing |ass
Method escr|pt|on
String s:bstring(int
beginIndex, int endIndex)
eLurns a new sLrlng LhaL ls a subsLrlng of Lhls sLrlng 1he flrsL
lnLeger argumenL speclfles Lhe lndex of Lhe flrsL characLer
1he second lnLeger argumenL ls Lhe lndex of Lhe lasL
characLer 1
String s:bstring(int
beginIndex)
eLurns a new sLrlng LhaL ls a subsLrlng of Lhls sLrlng 1he
lnLeger argumenL speclfles Lhe lndex of Lhe flrsL characLer
Pere Lhe reLurned subsLrlng exLends Lo Lhe end of Lhe
orlglnal sLrlng
16

The Iollowing code gets Irom the Niagara palindrome the substring that extends Irom
index 11 up to, but not including, index 15, which is the word "roar":
String anotherPalindro2e = "iagara. J roar again!";
String roar = anotherPalindro2e.s:bstring(11, 15);

Other ethods for anipuIating Strings
Pere are several oLher String meLhods for manlpulaLlng sLrlngs
Cther Methods |n the St7ing |ass for Man|pu|at|ng Str|ngs
Method escr|pt|on
String, split(String regex)
String, split(String regex, int
li2it)
Searches for a maLch as speclfled by Lhe sLrlng
argumenL (whlch conLalns a regular expresslon)
and spllLs Lhls sLrlng lnLo an array of sLrlngs
accordlngly 1he opLlonal lnLeger argumenL
speclfles Lhe maxlmum slze of Lhe reLurned array
egular expresslons are covered ln Lhe lesson
LlLled egular Lxpresslons
CharSeq:ence s:bSeq:ence(int
beginIndex, int endIndex)
eLurns a new characLer sequence consLrucLed
from beginIndex lndex up unLll endIndex 1
String tri2()
eLurns a copy of Lhls sLrlng wlLh leadlng and
Lralllng whlLe space removed
String toLowerCase()
String toUpperCase()
eLurns a copy of Lhls sLrlng converLed Lo
lowercase or uppercase lf no converslons are
necessary Lhese meLhods reLurn Lhe orlglnal
sLrlng
Searching for Characters and Substrings in a String
Pere are some oLher String meLhods for flndlng characLers or subsLrlngs wlLhln a
sLrlng 1he Stringclass provldes accessor meLhods LhaL reLurn Lhe poslLlon wlLhln Lhe
163

sLrlng of a speclflc characLer or subsLrlngindexJf() and lastIndexJf()
1he indexJf()meLhods search forward from Lhe beglnnlng of Lhe sLrlng and
Lhe lastIndexJf() meLhods search backward from Lhe end of Lhe sLrlng lf a
characLer or subsLrlng ls noL found indexJf() and lastIndexJf() reLurn 1
The String class also provides a search method,contains, that returns true iI the string
contains a particular character sequence. Use this method when you only need to
know that the string contains a character sequence, but the precise location isn't
important.
The Iollowing table describes the various string search methods.
1he Search Methods |n the St7ing |ass
Method escr|pt|on
int indexJf(int ch)
int lastIndexJf(int ch)
eLurns Lhe lndex of Lhe flrsL (lasL) occurrence of
Lhe speclfled characLer
int indexJf(int ch, int fro2Index)
int lastIndexJf(int ch, int fro2Index)
eLurns Lhe lndex of Lhe flrsL (lasL) occurrence of
Lhe speclfled characLer searchlng forward
(backward) from Lhe speclfled lndex
int indexJf(String str)
int lastIndexJf(String str)
eLurns Lhe lndex of Lhe flrsL (lasL) occurrence of
Lhe speclfled subsLrlng
int indexJf(String str, int fro2Index)
int lastIndexJf(String str, int
fro2Index)
eLurns Lhe lndex of Lhe flrsL (lasL) occurrence of
Lhe speclfled subsLrlng searchlng forward
(backward) from Lhe speclfled lndex
boolean contains(CharSeq:ence s)
eLurns Lrue lf Lhe sLrlng conLalns Lhe speclfled
characLer sequence

Note CharSeq:ence ls an lnLerface LhaL ls lmplemenLed by Lhe String class
1herefore you can use a sLrlng as an argumenL for Lhecontains() meLhod

164

RepIacing Characters and Substrings into a String
1he String class has very few meLhods for lnserLlng characLers or subsLrlngs lnLo a
sLrlng ln general Lhey are noL needed ?ou can creaLe a new sLrlng by concaLenaLlon
of subsLrlngs you have teoveJ from a sLrlng wlLh Lhe subsLrlng LhaL you wanL Lo
lnserL
The String class does have Iour methods Ior replacingIound characters or substrings,
however. They are:
Methods |n the St7ing |ass for Man|pu|at|ng Str|ngs
Method escr|pt|on
String replace(char oldChar, char
newChar)
eLurns a new sLrlng resulLlng from replaclng all
occurrences of oldChar ln Lhls sLrlng wlLh newChar
String replace(CharSeq:ence target,
CharSeq:ence replace2ent)
eplaces each subsLrlng of Lhls sLrlng LhaL maLches
Lhe llLeral LargeL sequence wlLh Lhe speclfled
llLeral replacemenL sequence
String replacell(String regex, String
replace2ent)
eplaces each subsLrlng of Lhls sLrlng LhaL maLches
Lhe glven regular expresslon wlLh Lhe glven
replacemenL
String replaceFirst(String regex,
String replace2ent)
eplaces Lhe flrsL subsLrlng of Lhls sLrlng LhaL
maLches Lhe glven regular expresslon wlLh Lhe
glven replacemenL
n ampIe
1he followlng class Filena2e lllusLraLes Lhe use oflastIndexJf() and s:bstring() Lo
lsolaLe dlfferenL parLs of a flle name

Note 1he meLhods ln Lhe followlng Filena2eclass donL do any error checklng and
assume LhaL Lhelr argumenL conLalns a full dlrecLory paLh and a fllename wlLh an
163

exLenslon lf Lhese meLhods were producLlon code Lhey would verlfy LhaL Lhelr
argumenLs were properly consLrucLed


p:blic class Filena2e ,
private String f:llPath;
private char pathSeparator, extensionSeparator;

p:blic Filena2e(String str, char sep, char ext) ,
f:llPath = str;
pathSeparator = sep;
extensionSeparator = ext;
,

p:blic String extension() ,
int dot = f:llPath.lastIndexJf(extensionSeparator);
ret:rn f:llPath.s:bstring(dot + 1);
,

p:blic String filena2e() , // gets filena2e witho:t extension
int dot = f:llPath.lastIndexJf(extensionSeparator);
int sep = f:llPath.lastIndexJf(pathSeparator);
ret:rn f:llPath.s:bstring(sep + 1, dot);
,

p:blic String path() ,
int sep = f:llPath.lastIndexJf(pathSeparator);
ret:rn f:llPath.s:bstring(0, sep);
,
,
Pere ls a program Filena2eDe2o LhaL consLrucLs aFilena2e ob[ecL and calls all of lLs
meLhods

p:blic class Filena2eDe2o ,
p:blic static void 2ain(String, args) ,
final String FPTH = "/ho2e/2e2/index.ht2l";
Filena2e 2yHo2ePage = new Filena2e(FPTH,
'/', '.');
Syste2.o:t.println("xtension = " +
2yHo2ePage.extension());
Syste2.o:t.println("Filena2e = " +
2yHo2ePage.filena2e());
Syste2.o:t.println("Path = " +
2yHo2ePage.path());
,
,
nd heres Lhe ouLpuL from Lhe program
xtension = ht2l
Filena2e = index
166

Path = /ho2e/2e2
s shown ln Lhe followlng flgure our extensionmeLhod uses lastIndexJf Lo locaLe
Lhe lasL occurrence of Lhe perlod () ln Lhe flle name 1hen s:bstring uses Lhe reLurn
value of lastIndexJf Lo exLracL Lhe flle name exLenslon LhaL ls Lhe subsLrlng from
Lhe perlod Lo Lhe end of Lhe sLrlng 1hls code assumes LhaL Lhe flle name has a perlod
ln lL lf Lhe flle name does noL have a perlodlastIndexJf reLurns 1 and Lhe subsLrlng
meLhod Lhrows a StringIndexJ:tJfo:ndsxception

Also, notice that the extension method uses dot + 1as the argument to s:bstring. II
the period character (.) is the last character oI the string, dot + 1 is equal to the length
oI the string, which is one larger than the largest index into the string (because indices
start at 0). This is a legal argument to s:bstring because that method accepts an index
equal to, but not greater than, the length oI the string and interprets it to mean "the end
oI the string."
11 Comparing Strings and Portions of Strings
The String class has a number oI methods Ior comparing strings and portions oI
strings. The Iollowing table lists these methods.
Methods for Comparing Strings
Method escription
boolean endsWith(String s:ffix)
boolean startsWith(String prefix)
Returns tr:e iI this string ends with or begins
with the substring speciIied as an argument to
the method.
boolean startsWith(String prefix, int
offset)
Considers the string beginning at the
indexoffset, and returns tr:e iI it begins with
the substring speciIied as an argument.
int co2pareTo(String anotherString)
Compares two strings lexicographically.
Returns an integer indicating whether this
string is greater than (result is ~ 0), equal to
(result is 0), or less than (result is 0) the
argument.
167

int co2pareToIgnoreCase(String str)
Compares two strings lexicographically,
ignoring diIIerences in case. Returns an integer
indicating whether this string is greater than
(result is ~ 0), equal to (result is 0), or less
than (result is 0) the argument.
boolean eq:als(Jbject anJbject)
Returns tr:e iI and only iI the argument is
aString object that represents the same
sequence oI characters as this object.
boolean eq:alsIgnoreCase(String
anotherString)
Returns tr:e iI and only iI the argument is
aString object that represents the same
sequence oI characters as this object, ignoring
diIIerences in case.
boolean regionatches(int toffset,
String other, int ooffset, int len)
Tests whether the speciIied region oI this string
matches the speciIied region oI the $tring
argument.
Region is oI length len and begins at the
index toffsetIor this string andooffset Ior
the other string.
boolean regionatches(boolean
ignoreCase, int toffset, String other,
int ooffset, int len)
Tests whether the speciIied region oI this string
matches the speciIied region oI the $tring
argument.
Region is oI length len and begins at the
index toffsetIor this string andooffset Ior
the other string.
The boolean argument indicates whether case
should be ignored; iI true, case is ignored when
comparing characters.
boolean 2atches(String regex)
Tests whether this string matches the speciIied
regular expression. Regular expressions are
discussed in the lesson titled "Regular
Expressions."
The Iollowing program, RegionatchesDe2o, uses theregionatches method to search
Ior a string within another string:

p:blic class RegionatchesDe2o ,
p:blic static void 2ain(String, args) ,
String searche = "Green ggs and Ha2";
16

String finde = "ggs";
int searcheLength = searche.length();
int findeLength = finde.length();
boolean fo:ndIt = false;
for (int i = 0; i <= (searcheLength - findeLength); i++) ,
if (searche.regionatches(i, finde, 0, findeLength)) ,
fo:ndIt = tr:e;
Syste2.o:t.println(searche.s:bstring(i, i +
findeLength));
break;
,
,
if (!fo:ndIt) Syste2.o:t.println("o 2atch fo:nd.");
,
,
The output Irom this program is ggs.
The program steps through the string reIerred to bysearche one character at a time.
For each character, the program calls the regionMatches method to determine whether
the substring beginning with the current character matches the string the program is
looking Ior.
12 The StringuiIder CIass
String:ilder ob[ecLs are llke String ob[ecLs excepL LhaL Lhey can be modlfled
lnLernally Lhese ob[ecLs are LreaLed llke varlablelengLh arrays LhaL conLaln a
sequence of characLers L any polnL Lhe lengLh and conLenL of Lhe sequence can be
changed Lhrough meLhod lnvocaLlons
$trings should always be used unless string builders oIIer an advantage in terms oI
simpler code (see the sample program at the end oI this section) or better
perIormance. For example, iI you need to concatenate a large number oI strings,
appending to a String:ilderobject is more eIIicient.
Length and Capacity
1he String:ilder class llke Lhe String class has alength() meLhod LhaL reLurns Lhe
lengLh of Lhe characLer sequence ln Lhe bullder
Unlike strings, every string builder also has a capacity, the number oI character spaces
that have been allocated. The capacity, which is returned by the capacity()method, is
always greater than or equal to the length (usually greater than) and will automatically
expand as necessary to accommodate additions to the string builder.
169

St7ingBuilde7 onstructors
onstructor escr|pt|on
String:ilder()
CreaLes an empLy sLrlng bullder wlLh a capaclLy of
16 (16 empLy elemenLs)
String:ilder(CharSeq:ence cs)
ConsLrucLs a sLrlng bullder conLalnlng Lhe same
characLers as Lhe speclfledCharSeq:ence plus an
exLra 16 empLy elemenLs Lralllng
LheCharSeq:ence
String:ilder(int initCapacity)
CreaLes an empLy sLrlng bullder wlLh Lhe speclfled
lnlLlal capaclLy
String:ilder(String s)
CreaLes a sLrlng bullder whose value ls lnlLlallzed
by Lhe speclfled sLrlng plus an exLra 16 empLy
elemenLs Lralllng Lhe sLrlng
For example, the Iollowing code
String:ilder sb = new String:ilder(); // creates e2pty b:ilder, capacity 16
sb.append("Greetings"); // adds 9 character string at beginning
wlll produce a sLrlng bullder wlLh a lengLh of 9 and a capaclLy of 16

The String:ilder class has some methods related to length and capacity that
the String class does not have:
ength and apac|ty Methods
Method escr|pt|on
170

void setLength(int newLength)
SeLs Lhe lengLh of Lhe characLer sequence
lfnewLength ls less Lhanlength() Lhe lasL
characLers ln Lhe characLer sequence are
LruncaLed lf newLengthls greaLer
Lhan length() null characLers are added aL Lhe
end of Lhe characLer sequence
void ens:reCapacity(int 2inCapacity)
Lnsures LhaL Lhe capaclLy ls aL leasL equal Lo Lhe
speclfled mlnlmum
A number oI operations (Ior example, append(),insert(), or setLength()) can
increase the length oI the character sequence in the string builder so that the
resultant length() would be greater than the currentcapacity(). When this happens,
the capacity is automatically increased.
StringuiIder Operations
1he prlnclpal operaLlons on a String:ilder LhaL are noL avallable ln String are
Lhe append() andinsert() meLhods whlch are overloaded so as Lo accepL daLa of any
Lype Lach converLs lLs argumenL Lo a sLrlng and Lhen appends or lnserLs Lhe
characLers of LhaL sLrlng Lo Lhe characLer sequence ln Lhe sLrlng bullder 1he append
meLhod always adds Lhese characLers aL Lhe end of Lhe exlsLlng characLer sequence
whlle Lhe lnserL meLhod adds Lhe characLers aL a speclfled polnL
Here are a number oI the methods oI theString:ilder class.
Var|ous St7ingBuilde7 Methods
Method escr|pt|on
String:ilder append(boolean b)
String:ilder append(char c)
String:ilder append(char, str)
String:ilder append(char, str, int
offset, int len)
String:ilder append(do:ble d)
String:ilder append(float f)
String:ilder append(int i)
String:ilder append(long lng)
String:ilder append(Jbject obj)
ppends Lhe argumenL Lo Lhls sLrlng bullder 1he
daLa ls converLed Lo a sLrlng before Lhe append
operaLlon Lakes place
171

String:ilder append(String s)
String:ilder delete(int start, int
end)
String:ilder deleteChart(int index)
1he flrsL meLhod deleLes Lhe subsequence from
sLarL Lo end1 (lncluslve) ln Lhe String:ilders
char sequence 1he second meLhod deleLes Lhe
characLer locaLed aLindex
String:ilder insert(int offset,
boolean b)
String:ilder insert(int offset, char
c)
String:ilder insert(int offset,
char, str)
String:ilder insert(int index, char,
str, int offset, int len)
String:ilder insert(int offset,
do:ble d)
String:ilder insert(int offset, float
f)
String:ilder insert(int offset, int
i)
String:ilder insert(int offset, long
lng)
String:ilder insert(int offset,
Jbject obj)
String:ilder insert(int offset,
String s)
lnserLs Lhe second argumenL lnLo Lhe sLrlng
bullder 1he flrsL lnLeger argumenL lndlcaLes Lhe
lndex before whlch Lhe daLa ls Lo be lnserLed 1he
daLa ls converLed Lo a sLrlng before Lhe lnserL
operaLlon Lakes place
String:ilder replace(int start, int
end, String s)
void setChart(int index, char c)
eplaces Lhe speclfled characLer(s) ln Lhls sLrlng
bullder
String:ilder reverse()
everses Lhe sequence of characLers ln Lhls sLrlng
bullder
String toString()
eLurns a sLrlng LhaL conLalns Lhe characLer
sequence ln Lhe bullder

Note ?ou can use any String meLhod on aString:ilder ob[ecL by flrsL converLlng
Lhe sLrlng bullder Lo a sLrlng wlLh Lhe toString()meLhod of Lhe String:ilder class
17

1hen converL Lhe sLrlng back lnLo a sLrlng bullder uslng Lhe String:ilder(String
str)consLrucLor

n ampIe
1he StringDe2o program LhaL was llsLed ln Lhe secLlon LlLled SLrlngs ls an example
of a program LhaL would be more efflclenL lf a String:ilder were used lnsLead of
a String
StringDe2o reversed a palindrome. Here, once again, is its listing:

p:blic class StringDe2o ,
p:blic static void 2ain(String, args) ,
String palindro2e = "Dot saw I was Tod";
int len = palindro2e.length();
char, te2pCharrray = new charlen,;
char, charrray = new charlen,;

// p:t original string in an array of chars
for (int i = 0; i < len; i++) ,
te2pCharrrayi, = palindro2e.chart(i);
,

// reverse array of chars
for (int j = 0; j < len; j++) ,
charrrayj, = te2pCharrraylen - 1 - j,;
,

String reversePalindro2e = new String(charrray);
Syste2.o:t.println(reversePalindro2e);
,
,
unnlng Lhe program produces Lhls ouLpuL
doT saw I was toD
1o accompllsh Lhe sLrlng reversal Lhe program converLs Lhe sLrlng Lo an array of
characLers (flrsL for loop) reverses Lhe array lnLo a second array (second forloop)
and Lhen converLs back Lo a sLrlng
II you convert the palindro2e string to a string builder, you can use
the reverse() method in theString:ilder class. It makes the code simpler and easier
to read:

p:blic class String:ilderDe2o ,
173

p:blic static void 2ain(String, args) ,
String palindro2e = "Dot saw I was Tod";

String:ilder sb = new String:ilder(palindro2e);

sb.reverse(); // reverse it

Syste2.o:t.println(sb);
,
,
unnlng Lhls program produces Lhe same ouLpuL
doT saw I was toD
noLe LhaL println() prlnLs a sLrlng bullder as ln
Syste2.o:t.println(sb);
because sb.toString() ls called lmpllclLly as lL ls wlLh any oLher ob[ecL ln
a println() lnvocaLlon

Note 1here ls also a String:ffer class LhaL ls exoctly Lhe same as
LheString:ilder class excepL LhaL lL ls Lhreadsafe by vlrLue of havlng lLs meLhods
synchronlzed 1hreads wlll be dlscussed ln Lhe lesson on concurrency
13 Summary of Characters and Strings
Most oI the time, iI you are using a single character value, you will use the
primitive char type. There are times, however, when you need to use a char as an
objectIor example, as a method argument where an object is expected. The Java
programming language provides a rapper class that "wraps" the char in
aCharacter object Ior this purpose. An object oI typeCharacter contains a single Iield
whose type is char. This Character class also oIIers a number oI useIul class (i.e.,
static) methods Ior manipulating characters.
$trings are a sequence oI characters and are widely used in Java programming. In the
Java programming language, strings are objects. The String class has over 60
methods and 13 constructors.
Most commonly, you create a string with a statement like
String s = "Hello world!";
rather than using one oI the String constructors.
174

The String class has many methods to Iind and retrieve substrings; these can then be
easily reassembled into new strings using the + concatenation operator.
The String class also includes a number oI utility methods, among
them split(), toLowerCase(),toUpperCase(), and val:eJf(). The latter method is
indispensable in converting user input strings to numbers. The :2ber subclasses also
have methods Ior converting strings to numbers and vice versa.
In addition to the String class, there is also aString:ilder class. Working
with String:ilderobjects can sometimes be more eIIicient than working with strings.
The String:ilder class oIIers a Iew methods that can be useIul Ior strings, among
themreverse(). In general, however, the String class has a wider variety oI methods.
A string can be converted to a string builder using aString:ilder constructor. A
string builder can be converted to a string with the toString() method.
1 Questions and ercises: Characters and Strings
Questions
1. What is the initial capacity oI the Iollowing string builder?
2. String:ilder sb = new String:ilder("ble was I ere I saw lba.");
3. Consider the Iollowing string:
4. String hannah = "Did Hannah see bees. Hannah did.";
a. What is the value displayed by the expression hannah.length()?
b. What is the value returned by the method call hannah.chart(12)?
c. Write an expression that reIers to the letterb in the string reIerred to
by hannah.
5. How long is the string returned by the Iollowing expression? What is the
string?
6. "Was it a car or a cat I saw.".s:bstring(9, 12)
7. In the Iollowing program, called Co2p:teRes:lt, what is the value
oI res:lt aIter each numbered line executes?
8. p:blic class Co2p:teRes:lt ,
9. p:blic static void 2ain(String, args) ,
10. String original = "software";
11. String:ilder res:lt = new String:ilder("hi");
12. int index = original.indexJf('a');
13.
14. /1/ res:lt.setChart(0, original.chart(0));
15. /2/ res:lt.setChart(1, original.chart(original.length()-1));
16. /3/ res:lt.insert(1, original.chart(4));
17. /4/ res:lt.append(original.s:bstring(1,4));
18. /5/ res:lt.insert(3, (original.s:bstring(index, index+2) + " "));
19.
20. Syste2.o:t.println(res:lt);
173

21. ,
22. ,
ercises
1. $how two ways to concatenate the Iollowing two strings together to get the
string "Hi, 2o2.":
2. String hi = "Hi, ";
3. String 2o2 = "2o2.";
4. Write a program that computes your initials Irom your Iull name and displays
them.
5. An anagram is a word or a phrase made by transposing the letters oI another
word or phrase; Ior example, "parliament" is an anagram oI "partial men," and
"soItware" is an anagram oI "swear oIt." Write a program that Iigures out
whether one string is an anagram oI another string. The program should ignore
white space and punctuation.
Check your answers.
























176

Generics

1. ntroduction
2. Generic Types
3. Generic Methods and Constructors
4. Type nference
5. Bounded Type Parameters
6. Subtyping
7. Wildcards
8. Type Erasure
9. Using Non-Reifiable Parameters with Varargs Methods
10. Summary of Generics
11. Questions and Exercises














177

Lesson: Generics
eoetlcs are a bullLln language feaLure LhaL wlll make your sofLware more rellable
1hls lesson dlscusses Lhe followlng Loplcs
Introduction
1hls secLlon explalns some common shorLcomlngs assoclaLed wlLh nongenerlc code
Speclflcally lL shows how cerLaln klnds of bugs wlll crash an appllcaLlon aL runLlme
slnce Lhey are noL deLecLable by Lhe compller
Generic Types
1hls secLlon explalns generlc Lype declaraLlons Lype varlables Lype parameLers and
Lype argumenLs lL also descrlbes Lhe namlng convenLlons LhaL are speclflc Lo
generlcs
Generic ethods and Constructors
1hls secLlon shows how Lype parameLers can be used Lo deflne generlc meLhods and
consLrucLors
Type Inference
1hls secLlon descrlbes Lype lnference whlch enables you Lo lnvoke a generlc meLhod
as you would an ordlnary meLhod wlLhouL speclfylng a Lype beLween angle brackeLs
ounded Type Parameters
1hls secLlon descrlbes how Lype parameLers can speclfy an upper bound LhaL llmlLs
Lhe klnd of Lypes LhaL can be passed ln
Subtyping
1hls secLlon descrlbes how generlc subLyplng dlffers from nongenerlc subLyplng
WiIdcards
1hls secLlon conLlnues Lhe dlscusslon of subLyplng by descrlblng bounded and
unbounded wlldcards
Type rasure
1hls secLlon descrlbes Lype erasure raw Lypes and unchecked warnlngs
17

&sing Non-ReifiabIe Parameters with Varargs ethods
1hls secLlon descrlbes Lhe consequences Lype erasure has LhaL are relaLed Lo varlable
argumenLs (also known as varargs) meLhods LhaL use generlc Lypes as parameLers
1 Introduction
ln any nonLrlvlal sofLware pro[ecL bugs are slmply a facL of llfe Careful plannlng
programmlng and LesLlng can help reduce Lhelr pervaslveness buL somehow
somewhere Lheyll always flnd a way Lo creep lnLo your code 1hls becomes
especlally apparenL as new feaLures are lnLroduced and your code base grows ln slze
and complexlLy
Fortunately, some bugs are easier to detect than others. Compile-time bugs, Ior
example, tell you immediately that something is wrong; you can use the compiler's
error messages to Iigure out what the problem is and Iix it, right then and there.
Runtime bugs, however, can be much more problematic; they don't always surIace
immediately, and when they do, it may be at a point in time that's Iar removed Irom
the actual cause oI the problem.
Generics add stability to your code by making more oI your bugs detectable at
compile time. $ome programmers choose to learn generics by studying the Java
Collections Framework; aIter all, generics areheavily used by those classes. However,
since we haven't yet covered collections, this chapter will Iocus primarily on simple
"collections-like" examples that we'll design Irom scratch. This hands-on approach
will teach you the necessary syntax and terminology while demonstrating the various
kinds oI problems that generics were designed to solve.
SimpIe o CIass
LeLs begln by deslgnlng a nongenerlc ox class LhaL operaLes on ob[ecLs of any Lype
lL need only provlde Lwo meLhods add whlch adds an ob[ecL Lo Lhe box andget
whlch reLrleves lL
p:blic class ox ,

private Jbject object;

p:blic void add(Jbject object) ,
this.object = object;
,

p:blic Jbject get() ,
ret:rn object;
,
179

,
Slnce lLs meLhods accepL or reLurn Jbject youre free Lo pass ln whaLever you wanL
provlded LhaL lLs noL one of Lhe prlmlLlve Lypes Powever should you need Lo
resLrlcL Lhe conLalned Lype Lo someLhlng speclflc (llkeInteger) your only opLlon
would be Lo speclfy Lhe requlremenL ln documenLaLlon (or ln Lhls case a commenL)
whlch of course Lhe compller knows noLhlng abouL

p:blic class oxDe2o1 ,

p:blic static void 2ain(String, args) ,

// JLY place Integer objects into this box!
ox integerox = new ox();

integerox.add(new Integer(10));
Integer so2eInteger = (Integer)integerox.get();
Syste2.o:t.println(so2eInteger);
,
,
1he oxDe2o1 program creaLes an Integer ob[ecL passes lL Lo add Lhen asslgns LhaL
same ob[ecL Loso2eInteger by Lhe reLurn value of get lL Lhen prlnLs Lhe ob[ecLs
value (10) Lo sLandard ouLpuL We know LhaL Lhe casL from Jbject Lo Integer ls
correcL because weve honored Lhe conLracL speclfled ln Lhe commenL 8uL
remember Lhe compller knows noLhlng abouL Lhls lL [usL LrusLs LhaL our casL ls
correcL urLhermore lL wlll do noLhlng Lo prevenL a careless programmer from
passlng ln an ob[ecL of Lhe wrong Lype such as String

p:blic class oxDe2o2 ,

p:blic static void 2ain(String, args) ,

// JLY place Integer objects into this box!
ox integerox = new ox();

// I2agine this is one part of a large application
// 2odified by one progra22er.
integerox.add("10"); // note how the type is now String

// ... and this is another, perhaps written
// by a different progra22er
Integer so2eInteger = (Integer)integerox.get();
Syste2.o:t.println(so2eInteger);
,
,
ln oxDe2o2 weve sLored Lhe number 10 as a String whlch could be Lhe case when
say a CDl collecLs lnpuL from Lhe user Powever Lhe exlsLlng casL
10

from JbjectLo Integer has mlsLakenly been overlooked 1hls ls clearly a bug buL
because Lhe code sLlll complles you wouldnL know anyLhlng ls wrong unLll runLlme
when Lhe appllcaLlon crashes wlLh a ClassCastxception
xception in thread "2ain"
java.lang.ClassCastxception:
java.lang.String cannot be cast to java.lang.Integer
at oxDe2o2.2ain(oxDe2o2.java:6)
II the ox class had been designed with generics in mind, this mistake would have
been caught by the compiler, instead oI crashing the application at runtime.
2 Generic Types
Let's update our ox class to use generics. We'll Iirst create a generic type
declarati4n by changing the code "p:blic class ox" to "p:blic class ox<T"; this
introduces one type variable, named T, that can be used anywhere inside the class.
This same technique can be applied to interIaces as well. There's nothing particularly
complex about this concept. In Iact, it's quite similar to what you already know about
variables in general. Just think oI T as a special kind oI variable, whose "value" will be
whatever type you pass in; this can be any class type, any interIace type, or even
another type variable. It just can't be any oI the primitive data types. In this context,
we also say that T is a 14rmal type parameter oI the ox class.
/
Generic version of the ox class.
para2 <T the type of val:e being boxed
/

p:blic class oxT> ,

private T t; // T stands for "Type"

p:blic void add(T t) ,
this.t = t;
,

p:blic T get() ,
ret:rn t;
,
,
As you can see, we've replaced all occurrences oIJbject with T.
Also note that a generic type may have multiple type parameters, but each parameter
must be unique within its declaring class or interIace. A declaration oI ox<T,T, Ior
11

example, would generate an error on the second occurrence oI T, but ox<T,U,
however, would be allowed.
To reIerence this generic class Irom within your own code, you must perIorm
a generic type inv4cati4n, which replaces T with some concrete value, such asInteger:
ox<Integer integerox;
You can think oI a generic type invocation as being similar to an ordinary method
invocation, but instead oI passing an argument to a method, you're passing a type
argument Integer in this case to the ox class itselI.
Like any other variable declaration, this code does not actually create a
new ox object. It simply declares thatintegerox will hold a reIerence to a
"ox oIInteger", which is how ox<Integer is read.
An invocation oI a generic type is generally known as aparameteri:ed type.
To instantiate this class, use the new keyword, as usual, but place <Integer between
the class name and the parenthesis:
integerox = new ox<Integer();
Or, you can put the entire statement on one line, such as:
ox<Integer integerox = new ox<Integer();
Once integerox is initialized, you're Iree to invoke itsget method without providing a
cast, as in
oxDe2o3

p:blic class oxDe2o3 ,

p:blic static void 2ain(String, args) ,
ox<Integer integerox = new ox<Integer();
integerox.add(new Integer(10));
Integer so2eInteger = integerox.get(); // no cast!
Syste2.o:t.println(so2eInteger);
,
,
Furthermore, iI you try adding an incompatible type to the box, such as String,
compilation will Iail, alerting you to what previously would have been a runtime bug:
1

oxDe2o3.java:5: add(java.lang.Integer) in ox<java.lang.Integer
cannot be applied to (java.lang.String)
integerox.add("10");
^
1 error
It's important to understand that type variables are not actually types themselves. In
the above examples, you won't Iind T.java or T.class anywhere on the Iile system.
Furthermore, T is not a part oI the ox class name. In Iact during compilation, all
generic inIormation will be removed entirely, leaving only ox.class on the Iile
system. We'll discuss this later in the section on
1ype Lrasure
The iamond
In Java $E 7 and later, you can replace the type arguments required to invoke the
constructor oI a generic class with an empty set oI type parameters (<) as long as the
compiler can determine, or inIer, the type arguments Irom the context. This pair oI
angle brackets,<, is inIormally called the diam4nd. For example, you can create an
instance oI ox<Integer with the Iollowing statement:
ox<Integer integerox = new ox<();
For more inIormation about the diamond, see Type InIerence and Instantiation oI
Generic Classes.
Type Parameter Naming Conventions
8y convenLlon Lype parameLer names are slngle uppercase leLLers 1hls sLands ln
sharp conLrasL Lo Lhe varlable namlng convenLlons LhaL you already know abouL and
wlLh good reason WlLhouL Lhls convenLlon lL would be dlfflculL Lo Lell Lhe dlfference
beLween a Lype varlable and an ordlnary class or lnLerface name
The most commonly used type parameter names are:
O L LlemenL (used exLenslvely by Lhe !ava CollecLlons ramework)
O k key
O n number
O 1 1ype
O I Ialue
O SDI eLc nd 3rd 4Lh Lypes
13

?oull see Lhese names used LhroughouL Lhe !ava SL l and Lhe resL of Lhls LuLorlal
3 Generic ethods and Constructors
Type parameters can also be declared within method and constructor signatures to
create generic meth4dsand generic c4nstruct4rs. 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.
/
This version introd:ces a generic 2ethod.
/
p:blic class ox<T ,

private T t;

p:blic void add(T t) ,
this.t = t;
,

p:blic T get() ,
ret:rn t;
,

publi. &> void inspe.t(& u){
System.out.p7intln("T: " + t.getl,ss().getN,me());
System.out.p7intln("&: " + u.getl,ss().getN,me());
}

p:blic static void 2ain(String, args) ,
ox<Integer integerox = new ox<Integer();
integerox.add(new Integer(10));
integerox.inspect("so2e text");
,
,
Here we've added one generic method, namedinspect, that deIines one type
parameter, named U. This method accepts an object and prints its type to standard
output. For comparison, it also prints out the type oI T. For convenience, this class
now also has a2ain method so that it can be run as an application.
The output Irom this program is:
T: java.lang.Integer
U: java.lang.String
By passing in diIIerent types, the output will change accordingly.
A more realistic use oI generic methods might be something like the Iollowing, which
deIines a static method that stuIIs reIerences to a single item into multiple boxes:
14

p:blic static <U void filloxes(U :, List<ox<U boxes) ,
for (ox<U box : boxes) ,
box.add(:);
,
,
To use this method, your code would look something like the Iollowing:
Crayon red = ...;
List<ox<Crayon crayonoxes = ...;
The complete syntax Ior invoking this method is:
ox.<Crayonfilloxes(red, crayonoxes);
Here we've explicitly provided the type to be used as U, but more oIten than not, this
can be leIt out and the compiler will inIer the type that's needed:
ox.filloxes(red, crayonoxes); // co2piler infers that U is Crayon
This Ieature, known as type in1erence, allows you to invoke a generic method as you
would an ordinary method, without speciIying a type between angle brackets.
Type Inference
The Iollowing topics are covered:
O 1ype lnference and Cenerlc ,eLhods
O 1ype lnference and lnsLanLlaLlon of Cenerlc Classes
O 1ype lnference and Cenerlc ConsLrucLors of Cenerlc and nonCenerlc Classes
Type Inference and Generic ethods
The section, Generic Methods and Constructors, introduced you to type inIerence,
which enables you to invoke a generic method as you would an ordinary method,
without speciIying a type between angle brackets. Consider the Iollowing
example, oxDe2o4, which requires the ox example and Java $E 7 or later:
p:blic class oxDe2o4 ,

p:blic static <U void addox(U :, java.:til.List<ox<U boxes) ,
ox<U box = new ox<();
box.add(:);
boxes.add(box);
,

p:blic static <U void o:tp:toxes(java.:til.List<ox<U boxes) ,
int co:nter = 0;
for (ox<U box: boxes) ,
U boxContents = box.get();
Syste2.o:t.println(
"ox #" + co:nter + " contains " +
boxContents.toString() + ",");
co:nter++;
,
,
13


p:blic static void 2ain(String, args) ,
java.:til.rrayList<ox<Integer listJfIntegeroxes =
new java.:til.rrayList<();
oxDe2o4.<Integeraddox(Integer.val:eJf(10), listJfIntegeroxes);
oxDe2o4.addox(Integer.val:eJf(20), listJfIntegeroxes);
oxDe2o4.addox(Integer.val:eJf(30), listJfIntegeroxes);
oxDe2o4.o:tp:toxes(listJfIntegeroxes);
,
,
The Iollowing is the output Irom this example:
ox #0 contains 10,
ox #1 contains 20,
ox #2 contains 30,
The generic method addox deIines one type parameter named U. Generally, the Java
compiler can inIer the type parameters oI a generic method call. Consequently, in
most cases, you do not have to speciIy them. For example, to call the generic
method addox, you can speciIy the type parameter as Iollows:
oxDe2o4.Intege7>addox(Integer.val:eJf(10), listJfIntegeroxes);
Alternatively, iI you omit the type parameters, the Java compiler automatically inIers
(Irom the method's arguments) that the type parameter is Integer:
oxDe2o4.addox(Integer.val:eJf(20), listJfIntegeroxes);
Type Inference and Instantiation of Generic CIasses
You can replace the type arguments required to invoke the constructor oI a generic
class with an empty set oI type parameters (<) as long as the compiler can inIer the
type arguments Irom the context. This pair oI angle brackets is inIormally called
the diam4nd.
For example, consider the Iollowing variable declaration:
ap<String, List<String 2yap = new Hashap<String, List<String();
In Java $E 7 and later, you can substitute the parameterized type oI the constructor
with an empty set oI type parameters (<):
ap<String, List<String 2yap = new Hashap<();
Note that to take advantage oI automatic type inIerence during generic class
instantiation, you must speciIy the diamond. In the Iollowing example, the compiler
16

generates an unchecked conversion warning because theHashap() constructor reIers
to the Hashap raw type, not the ap<String, List<String type:
ap<String, List<String 2yap = new Hashap(); // :nchecked conversion
warning
Java $E 7 and later support limited type inIerence Ior generic instance creation; you
can only use type inIerence iI the parameterized type oI the constructor is obvious
Irom the context. For example, the Iollowing example does not compile:
List<String list = new rrayList<();
list.add("");

// The following state2ent sho:ld fail since addll expects
// Collection<. extends String

list.addll(new rrayList<());
Note that the diamond oIten works in method calls; however, Ior greater clarity, it is
suggested that you use the diamond primarily to initialize a variable where it is
declared.
In comparison, the Iollowing example compiles:
// The following state2ents co2pile:

List<. extends String list2 = new rrayList<();
list.addll(list2);
Type Inference and Generic Constructors of Generic and Non-Generic
CIasses
Note that constructors can be generic (in other words, declare their own Iormal type
parameters) in both generic and non-generic classes. Consider the Iollowing example:
class yClass<X ,
<T yClass(T t) ,
// ...
,
,
Consider the Iollowing instantiation oI the classyClass, which is valid in Java $E 7
and prior releases:
new yClass<Integer("")
This statement creates an instance oI the parameterized type yClass<Integer; the
statement explicitly speciIies the type Integer Ior the Iormal type parameter, X, oI the
17

generic class yClass<X. Note that the constructor Ior this generic class contains a
Iormal type parameter, T. The compiler inIers the typeString Ior the Iormal type
parameter, T, oI the constructor oI this generic class (because the actual parameter oI
this constructor is a String object).
Compilers Irom releases prior to Java $E 7 are able to inIer the actual type parameters
oI generic constructors, similar to generic methods. However, compilers in Java $E 7
and later can inIer the actual type parameters 41 the generic class being instantiated iI
you use the diamond (<). Consider the Iollowing examples, which are valid Ior Java
$E 7 and later:
O yClass<Integer 2yJbject = new yClass<("");
In this example, the compiler inIers the typeInteger Ior the Iormal type
parameter, X, oI the generic class yClass<X. It inIers the typeString Ior the
Iormal type parameter, T, oI the constructor oI this generic class.
O yClass<Integer 2yJbject = new <String yClass<("");
In this example, the compiler inIers the typeInteger Ior the Iormal type
parameter, X, oI the generic class yClass<X. The statement explicitly speciIies
the type String Ior the Iormal type parameter, T, oI the constructor oI this
generic class.
5 ounded Type Parameters
There may be times when you'll want to restrict the kinds oI 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 oI :2beror its subclasses. This is what b4unded type
parametersare Ior.
To declare a bounded type parameter, list the type parameter's name, Iollowed by
the extends keyword, Iollowed by its upper b4und, which in this example is:2ber.
Note that, in this context, extends is used in a general sense to mean either "extends"
(as in classes) or "implements" (as in interIaces).
/
This version introd:ces a bo:nded type para2eter.
/
p:blic class ox<T ,

private T t;

p:blic void add(T t) ,
this.t = t;
1

,

p:blic T get() ,
ret:rn t;
,

p:blic <U extends Numbe7 void inspect(U :),
Syste2.o:t.println("T: " + t.getClass().geta2e());
Syste2.o:t.println("U: " + :.getClass().geta2e());
,

p:blic static void 2ain(String, args) ,
ox<Integer integerox = new ox<Integer();
integerox.add(new Integer(10));
integerox.inspect("so2e text"); // e77o7: this is still St7ing!
,
,
By modiIying our generic method to include this bounded type parameter,
compilation will now Iail, since our invocation oI inspect still includes a String:
ox.java:21: <Uinspect(U) in ox<java.lang.Integer cannot
be applied to (java.lang.String)
integerox.inspect("10");
^
1 error
To speciIy additional interIaces that must be implemented, use the & character, as in:
<U extends :2ber & yInterface
6 Subtyping
As you already know, it's possible to assign an object oI one type to an object oI
another type provided that the types are compatible. For example, you can assign
anInteger to an Jbject, since Jbject is one oIInteger's supertypes:
Jbject so2eJbject = new Jbject();
Integer so2eInteger = new Integer(10);
so2eJbject = so2eInteger; // JK
In object-oriented terminology, this is called an "is a" relationship. $ince an Integer is
a kind oI Jbject, the assignment is allowed. But Integer is also a kind oI:2ber, so
the Iollowing code is valid as well:
p:blic void so2eethod(:2ber n),
// 2ethod body o2itted
,

so2eethod(new Integer(10)); // JK
so2eethod(new Do:ble(10.1)); // JK
The same is also true with generics. You can perIorm a generic type invocation,
passing :2ber as its type argument, and any subsequent invocation oI add will be
allowed iI the argument is compatible with :2ber:
19

ox<:2ber box = new ox<:2ber();
box.add(new Integer(10)); // JK
box.add(new Do:ble(10.1)); // JK
Now consider the Iollowing method:
p:blic void boxTest(ox<:2ber n),
// 2ethod body o2itted
,
What type oI argument does it accept? By looking at its signature, we can see that it
accepts a single argument whose type is ox<:2ber. But what exactly does that
mean? Are you allowed to pass in ox<Integer orox<Do:ble, as you might expect?
$urprisingly, the answer is "no", because ox<Integer andox<Do:ble are not
subtypes oI ox<:2ber.
Understanding why becomes much easier iI you think oI tangible objects things
you can actually picture such as a cage:
// cage is a collection of things, with bars to keep the2 in.
interface Cage< extends Collection<;

ote: The Collection interIace is the root interIace oI the c4llecti4n hierarchy; it
represents a group oI objects. $ince a cage would be used Ior holding a collection oI
objects (the animals), it makes sense to include it in this example.

A lion is a kind oI animal, so Lion would be a subtype oI ni2al:
interface Lion extends ni2al ,,
Lion king = ...;
Where we need some animal, we're Iree to provide a lion:
ni2al a = king;
A lion can oI course be put into a lion cage:
Cage<Lion lionCage = ...;
lionCage.add(king);
and a butterIly into a butterIly cage:
interface :tterfly extends ni2al ,,
:tterfly 2onarch = ...;
Cage<:tterfly b:tterflyCage = ...;
b:tterflyCage.add(2onarch);
190

But what about an "animal cage"? English is ambiguous, so to be precise let's assume
we're talking about an "all-animal cage":
Cage<ni2al ani2alCage = ...;
This is a cage designed to hold all kinds oI animals, mixed together. It must have bars
strong enough to hold in the lions, and spaced closely enough to hold in the
butterIlies. $uch a cage might not even be Ieasible to build, but iI it is, then:
ani2alCage.add(king);
ani2alCage.add(2onarch);
$ince a lion is a kind oI animal (Lion is a subtype oIni2al), the question then
becomes, "Is a lion cage a kind oI animal cage? Is Cage<Lion a subtype
oICage<ni2al?". By the above deIinition oI animal cage, the answer must be "no".
This is surprising! But it makes perIect sense when you think about it: A lion cage
cannot be assumed to keep in butterIlies, and a butterIly cage cannot be assumed to
hold in lions. ThereIore, neither cage can be considered an "all-animal" cage:
ani2alCage = lionCage; // co2pile-ti2e error
ani2alCage = b:tterflyCage; // co2pile-ti2e error
Without generics, the animals could be placed into the wrong kinds oI cages, where it
would be possible Ior them to escape.
7 WiIdcards
Earlier we mentioned that English is ambiguous. The phrase "animal cage" can
reasonably mean "all-animal cage", but it also suggests an entirely diIIerent concept: a
cage designed not Ior any kind oI animal, but rather Iors4me kind oI animal whose
type is unknown. In generics, an unknown type is represented by
the ildcardcharacter ".".
To speciIy a cage capable oI holding s4me kind oI animal:
Cage<. extends ni2al so2eCage = ...;
Read ". extends ni2al" as "an unknown type that is a subtype oI ni2al,
possibly ni2al itselI", which boils down to "some kind oI animal". This is an
example oI a b4unded ildcard, where ni2al Iorms the upper b4und oI the expected
type. II you're asked Ior a cage that simply holds s4me kind oI animal, you're Iree to
provide a lion cage 4r a butterIly cage.

ote: It's also possible to speciIy a l4er b4und by using the s:per keyword instead
oI extends. The code<. s:per ni2al, thereIore, would be read as "an unknown type
that is a supertype oI ni2al, possiblyni2al itselI". You can also speciIy an unknown
191

type with an unb4unded ildcard, which simply looks like<.. An unbounded
wildcard is essentially the same as saying <. extends Jbject.

While Cage<Lion and Cage<:tterfly are not subtypes oI Cage<ni2al, they are in
Iact subtypes oICage<. extends ni2al:
so2eCage = lionCage; // JK
so2eCage = b:tterflyCage; // JK
$o now the question becomes, "Can you add butterIlies and lions directly
to so2eCage?". As you can probably guess, the answer to this question is "no".
so2eCage.add(king); // co2piler-ti2e error
so2eCage.add(2onarch); // co2piler-ti2e error
II so2eCage is a butterIly cage, it would hold butterIlies just Iine, but the lions would
be able to break Iree. II it's a lion cage, then all would be well with the lions, but the
butterIlies would Ily away. $o iI you can't put anything at all into so2eCage, is it
useless? No, because you can still read its contents:
void feedni2als(Cage<. extends ni2al so2eCage) ,
for (ni2al a : so2eCage)
a.feede();
,
ThereIore, you could house your animals in their individual cages, as shown earlier,
and invoke this method Iirst Ior the lions and then Ior the butterIlies:
feedni2als(lionCage);
feedni2als(b:tterflyCage);
Or, you could choose to combine your animals in the all-animal cage instead:
feedni2als(ani2alCage);
Type rasure
When a generic type is instantiated, the compiler translates those types by a technique
called type erasure a process where the compiler removes all inIormation related to
type parameters and type arguments within a class or method. Type erasure enables
Java applications that use generics to maintain binary compatibility with Java libraries
and applications that were created beIore generics.
For instance, ox<String is translated to type ox, which is called the ra type a
raw type is a generic class or interIace name without any type arguments. This means
that you can't Iind out what type oI Jbject a generic class is using at runtime. The
Iollowing operations are not possible:
p:blic class yClass< ,
p:blic static void 2yethod(Jbject ite2) ,
if (item inst,n.eo1 E) , //Co2piler error
...
,
19

E item2 = new E(); //Co2piler error
E[] iA77,y = new E[10]; //Co2piler error
E obj = (E)new Obje.t(); //Unchecked cast warning
,
,
The operations shown in bold are meaningless at runtime because the compiler
removes all inIormation about the actual type argument (represented by the type
parameter) at compile time.
Type erasure exists so that new code may continue to interIace with legacy code.
Using a raw type Ior any other reason is considered bad programming practice and
should be avoided whenever possible.
When mixing legacy code with generic code, you may encounter warning messages
similar to the Iollowing:
ote: WarningDe2o.java :ses :nchecked or :nsafe operations.
ote: Reco2pile with -Xlint::nchecked for details.
This can happen when using an older API that operates on raw types, as shown in the
Iollowing WarningDe2oprogram:

p:blic class WarningDe2o ,
p:blic static void 2ain(String, args),
ox<Integer bi;
bi = createox();
,

static ox createox(),
ret:rn new ox();
,
,
Recompiling with -Xlint::nchecked reveals the Iollowing additional inIormation:
WarningDe2o.java:4: warning: :nchecked, :nchecked conversion
fo:nd : ox
req:ired: ox<java.lang.Integer
bi = createox();
^
1 warning
&sing Non-ReifiabIe Parameters with Varargs ethods
The section Type Erasure discusses the process where the compiler removes
inIormation related to type parameters and type arguments to ensure binary
compatibility with Java libraries and applications that were created beIore generics.
193

Type erasure has consequences related to variable arguments (also known as varargs)
methods whose varargs Iormal parameter has a non-reiIiable type. $ee the
section Arbitrary Number oI Arguments in Passing InIormation to a Method or a
Constructor Ior more inIormation about varargs methods.
This page covers the Iollowing topics:
O Peap olluLlon
O Iarlable rgumenLs ,eLhods and nonelflable ormal arameLers
O oLenLlal IulnerablllLles of Iarargs ,eLhods wlLh nonelflable ormal
arameLers
O Suppresslng Warnlngs from Iarargs ,eLhods wlLh nonelflable ormal
arameLers
Heap PoIIution
Most parameterized types, such asrrayList<:2ber and List<String, are n4n-
rei1iable types. A non-reiIiable type is a type that is n4tcompletely available at
runtime. At compile time, non-reiIiable types undergo a process called type erasure
during which the compiler removes inIormation related to type parameters and type
arguments. This ensures binary compatibility with Java libraries and applications that
were created beIore generics. Because type erasure removes inIormation Irom
parameterized types at compile-time, these types are non-reiIiable.
Heap p4lluti4n occurs when a variable oI a parameterized type reIers to an object that
is not oI that parameterized type. This situation can only occur iI the program
perIormed some operation that would give rise to an unchecked warning at compile-
time. Anunchecked arning is generated iI, either at compile-time (within the limits
oI the compile-time type checking rules) or at runtime, the correctness oI an operation
involving a parameterized type (Ior example, a cast or method call) cannot be veriIied.
Consider the Iollowing example:
List l = new rrayList<:2ber();
List<String ls = l; // :nchecked warning
l.add(0, new Integer(42)); // another :nchecked warning
String s = ls.get(0); // ClassCastxception is thrown
During type erasure, the
types rrayList<:2berand List<String become rrayList and List, respectively.
The variable ls has the parameterized typeList<String. When the List reIerenced
by l is assigned to ls, the compiler generates an unchecked warning; the compiler is
194

unable to determine at compile time, and moreover knows that the JVM will not be
able to determine at runtime, iI l reIers to a List<Stringtype; it does not.
Consequently, heap pollution occurs.
As a result, at compile time, the compiler generates another unchecked warning at
the add statement. The compiler is unable to determine iI the variable l reIers to
a List<String type or a List<Integer type (and another heap pollution situation
occurs). However, the compiler does not generate a warning or error at
the getstatement. This statement is valid; it is calling theList<String.get method to
retrieve a Stringobject. Instead, at runtime, the get statement throws
aClassCastxception.
In detail, a heap pollution situation occurs when the Listobject l, whose static type
is List<:2ber, is assigned to another List object, ls, that has a diIIerent static
type, List<String. However, the compiler still allows this assignment. It must allow
this assignment to preserve backwards compatibility with versions oI Java $E that do
not support generics. Because oI type erasure, List<:2ber and List<String both
become List. Consequently, the compiler allows the assignment oI the object l, which
has a raw type oIList, to the object ls.
Furthermore, a heap pollution situation occurs when thel.add method is called. The
static type second Iormal parameter oI the add method is String, but this method is
called with an actual parameter oI a diIIerent type,Integer. However, the compiler
still allows this method call. Because oI type erasure, the type oI the second Iormal
parameter oI the add method (which is deIined asList<.add(int,))
becomes Jbject. Consequently, the compiler allows this method call because, aIter
type erasure, the l.add method can add any object oI type Jbject, including an object
oIInteger type, which is a subtype oI Jbject.
VariabIe rguments ethods and Non-ReifiabIe ormaI Parameters
Consider the method rray:ilder.addToList in the Iollowing example. It is a
variable arguments (also known as varargs) method that adds the objects oI
type Tcontained in the ele2ents varargs Iormal parameter to the List listrg:
i2port java.:til.;

p:blic class rray:ilder ,

p:blic static <T void addToList (List<T listrg, T... ele2ents) ,
for (T x : ele2ents) ,
listrg.add(x);
,
,
193


p:blic static void fa:ltyethod(List<String... l) ,
Jbject, objectrray = l; // Valid
objectrray0, = rrays.asList(new Integer(42));
String s = l0,.get(0); // ClassCastxception thrown here
,

,
i2port java.:til.;

p:blic class HeapPoll:tionxa2ple ,

p:blic static void 2ain(String, args) ,

List<String stringList = new rrayList<String();
List<String stringList = new rrayList<String();

rray:ilder.addToList(stringList, "Seven", "ight", "ine");
rray:ilder.addToList(stringList, "Ten", "leven", "Twelve");
List<List<String listJfStringLists = new rrayList<List<String();
rray:ilder.addToList(listJfStringLists, stringList, stringList);

rray:ilder.fa:ltyethod(rrays.asList("Hello!"),
rrays.asList("World!"));
,
,
The Java $E 7 compiler generates the Iollowing warning Ior the deIinition oI the
methodrray:ilder.addToList:
warning: varargs, Possible heap poll:tion fro2 para2eterized vararg type T
When the compiler encounters a varargs method, it translates the varargs Iormal
parameter into an array. However, the Java programming language does not permit
the creation oI arrays oI parameterized types. In the method rray:ilder.addToList,
the compiler translates the varargs Iormal parameter T... ele2entsto the Iormal
parameter T, ele2ents, an array. However, because oI type erasure, the compiler
converts the varargs Iormal parameter to Jbject, ele2ents. Consequently, there is a
possibility oI heap pollution. $ee the next section, Potential Vulnerabilities oI Varargs
Methods with Non-ReiIiable Formal Parameters, Ior more inIormation.
ote: The Java $E 5 and 6 compilers generate this warning when
the rray:ilder.addToList is called; in this example, the warning is generated Ior the
classHeapPoll:tionxa2ple. These compilers do not generate the warning at the
declaration site. However, the Java $E 7 generates the warning at both the declaration
site and the call site (unless the warnings are preempted with annotations;
see $uppressing Warnings Irom Varargs Methods with Non-ReiIiable Formal
Parameters Ior more inIormation). The advantage oI generating a warning when a
compiler encounters a varargs method that has a non-reiIiable varargs Iormal
196

parameter at the declaration site as opposed to the call site is that there is only one
declaration site; there are potentially many call sites.
PotentiaI VuInerabiIities of Varargs ethods with Non-ReifiabIe ormaI
Parameters
The method rray:ilder.fa:ltyethod shows why the compiler warns you about
these kinds oI methods. The Iirst statement oI this method assigns the varargs Iormal
parameter l to the Jbject arrayobjectrgs:
Jbject, objectrray = l;
This statement can potentially introduce heap pollution. A value that does match the
parameterized type oI the varargs Iormal parameter l can be assigned to the
variable objectrray, and thus can be assigned to l. However, the compiler does not
generate an unchecked warning at this statement. The compiler has already generated
a warning when it translated the varargs Iormal parameter List<String... l to the
Iormal parameter List, l. This statement is valid; the variable l has the type List,,
which is a subtype oIJbject,.
Consequently, the compiler does not issue a warning or error iI you assign
a List object oI any type to any array component oI the objectrray array as shown
by this statement:
objectrray0, = rrays.asList(new Integer(42));
This statement assigns to the Iirst array component oI theobjectrray array with
a List object that contains one object oI type Integer.
$uppose you call the rray:ilder.2akerraymethod with the Iollowing statement:
rray:ilder.fa:ltyethod(rrays.asList("Hello!"),
rrays.asList("World!"));
At runtime, the JVM throws a ClassCastxception at the Iollowing statement:
String s = l0,.get(0); // ClassCastxception thrown here
The object stored in the Iirst array component oI the variable l has the
type List<Integer, but this statement is expecting an object oI type List<String.
197

Suppressing Warnings from Varargs ethods with Non-ReifiabIe ormaI
Parameters
II you declare a varargs method that has parameterized parameters, and you ensure
that the body oI the method does not throw a ClassCastxception or other similar
exception due to improper handling oI the varargs Iormal parameter (as shown in
therray:ilder.fa:ltyethod method), you can suppress the warning that the
compiler generates Ior these kinds oI varargs methods by using one oI the Iollowing
options:
O Add the Iollowing annotation to static and non-constructor method
declarations:
O SafeVarargs
The SafeVarargs annotation is a documented part oI the method's contract; this
annotation asserts that the implementation oI the method will not improperly
handle the varargs Iormal parameter.
O Add the Iollowing annotation to the method declaration:
O S:ppressWarnings(,":nchecked", "varargs",)
Unlike the SafeVarargs annotation, theS:ppressWarnings("varargs") does
not suppress warnings generated Irom the method's call site.
O Use the compiler option -Xlint:-varargs.
For example, the Iollowing version oI therray:ilder class has two additional
methods,addToList2 and addToList3:
p:blic class rray:ilder ,

p:blic static <T void addToList (List<T listrg, T... ele2ents) ,
for (T x : ele2ents) ,
listrg.add(x);
,
,

Supp7essW,7nings({"un.he.ed" "v,7,7gs"})
p:blic static <T void addToList2 (List<T listrg, T... ele2ents) ,
for (T x : ele2ents) ,
listrg.add(x);
,
,

S,1eV,7,7gs
p:blic static <T void addToList3 (List<T listrg, T... ele2ents) ,
for (T x : ele2ents) ,
19

listrg.add(x);
,
,

// ...

,
p:blic class HeapPoll:tionxa2ple ,

// ...

p:blic static void 2ain(String, args) ,

// ...

rray:ilder.addToList(listJfStringLists, stringList, stringList);
A77,yBuilde7.,ddToList2(listO1St7ingLists st7ingListA st7ingListB);
A77,yBuilde7.,ddToList3(listO1St7ingLists st7ingListA st7ingListB);

// ...

,
,
The Java compiler generates the Iollowing warnings Ior this example:
O ,ddToList
o L Lhe meLhods declaraLlon :nchecked, Possible heap poll:tion
fro2 para2eterized vararg type T
o When Lhe meLhod ls called :nchecked, :nchecked generic array
creation for varargs para2eter of type List<String,
O ,ddToList2 When Lhe meLhod ls called (no warnlng ls generaLed aL Lhe
meLhods declaraLlon):nchecked, :nchecked generic array creation for
varargs para2eter of type List<String,
O ,ddToList3 no warnlngs are generaLed elLher aL Lhe meLhods declaraLlon or
when lL ls called
ote: In Java $E 5 and 6, it is the responsibility oI the programmer who calls a
varargs method that has a non-reiIiable varargs Iormal parameter to determine
whether heap pollution would occur. However, iI this programmer did not write such
a method, he or she cannot easily determine this. In Java $E 7, it is the responsibility
oI the programmer who rites these kinds oI varargs methods to ensure that they
properly handle the varargs Iormal parameter and ensure heap pollution does not
occur.


199

10 Summary of Generics
This chapter described the Iollowing problem: We have aox class, written to be
generally useIul so it deals withJbjects. We need an instance that takes onlyIntegers.
The comments say that only Integers go in, so the programmer knows this (or should
know it), but the compiler doesn't know it. This means that the compiler can't catch
someone erroneously adding aString. When we read the value and cast it to
anInteger we'll get an exception, but that's not ideal since the exception may be Iar
removed Irom the bug in both space and time:
1. Debugging may be diIIicult, as the point in the code where the exception is
thrown may be Iar removed Irom the point in the code where the error is
located.
2. It's always better to catch bugs when compiling than when running.
$peciIically, you learned that generic type declarations can include one or more type
parameters; you supply one type argument Ior each type parameter when you use the
generic type. You also learned that type parameters can be used to deIine generic
methods and constructors. Bounded type parameters limit the kinds oI types that can
be passed into a type parameter; they can speciIy an upper bound only. Wildcards
represent unknown types, and they can speciIy an upper or lower bound. During
compilation, type erasure removes all generic inIormation Irom a generic class or
interIace, leaving behind only its raw type. It is possible Ior generic code and legacy
code to interact, but in many cases the compiler will emit a warning telling you to
recompile with special Ilags Ior more details.
For additional inIormation on this topic, see Generics by Gilad Bracha.
11 Questions and ercises: Generics
Questions
1. Consider the Iollowing classes:
2. p:blic class ni2alHo:se< ,
3. private ani2al;
4. p:blic void setni2al( x) ,
5. ani2al = x;
6. ,
7. p:blic getni2al() ,
8. ret:rn ani2al;
9. ,
10. ,
11.
12. p:blic class ni2al,
13. ,
00

14.
15. p:blic class Cat extends ni2al ,
16. ,
17.
18. p:blic class Dog extends ni2al ,
19. ,
For the Iollowing code snippets, identiIy whether the code:
o falls Lo complle
o complles wlLh a warnlng
o generaLes an error aL runLlme or
o none of Lhe above (complles and runs wlLhouL problem)
e. ni2alHo:se<ni2al ho:se = new ni2alHo:se<Cat();
f
g. ni2alHo:se<Dog ho:se = new ni2alHo:se<ni2al();
h
i. ni2alHo:se<. ho:se = new ni2alHo:se<Cat();
j. ho:se.setni2al(new Cat());
k
l. ni2alHo:se ho:se = new ni2alHo:se();
2. ho:se.setni2al(new Dog());
n
ercises
1. Design a class that acts as a library Ior the Iollowing kinds oI media: book,
video, and newspaper. Provide one version oI the class that uses generics and
one that does not. Feel Iree to use any additional APIs Ior storing and retrieving
the media.
Check your answers











01


G Packages

1. Creating and Using Packages
2. Creating a Package
3. Naming a Package
4. Using Package Members
5. Managing Source and Class Files
6. Summary of Creating and Using Packages
7. Questions and Exercises




























0

Lesson: Packages
This lesson explains how to bundle classes and interIaces into packages, how to
use classes that are in packages, and how to arrange your Iile system so that the
compiler can Iind your source Iiles.
1 Creating and &sing Packages
To make types easier to Iind and use, to avoid naming conIlicts, and to control access,
programmers bundle groups oI related types into packages.

efinition: A package is a grouping oI related types providing access protection and
name space management. Note that typesreIers to classes, interIaces, enumerations,
and annotation types. Enumerations and annotation types are special kinds oI classes
and interIaces, respectively, so types are oIten reIerred to in this lesson simply
as classes and inter1aces.

The types that are part oI the Java platIorm are members oI various packages that
bundle classes by Iunction: Iundamental classes are in java.lang, classes Ior reading
and writing (input and output) are in java.io, and so on. You can put your types in
packages too.
$uppose you write a group oI classes that represent graphic objects, such as circles,
rectangles, lines, and points. You also write an interIace, Draggable, that classes
implement iI they can be dragged with the mouse.
//in the Draggable.java file
p:blic interface Draggable ,
. . .
,

//in the Graphic.java file
p:blic abstract class Graphic ,
. . .
,

//in the Circle.java file
p:blic class Circle extends Graphic i2ple2ents Draggable ,
. . .
,

//in the Rectangle.java file
p:blic class Rectangle extends Graphic i2ple2ents Draggable ,
. . .
,

03

//in the Point.java file
p:blic class Point extends Graphic i2ple2ents Draggable ,
. . .
,

//in the Line.java file
p:blic class Line extends Graphic i2ple2ents Draggable ,
. . .
,
You should bundle these classes and the interIace in a package Ior several reasons,
including the Iollowing:
O You and other programmers can easily determine that these types are related.
O You and other programmers know where to Iind types that can provide
graphics-related Iunctions.
O The names oI your types won't conIlict with the type names in other
packages because the package creates a new namespace.
O You can allow types within the package to have unrestricted access to one
another yet still restrict access Ior types outside the package.
2 Creating a Package
To create a package, you choose a name Ior the package (naming conventions are
discussed in the next section) and put a package statement with that name at the top
oI every s4urce 1ile that contains the types (classes, interIaces, enumerations, and
annotation types) that you want to include in the package.
The package statement (Ior example, package graphics;) must be the Iirst line in the
source Iile. There can be only one package statement in each source Iile, and it
applies to all types in the Iile.

ote: II you put multiple types in a single source Iile, only one can be p:blic,
and it must have the same name as the source Iile. For example, you can
deIine p:blic class Circle in the Iile Circle.java, deIinep:blic interface
Draggable in the IileDraggable.java, deIine p:blic en:2 Dayin the
Iile Day.java, and so Iorth.
You can include non-public types in the same Iile as a public type (this is
strongly discouraged, unless the non-public types are small and closely related to
the public type), but only the public type will be accessible Irom outside oI the
package. All the top-level, non-public types will be package private.
04


II you put the graphics interIace and classes listed in the preceding section in a
package called graphics, you would need six source Iiles, like this:
//in the Draggable.java file
package graphics;
p:blic interface Draggable ,
. . .
,

//in the Graphic.java file
package graphics;
p:blic abstract class Graphic ,
. . .
,

//in the Circle.java file
package graphics;
p:blic class Circle extends Graphic i2ple2ents Draggable ,
. . .
,

//in the Rectangle.java file
package graphics;
p:blic class Rectangle extends Graphic i2ple2ents Draggable ,
. . .
,

//in the Point.java file
package graphics;
p:blic class Point extends Graphic i2ple2ents Draggable ,
. . .
,

//in the Line.java file
package graphics;
p:blic class Line extends Graphic i2ple2ents Draggable ,
. . .
,
II you do not use a package statement, your type ends up in an unnamed
package. Generally speaking, an unnamed package is only Ior small or
temporary applications or when you are just beginning the development
process. Otherwise, classes and interIaces belong in named packages.
3 Naming a Package
With programmers worldwide writing classes and interIaces using the Java
programming language, it is likely that many programmers will use the same name Ior
diIIerent types. In Iact, the previous example does just that: It deIines
a Rectangle class when there is already a Rectangle class in the java.awt package.
$till, the compiler allows both classes to have the same name iI they are in diIIerent
packages. The Iully qualiIied name oI each Rectangle class includes the package
03

name. That is, the Iully qualiIied name oI the Rectangle class in thegraphics package
is graphics.Rectangle, and the Iully qualiIied name oI the Rectangle class in
thejava.awt package is java.awt.Rectangle.
This works well unless two independent programmers use the same name Ior their
packages. What prevents this problem? Convention.
Naming Conventions
Package names are written in all lower case to avoid conIlict with the names oI
classes or interIaces.
Companies use their reversed Internet domain name to begin their package names
Ior example,co2.exa2ple.2ypackage Ior a package named2ypackage created by a
programmer at exa2ple.co2.
Name collisions that occur within a single company need to be handled by convention
within that company, perhaps by including the region or the project name aIter the
company name (Ior example,co2.exa2ple.region.2ypackage).
Packages in the Java language itselI begin with java. orjavax.
In some cases, the internet domain name may not be a valid package name. This can
occur iI the domain name contains a hyphen or other special character, iI the package
name begins with a digit or other character that is illegal to use as the beginning oI a
Java name, or iI the package name contains a reserved Java keyword, such as "int". In
this event, the suggested convention is to add an underscore. For example:
ega||z|ng 9ackage Names
oma|n Name 9ackage Name 9ref|x
hyphenated-na2e.exa2ple.org org.exa2ple.hyphenated*na2e
exa2ple.int int*.exa2ple
123na2e.exa2ple.co2 co2.exa2ple.*123na2e
&sing Package embers
1he Lypes LhaL comprlse a package are known as Lhepockoqe ebets
06

To use a p:blic package member Irom outside its package, you must do one oI the
Iollowing:
O efer Lo Lhe member by lLs fully quallfled name
O lmporL Lhe package member
O lmporL Lhe members enLlre package
Lach ls approprlaLe for dlfferenL slLuaLlons as explalned ln Lhe secLlons LhaL follow
Referring to a Package ember by Its QuaIified Name
So far mosL of Lhe examples ln Lhls LuLorlal have referred Lo Lypes by Lhelr slmple
names such as Rectangle andStackJfInts ?ou can use a package members slmple
name lf Lhe code you are wrlLlng ls ln Lhe same package as LhaL member or lf LhaL
member has been lmporLed
However, iI you are trying to use a member Irom a diIIerent package and that package
has not been imported, you must use the member's Iully qualiIied name, which
includes the package name. Here is the Iully qualiIied name Ior the Rectangle class
declared in thegraphics package in the previous example.
graphics.Rectangle
?ou could use Lhls quallfled name Lo creaLe an lnsLance of graphics.Rectangle
graphics.Rectangle 2yRect = new graphics.Rectangle();
Cuallfled names are all rlghL for lnfrequenL use When a name ls used repeLlLlvely
however Lyplng Lhe name repeaLedly becomes Ledlous and Lhe code becomes
dlfflculL Lo read s an alLernaLlve you can lpott Lhe member or lLs package and
Lhen use lLs slmple name
Importing a Package ember
1o lmporL a speclflc member lnLo Lhe currenL flle puL ani2port sLaLemenL aL Lhe
beglnnlng of Lhe flle before any Lype deflnlLlons buL afLer Lhe package sLaLemenL lf
Lhere ls one Peres how you would lmporL Lhe Rectangleclass from
Lhe graphics package creaLed ln Lhe prevlous secLlon
i2port graphics.Rectangle;
now you can refer Lo Lhe Rectangle class by lLs slmple name
Rectangle 2yRectangle = new Rectangle();
07

1hls approach works well lf you use [usL a few members from Lhe graphics package
8uL lf you use many Lypes from a package you should lmporL Lhe enLlre package
Importing an ntire Package
1o lmporL all Lhe Lypes conLalned ln a parLlcular package use Lhe i2port sLaLemenL
wlLh Lhe asLerlsk () wlldcard characLer
i2port graphics.;
now you can refer Lo any class or lnLerface ln Lhegraphics package by lLs slmple
name
Circle 2yCircle = new Circle();
Rectangle 2yRectangle = new Rectangle();
1he asLerlsk ln Lhe i2port sLaLemenL can be used only Lo speclfy all Lhe classes wlLhln
a package as shown here lL cannoL be used Lo maLch a subseL of Lhe classes ln a
package or example Lhe followlng does noL maLch all Lhe classes ln
Lhe graphics package LhaL begln wlLh
i2port graphics.; //does not work
lnsLead lL generaLes a compller error WlLh Lhe i2portsLaLemenL you generally
lmporL only a slngle package member or an enLlre package

Note noLher less common form of i2portallows you Lo lmporL Lhe publlc nesLed
classes of an encloslng class or example lf Lhegraphics.Rectangle class conLalned
useful nesLed classes such asRectangle.Do:bleWide andRectangle.Sq:are you could
lmporLRectangle and lLs nesLed classes by uslng Lhe followlng two sLaLemenLs
i2port graphics.Rectangle;
i2port graphics.Rectangle.;
8e aware LhaL Lhe second lmporL sLaLemenL wllloot lmporL Rectangle
Another less common Iorm oI i2port, thestatic imp4rt statement, will be discussed at
the end oI this section.

or convenlence Lhe !ava compller auLomaLlcally lmporLs Lhree enLlre packages for
each source flle (1) Lhe package wlLh no name () Lhe java.lang package and (3)
Lhe currenL package (Lhe package for Lhe currenL flle)
0

pparent Hierarchies of Packages
L flrsL packages appear Lo be hlerarchlcal buL Lhey are noL or example Lhe !ava
l lncludes a java.awtpackage a java.awt.color package ajava.awt.font package
and many oLhers LhaL begln wlLh java.awt Powever Lhe java.awt.colorpackage
Lhe java.awt.font package and oLherjava.awt.xxxx packages are oot locloJeJ ln
Lhejava.awt package 1he preflx java.awt (Lhe !ava bsLracL Wlndow 1oolklL) ls used
for a number of relaLed packages Lo make Lhe relaLlonshlp evldenL buL noL Lo show
lncluslon
Importing java.awt. imports all oI the types in thejava.awt package, but it d4es n4t
imp4rtjava.awt.color, java.awt.font, or any otherjava.awt.xxxx packages. II you
plan to use the classes and other types in java.awt.color as well as those in java.awt,
you must import both packages with all their Iiles:
i2port java.awt.;
i2port java.awt.color.;
Name mbiguities
lf a member ln one package shares lLs name wlLh a member ln anoLher package and
boLh packages are lmporLed you musL refer Lo each member by lLs quallfled name
or example Lhe graphics package deflned a class named Rectangle
1he java.awt package also conLalns a Rectangle class lf
boLh graphics andjava.awt have been lmporLed Lhe followlng ls amblguous
Rectangle rect;
ln such a slLuaLlon you have Lo use Lhe members fully quallfled name Lo lndlcaLe
exacLly whlch Rectangleclass you wanL or example
graphics.Rectangle rect;
The Static Import Statement
1here are slLuaLlons where you need frequenL access Lo sLaLlc flnal flelds (consLanLs)
and sLaLlc meLhods from one or Lwo classes reflxlng Lhe name of Lhese classes over
and over can resulL ln cluLLered code 1he stotlc lpottsLaLemenL glves you a way Lo
lmporL Lhe consLanLs and sLaLlc meLhods LhaL you wanL Lo use so LhaL you do noL
need Lo preflx Lhe name of Lhelr class
The java.lang.ath class deIines the PI constant and many static methods, including
methods Ior calculating sines, cosines, tangents, square roots, maxima, minima,
exponents, and many more. For example,
09

p:blic static final do:ble PI 3.141592653589793
p:blic static do:ble cos(do:ble a)
Crdlnarlly Lo use Lhese ob[ecLs from anoLher class you preflx Lhe class name as
follows
do:ble r = ath.cos(ath.PI theta);
?ou can use Lhe sLaLlc lmporL sLaLemenL Lo lmporL Lhe sLaLlc members of
[avalang,aLh so LhaL you donL need Lo preflx Lhe class name ath 1he sLaLlc
members ofath can be lmporLed elLher lndlvldually
i2port st,ti. java.lang.ath.PI;
or as a group
i2port st,ti. java.lang.ath.;
Cnce Lhey have been lmporLed Lhe sLaLlc members can be used wlLhouL
quallflcaLlon or example Lhe prevlous code snlppeL would become
do:ble r = cos(PI theta);
Cbvlously you can wrlLe your own classes LhaL conLaln consLanLs and sLaLlc meLhods
LhaL you use frequenLly and Lhen use Lhe sLaLlc lmporL sLaLemenL or example
i2port st,ti. 2ypackage.yConstants.;

Note Dse sLaLlc lmporL very sparlngly Cveruslng sLaLlc lmporL can resulL ln code LhaL
ls dlfflculL Lo read and malnLaln because readers of Lhe code wonL know whlch class
deflnes a parLlcular sLaLlc ob[ecL Dsed properly sLaLlc lmporL makes code more
readable by removlng class name repeLlLlon
5 anaging Source and CIass iIes
,any lmplemenLaLlons of Lhe !ava plaLform rely on hlerarchlcal flle sysLems Lo
manage source and class flles alLhough @be Iovo looqooqe 5peclflcotloo does noL
requlre Lhls 1he sLraLegy ls as follows
Put the source code Ior a class, interIace, enumeration, or annotation type in a text Iile
whose name is the simple name oI the type and whose extension is .java. For
example:
//in the Rectangle.java file
package graphics;
p:blic class Rectangle ,
. . .
10

,
1hen puL Lhe source flle ln a dlrecLory whose name reflecLs Lhe name of Lhe package
Lo whlch Lhe Lype belongs
.....\graphics\Rectangle.java
1he quallfled name of Lhe package member and Lhe paLh name Lo Lhe flle are
parallel assumlng Lhe ,lcrosofL Wlndows flle name separaLor backslash (for Dnlx
use Lhe forward slash)
c|ass name
graphics.Rectangle
pathname to f||e
graphics\Rectangle.java
As you should recall, by convention a company uses its reversed Internet domain
name Ior its package names. The Example company, whose Internet domain name
isexa2ple.co2, would precede all its package names with co2.exa2ple. Each
component oI the package name corresponds to a subdirectory. $o, iI the Example
company had a co2.exa2ple.graphics package that contained a Rectangle.java source
Iile, it would be contained in a series oI subdirectories like this:
....\co2\exa2ple\graphics\Rectangle.java
When you compile a source Iile, the compiler creates a diIIerent output Iile Ior each
type deIined in it. The base name oI the output Iile is the name oI the type, and its
extension is .class. For example, iI the source Iile is like this
//in the Rectangle.java file
package co2.exa2ple.graphics;
p:blic class Rectangle ,
. . .
,

class Helper,
. . .
,
Lhen Lhe complled flles wlll be locaLed aL
<path to the parent directory of the o:tp:t
files\co2\exa2ple\graphics\Rectangle.class
<path to the parent directory of the o:tp:t
files\co2\exa2ple\graphics\Helper.class
Like the .java source Iiles, the compiled .class Iiles should be in a series oI
directories that reIlect the package name. However, the path to the .class Iiles does
11

not have to be the same as the path to the .javasource Iiles. You can arrange your
source and class directories separately, as:
<path*one\so:rces\co2\exa2ple\graphics\Rectangle.java

<path*two\classes\co2\exa2ple\graphics\Rectangle.class
8y dolng Lhls you can glve Lhe classes dlrecLory Lo oLher programmers wlLhouL
reveallng your sources ?ou also need Lo manage source and class flles ln Lhls manner
so LhaL Lhe compller and Lhe !ava IlrLual ,achlne (!I,) can flnd all Lhe Lypes your
program uses
The Iull path to the classes directory,<path*two\classes, is called the class path, and
is set with the CLSSPTH system variable. Both the compiler and the JVM construct
the path to your.class Iiles by adding the package name to the class path. For
example, iI
<path*two\classes
ls your class paLh and Lhe package name ls
co2.exa2ple.graphics,
Lhen Lhe compller and !I, look for .class files ln
<path*two\classes\co2\exa2ple\graphics.
A class path may include several paths, separated by a semicolon (Windows) or colon
(Unix). By deIault, the compiler and the JVM search the current directory and the
JAR Iile containing the Java platIorm classes so that these directories are
automatically in your class path.
Setting the CLSSPTH System VariabIe
1o dlsplay Lhe currenL CLSSPTH varlable use Lhese commands ln Wlndows and Dnlx
(8ourne shell)
In Windows: C:\ set CLSSPTH
In Unix: % echo CLSSPTH
1o deleLe Lhe currenL conLenLs of Lhe CLSSPTHvarlable use Lhese commands
In Windows: C:\ set CLSSPTH=
In Unix: % :nset CLSSPTH; export CLSSPTH
1o seL Lhe CLSSPTH varlable use Lhese commands (for example)
In Windows: C:\ set CLSSPTH=C:\:sers\george\java\classes
In Unix: % CLSSPTH=/ho2e/george/java/classes; export CLSSPTH
1

6 Summary of Creating and &sing Packages
To create a package Ior a type, put a packagestatement as the Iirst statement in the
source Iile that contains the type (class, interIace, enumeration, or annotation type).
To use a public type that's in a diIIerent package, you have three choices: (1) use the
Iully qualiIied name oI the type, (2) import the type, or (3) import the entire package
oI which the type is a member.
The path names Ior a package's source and class Iiles mirror the name oI the package.
You might have to set your CLSSPTH so that the compiler and the JVM can Iind
the .class Iiles Ior your types.
7 Questions and ercises: Creating and &sing Packages
Questions
ssume you have wrlLLen some classes 8elaLedly you declde Lhey should be spllL
lnLo Lhree packages as llsLed ln Lhe followlng Lable urLhermore assume Lhe classes
are currenLly ln Lhe defaulL package (Lhey have nopackage sLaLemenLs)
est|nat|on 9ackages
9ackage Name |ass Name
2yga2e.server Server
2yga2e.shared
Utilities
2yga2e.client Client
1 Whlch llne of code wlll you need Lo add Lo each source flle Lo puL each class ln
Lhe rlghL package?
1o adhere Lo Lhe dlrecLory sLrucLure you wlll need Lo creaLe some
subdlrecLorles ln Lhe developmenL dlrecLory and puL source flles ln Lhe correcL
subdlrecLorles WhaL subdlrecLorles musL you creaLe? Whlch subdlrecLory
does each source flle go ln?
3 uo you Lhlnk youll need Lo make any oLher changes Lo Lhe source flles Lo
make Lhem complle correcLly? lf so whaL?
13

ercises
uownload Lhe source flles as llsLed here
O Client
O Server
O Utilities
1 lmplemenL Lhe changes you proposed ln quesLlons 1 Lhrough 3 uslng Lhe
source flles you [usL downloaded
Complle Lhe revlsed source flles (nlot lf youre lnvoklng Lhe compller from
Lhe command llne (as opposed Lo uslng a bullder) lnvoke Lhe compller from
Lhe dlrecLory LhaL conLalns Lhe 2yga2edlrecLory you [usL creaLed)
Check your answers.













14

Learning the Java Language: nd of TraiI
?ouve reached Lhe end of Lhe Learnlng Lhe !ava Language Lrall
II you have comments or suggestions about this trail, use our Ieedback page to tell us
about it.
What net?
Cnce youve caughL your breaLh you have several cholces of where Lo go nexL ?ou
can go Lo Lhe fronL page Lo see all of your cholces or you can go dlrecLly Lo one of
Lhe followlng relaLed Lralls
Essential Java Classes: Learn about the most-used classes in the JDK APIs
including String, Syste2, Thread and the classes in java.io.
Creating a GUI with JFC/$wing: Once you know how to create applications or
applets, Iollow this trail to learn how to create their user interIaces.
Custom Networking and $ecurity: II you're interested in writing applications or
applets that use the network, Iollow this trail. You'll learn about URLs, sockets,
datagrams, and security.