WHAT IS A JAVA
Introduction
Like any human language, Java provides a way to express
concepts. If successful, this medium of expression will be
significantly easier and more flexible than the alternatives
as problems grow larger and more complex.
You can’t look at Java as just a collection of features—some of the features
make no sense in isolation. You can use the sum of the parts only if you
are thinking about design, not simply coding. And to understand Java in
this way, you must understand the problems with it and with
programming in general. This book discusses programming problems,
why they are problems, and the approach Java has taken to solve them.
Thus, the set of features I explain in each chapter are based on the way I
see a particular type of problem being solved with the language. In this
way I hope to move you, a little at a time, to the point where the Java
mindset becomes your native tongue.
Throughout, I’ll be taking the attitude that you want to build a model in
your head that allows you to develop a deep understanding of the
language; if you encounter a puzzle you’ll be able to feed it to your model
and deduce the answer.
Prerequisites
This book assumes that you have some programming familiarity: you
understand that a program is a collection of statements, the idea of a
subroutine/function/macro, control statements such as “if” and looping
constructs such as “while,” etc. However, you might have learned this in
many places, such as programming with a macro language or working
with a tool like Perl. As long as you’ve programmed to the point where you
feel comfortable with the basic ideas of programming, you’ll be able to
work through this book. Of course, the book will be easier for the C
programmers and more so for the C++ programmers, but don’t count
yourself out if you’re not experienced with those languages (but come
Learning Java
At about the same time that my first book Using C++ (Osborne/McGraw-
Hill, 1989) came out, I began teaching that language. Teaching
programming languages has become my profession; I’ve seen nodding
heads, blank faces, and puzzled expressions in audiences all over the
world since 1989. As I began giving in-house training with smaller groups
of people, I discovered something during the exercises. Even those people
who were smiling and nodding were confused about many issues. I found
out, by chairing the C++ track at the Software Development Conference
for a number of years (and later the Java track), that I and other speakers
tended to give the typical audience too many topics too fast. So eventually,
through both variety in the audience level and the way that I presented
the material, I would end up losing some portion of the audience. Maybe
it’s asking too much, but because I am one of those people resistant to
traditional lecturing (and for most people, I believe, such resistance
results from boredom), I wanted to try to keep everyone up to speed.
For a time, I was creating a number of different presentations in fairly
short order. Thus, I ended up learning by experiment and iteration (a
technique that also works well in Java program design). Eventually I
developed a course using everything I had learned from my teaching
experience—one that I would be happy giving for a long time. It tackles
the learning problem in discrete, easy-to-digest steps, and in a hands-on
seminar (the ideal learning situation) there are exercises following each of
willing to work hard; also, the multimedia CD that accompanies this book
will bring you up to speed on the basic C syntax necessary to learn Java).
I’ll be introducing the concepts of object-oriented programming (OOP)
and Java’s basic control mechanisms, so you’ll be exposed to those, and
the first exercises will involve the basic control-flow statements.
Although references will often be made to C and C++ language features,
these are not intended to be insider comments, but instead to help all
programmers put Java in perspective with those languages, from which,
after all, Java is descended. I will attempt to make these references simple
and to explain anything that I think a non- C/C++ programmer would not
be familiar with.
Goals
Like my previous book Thinking in C++, this book has come to be
structured around the process of teaching the language. In particular, my
motivation is to create something that provides me with a way to teach the
language in my own seminars. When I think of a chapter in the book, I
think in terms of what makes a good lesson during a seminar. My goal is
to get bite-sized pieces that can be taught in a reasonable amount of time,
followed by exercises that are feasible to accomplish in a classroom
situation.
My goals in this book are to:
1. Present the material one simple step at a time so that you can easily
2. Use examples that are as simple and short as possible. This
sometimes prevents me from tackling “real world” problems, but
I’ve found that beginners are usually happier when they can
understand every detail of an example rather than being impressed
by the scope of the problem it solves. Also, there’s a severe limit to
the amount of code that can be absorbed in a classroom situation.
For this I will no doubt receive criticism for using “toy examples,”
but I’m willing to accept that in favor of producing something
pedagogically useful.
3. Carefully sequence the presentation of features so that you aren’t
seeing something that you haven’t been exposed to. Of course, this
isn’t always possible; in those situations, a brief introductory
description is given.
4. Give you what I think is important for you to understand about the
language, rather than everything I know. I believe there is an
information importance hierarchy, and that there are some facts
that 95 percent of programmers will never need to know and that
just confuse people and adds to their perception of the complexity
of the language. To take an example from C, if you memorize the
operator precedence table (I never did), you can write clever code.
But if you need to think about it, it will also confuse the
reader/maintainer of that code. So forget about precedence, and
use parentheses when things aren’t clear.
5. Keep each section focused enough so that the lecture time—and the
time between exercise periods—is small. Not only does this keep
the audience’s minds more active and involved during a hands-on
seminar, but it gives the reader a greater sense of accomplishment.
6. Provide you with a solid foundation so that you can understand the
issues well enough to move on to more difficult coursework and
Online documentation
The Java language and libraries from Sun Microsystems (a free download)
come with documentation in electronic form, readable using a Web
browser, and virtually every third party implementation of Java has this
or an equivalent documentation system. Almost all the books published
on Java have duplicated this documentation. So you either already have it
or you can download it, and unless necessary, this book will not repeat
that documentation because it’s usually much faster if you find the class
descriptions with your Web browser than if you look them up in a book
(and the on-line documentation is probably more up-to-date). This book
will provide extra descriptions of the classes only when it’s necessary to
supplement the documentation so you can understand
a particular example.