-
"Thinking in Java"
(by
Bruce Eckel)
-
is a revisited mirror
(published with the agreement and the cooperation of the original author)
of
http://www.EckelObjects.com/javabook.html
where you can download this free book in Acrobat
(
www.adobe.com)
PDF format:
Thinking in Java Rev. 9, August 16, 1997 TJAVA9.PDF (2678 kb).
Downloading the PDF file from the original site is quite difficult.
The
TJAVA9.PDF Acrobat file,
the
tjava9W95.zip file (1557 kb --> a 4349 kb WORD file),
the CodePackager
tj9code.zip
(122 kb --> 3 files, 1070 kb)
and the
CodePackager java source
are mirrored on our site. A detailled description of all
chapters and an important
errata note are also available.
This book has been written by Bruce Eckel
(
Bruce@EckelObjects.com),
also author of
"Thinking in
C++".
Capitalize the first letter of class names. The first letter of fields, methods and objects
(handles) should be lowercase. All identifiers should run their words together, and
capitalize the first letter of all intermediate words. For example
ThisIsAClassName
thisIsAMethodOrFieldName
Capitalize all the letters of static final identifiers.
Packages are a special case: they are all lowercase letters, even for intermediate words.
(in appendix C (Java programming guidelines) of
Thinking in Java, by
Bruce Eckel)
For each class you create, consider including a main( ) that contains code to test that
class. You don't need to remove the test code to use the class in a project, and if you
make any changes you can easily re-run the tests.
(in appendix C (Java programming guidelines) of
Thinking in Java, by
Bruce Eckel)
If your class requires any cleanup when the client programmer is finished with the object,
place the cleanup code in a single, well defined method. In addition, place a boolean
flag in the class to indicate whether the object has been cleaned up or not. In the
finalize( ) method for the class, check to make sure the object has been cleaned up,
and throw a class derived from RuntimeException if it hasn't, to indicate a programming
error.
(in appendix C (Java programming guidelines) of
Thinking in Java, by
Bruce Eckel)
When overriding finalize( ) during inheritance, remember to call super.finalize( ) (this
is not necessary if Object is your immediate superclass). You should call
super.finalize( ) as the last act of your overridden finalize( ) rather than the first, to
ensure that base-class components are still valid if you need them.
(in appendix C (Java programming guidelines) of
Thinking in Java, by
Bruce Eckel)
Constructors, exceptions, and finalize( ).
You'll generally want to re-throw any exceptions that you catch in a constructor if it
causes failure of the creation of that object, so the caller doesn't blindly continue,
thinking that the object was created correctly.
(in appendix C (Java programming guidelines) of
Thinking in Java, by
Bruce Eckel)
When you are creating a fixed-size collection of objects, transfer them to an array
(especially if you're returning this collection from a method). This way you get the
benefit of the array's compile-time type checking and the recipient of the array may not
have to cast the objects in the array to use them.
(in appendix C (Java programming guidelines) of
Thinking in Java, by
Bruce Eckel)
Choose interfaces over abstract classes. If you know something is going to be a base
class, your first choice should be to make it an interface, and only if you're forced to
have method definitions or member variables should you change to an abstract class.
(in appendix C (Java programming guidelines) of
Thinking in Java, by
Bruce Eckel)
Inside constructors, do only what is necessary to set the object into the proper state.
Actively avoid calling other methods (except for final methods) since those methods
may be overridden to produce unexpected results during construction (see
Chapter 7 for
details).
(in appendix C (Java programming guidelines) of
Thinking in Java, by
Bruce Eckel)
Choose composition first when creating new classes from existing classes. Only if
inheritance is required by your design should it be used. If you use inheritance where
composition will work, your designs will become needlessly complicated.
(in appendix C (Java programming guidelines) of
Thinking in Java, by
Bruce Eckel)
Use inheritance to express differences in behavior, and member variables to express
variations in state.
(in appendix C (Java programming guidelines) of
Thinking in Java, by
Bruce Eckel)
To avoid a highly frustrating experience, make sure there's only one class of each name
anywhere in your classpath. Otherwise the compiler can find the identically-named other
class first, and report error messages that make no sense. If you suspect you are having a
classpath problem, try looking for .class files with the same names at each of the starting
points in your classpath.
(in appendix C (Java programming guidelines) of
Thinking in Java, by
Bruce Eckel)
When using the event "adapters" in the new Java 1.1 AWT, there's a particularly easy
pitfall you can encounter. If you override one of the adapter methods and you don't quite
get the spelling right, you'll end up adding a new method rather than overriding an
existing method. However, this is perfectly legal and so you won't get any error message
from the compiler or run-time system – your code simply won't work correctly.
(in appendix C (Java programming guidelines) of
Thinking in Java, by
Bruce Eckel)
introduction to objects (in
Thinking in Java, by
Bruce Eckel)
the progress of abstraction (in
Thinking in Java - Chapter :
introduction to objects - by
Bruce Eckel)
an object has an interface (in
Thinking in Java - Chapter :
introduction to objects - by
Bruce Eckel)
the hidden implementation (in
Thinking in Java - Chapter :
introduction to objects - by
Bruce Eckel)
reusing the implementation (in
Thinking in Java - Chapter :
introduction to objects - by
Bruce Eckel)
inheritance: reusing the interface (in
Thinking in Java - Chapter :
introduction to objects - by
Bruce Eckel)
overriding base-class functionality (in
Thinking in Java - Chapter :
introduction to objects - by
Bruce Eckel)
is-a vs. is-like-a relationships (in
Thinking in Java - Chapter :
introduction to objects - by
Bruce Eckel)
interchangeable objects with polymorphism
(in
Thinking in Java - Chapter :
introduction to objects - by
Bruce Eckel)
dynamic binding (in
Thinking in Java - Chapter :
introduction to objects - by
Bruce Eckel)
the abstract base class (in
Thinking in Java - Chapter :
introduction to objects - by
Bruce Eckel)
object landscapes and lifetimes (in
Thinking in Java - Chapter :
introduction to objects - by
Bruce Eckel)
containers and iterators (in
Thinking in Java - Chapter :
introduction to objects - by
Bruce Eckel)
the singly-rooted hierarchy (in
Thinking in Java - Chapter :
introduction to objects - by
Bruce Eckel)
container libraries and support for easy
container use (in
Thinking in Java - Chapter :
introduction to objects - by
Bruce Eckel)
the housekeeping dilemma: who should clean
up? (in
Thinking in Java - Chapter :
introduction to objects - by
Bruce Eckel)
exception handling: dealing with errors
(in
Thinking in Java - Chapter :
introduction to objects - by
Bruce Eckel)
multithreading (in
Thinking in Java - Chapter :
introduction to objects - by
Bruce Eckel)
persistence (in
Thinking in Java - Chapter :
introduction to objects - by
Bruce Eckel)
Java and the Internet (in
Thinking in Java - Chapter :
introduction to objects - by
Bruce Eckel)
what is the Web? (in
Thinking in Java - Chapter :
introduction to objects - by
Bruce Eckel)
client-side programming (in
Thinking in Java - Chapter :
introduction to objects - by
Bruce Eckel)
a separate arena: applications (in
Thinking in Java - Chapter :
introduction to objects - by
Bruce Eckel)
online documentation
(in
Thinking in Java - Chapter :
introduction to objects - by
Bruce Eckel)
summary: Java vs. C++?
(in
Thinking in Java - Chapter :
introduction to objects - by
Bruce Eckel)
everything is an object (in
Thinking in Java, by
Bruce Eckel)
you manipulate objects through handles
(in
Thinking in Java - Chapter :
everything is an object - by
Bruce Eckel)
you must create all the objects (in
Thinking in Java - Chapter :
everything is an object - by
Bruce Eckel)
where storage lives (in
Thinking in Java - Chapter :
everything is an object - by
Bruce Eckel)
special case: primitive types (in
Thinking in Java - Chapter :
everything is an object - by
Bruce Eckel)
arrays in Java (in
Thinking in Java - Chapter :
everything is an object - by
Bruce Eckel)
you never have to destroy an object
(in
Thinking in Java - Chapter :
everything is an object - by
Bruce Eckel)
scoping (in
Thinking in Java - Chapter :
everything is an object - by
Bruce Eckel)
scope of objects (in
Thinking in Java - Chapter :
everything is an object - by
Bruce Eckel)
creating new data types: class (in
Thinking in Java - Chapter :
everything is an object - by
Bruce Eckel)
fields and methods (in
Thinking in Java - Chapter :
everything is an object - by
Bruce Eckel)
methods, arguments, and return values
(in
Thinking in Java - Chapter :
everything is an object - by
Bruce Eckel)
the argument list (in
Thinking in Java - Chapter :
everything is an object - by
Bruce Eckel)
building a Java program (in
Thinking in Java - Chapter :
everything is an object - by
Bruce Eckel)
name visibility (in
Thinking in Java - Chapter :
everything is an object - by
Bruce Eckel)
using other components (in
Thinking in Java - Chapter :
everything is an object - by
Bruce Eckel)
the "static" keyword (in
Thinking in Java - Chapter :
everything is an object - by
Bruce Eckel)
your first Java program (in
Thinking in Java - Chapter :
everything is an object - by
Bruce Eckel)
comments & embedded documentation
(in
Thinking in Java - Chapter :
everything is an object - by
Bruce Eckel)
comment documentation (in
Thinking in Java - Chapter :
everything is an object - by
Bruce Eckel)
syntax (in
Thinking in Java - Chapter :
everything is an object - by
Bruce Eckel)
embedded HTML (in
Thinking in Java - Chapter :
everything is an object - by
Bruce Eckel)
@see: referring to other classes (in
Thinking in Java - Chapter :
everything is an object - by
Bruce Eckel)
class documentation tags (in
Thinking in Java - Chapter :
everything is an object - by
Bruce Eckel)
variable documentation tags (in
Thinking in Java - Chapter :
everything is an object - by
Bruce Eckel)
method documentation tags (in
Thinking in Java - Chapter :
everything is an object - by
Bruce Eckel)
documentation example (in
Thinking in Java - Chapter :
everything is an object - by
Bruce Eckel)
coding style (in
Thinking in Java - Chapter :
everything is an object - by
Bruce Eckel)
controlling program flow (in
Thinking in Java, by
Bruce Eckel)
using Java operators (in
Thinking in Java - Chapter :
controlling program flow - by
Bruce Eckel)
precedence (in
Thinking in Java - Chapter :
controlling program flow - by
Bruce Eckel)
assignment (in
Thinking in Java - Chapter :
controlling program flow - by
Bruce Eckel)
mathematical operators (in
Thinking in Java - Chapter :
controlling program flow - by
Bruce Eckel)
auto increment and decrement (in
Thinking in Java - Chapter :
controlling program flow - by
Bruce Eckel)
relational operators (in
Thinking in Java - Chapter :
controlling program flow - by
Bruce Eckel)
logical operators (in
Thinking in Java - Chapter :
controlling program flow - by
Bruce Eckel)
bitwise operators (in
Thinking in Java - Chapter :
controlling program flow - by
Bruce Eckel)
shift operators (in
Thinking in Java - Chapter :
controlling program flow - by
Bruce Eckel)
ternary if-else operator (in
Thinking in Java - Chapter :
controlling program flow - by
Bruce Eckel)
the comma operator (in
Thinking in Java - Chapter :
controlling program flow - by
Bruce Eckel)
String operator + (in
Thinking in Java - Chapter :
controlling program flow - by
Bruce Eckel)
common pitfalls when using operators
(in
Thinking in Java - Chapter :
controlling program flow - by
Bruce Eckel)
casting operators (in
Thinking in Java - Chapter :
controlling program flow - by
Bruce Eckel)
Java has no "sizeof" (in
Thinking in Java - Chapter :
controlling program flow - by
Bruce Eckel)
precedence revisited (in
Thinking in Java - Chapter :
controlling program flow - by
Bruce Eckel)
a compendium of operators (in
Thinking in Java - Chapter :
controlling program flow - by
Bruce Eckel)
execution control (in
Thinking in Java - Chapter :
controlling program flow - by
Bruce Eckel)
true and false (in
Thinking in Java - Chapter :
controlling program flow - by
Bruce Eckel)
if-else (in
Thinking in Java - Chapter :
controlling program flow - by
Bruce Eckel)
iteration (in
Thinking in Java - Chapter :
controlling program flow - by
Bruce Eckel)
do-while (in
Thinking in Java - Chapter :
controlling program flow - by
Bruce Eckel)
for (in
Thinking in Java - Chapter :
controlling program flow - by
Bruce Eckel)
break and continue (in
Thinking in Java - Chapter :
controlling program flow - by
Bruce Eckel)
switch (in
Thinking in Java - Chapter :
controlling program flow - by
Bruce Eckel)
initialization & cleanup (in
Thinking in Java, by
Bruce Eckel)
guaranteed initialization with the constructor
(in
Thinking in Java - Chapter :
initialization & cleanup - by
Bruce Eckel)
method overloading (in
Thinking in Java - Chapter :
initialization & cleanup - by
Bruce Eckel)
distinguishing overloaded methods (in
Thinking in Java - Chapter :
initialization & cleanup - by
Bruce Eckel)
overloading on return values (in
Thinking in Java - Chapter :
initialization & cleanup - by
Bruce Eckel)
default constructors (in
Thinking in Java - Chapter :
initialization & cleanup - by
Bruce Eckel)
the this keyword (in
Thinking in Java - Chapter :
initialization & cleanup - by
Bruce Eckel)
cleanup: finalization and garbage collection
(in
Thinking in Java - Chapter :
initialization & cleanup - by
Bruce Eckel)
what is finalize ( ) for? (in
Thinking in Java - Chapter :
initialization & cleanup - by
Bruce Eckel)
you must perform cleanup (in
Thinking in Java - Chapter :
initialization & cleanup - by
Bruce Eckel)
member initialization (in
Thinking in Java - Chapter :
initialization & cleanup - by
Bruce Eckel)
specifying initialization (in
Thinking in Java - Chapter :
initialization & cleanup - by
Bruce Eckel)
constructor initialization (in
Thinking in Java - Chapter :
initialization & cleanup - by
Bruce Eckel)
array initialization (in
Thinking in Java - Chapter :
initialization & cleanup - by
Bruce Eckel)
hiding the implementation
(in
Thinking in Java, by
Bruce Eckel)
package: the library unit (in
Thinking in Java - Chapter :
hiding the implementation - by
Bruce Eckel)
creating unique package names (in
Thinking in Java - Chapter :
hiding the implementation - by
Bruce Eckel)
a custom tool library (in
Thinking in Java - Chapter :
hiding the implementation - by
Bruce Eckel)
package caveat (in
Thinking in Java - Chapter :
hiding the implementation - by
Bruce Eckel)
Java access specifiers (in
Thinking in Java - Chapter :
hiding the implementation - by
Bruce Eckel)
"friendly" (in
Thinking in Java - Chapter :
hiding the implementation - by
Bruce Eckel)
public: interface access (in
Thinking in Java - Chapter :
hiding the implementation - by
Bruce Eckel)
private: you can't touch that! (in
Thinking in Java - Chapter :
hiding the implementation - by
Bruce Eckel)
protected: "sort of private" (in
Thinking in Java - Chapter :
hiding the implementation - by
Bruce Eckel)
interface & implementation (in
Thinking in Java - Chapter :
hiding the implementation - by
Bruce Eckel)
class access (in
Thinking in Java - Chapter :
hiding the implementation - by
Bruce Eckel)
reusing classes (in
Thinking in Java, by
Bruce Eckel)
composition syntax (in
Thinking in Java - Chapter :
reusing classes - by
Bruce Eckel)
inheritance syntax (in
Thinking in Java - Chapter :
reusing classes - by
Bruce Eckel)
initializing the base class (in
Thinking in Java - Chapter :
reusing classes - by
Bruce Eckel)
combining composition & inheritance
(in
Thinking in Java - Chapter :
reusing classes - by
Bruce Eckel)
guaranteeing proper cleanup (in
Thinking in Java - Chapter :
reusing classes - by
Bruce Eckel)
name hiding (in
Thinking in Java - Chapter :
reusing classes - by
Bruce Eckel)
choosing composition vs. inheritance
(in
Thinking in Java - Chapter :
reusing classes - by
Bruce Eckel)
protected (in
Thinking in Java - Chapter :
reusing classes - by
Bruce Eckel)
incremental development (in
Thinking in Java - Chapter :
reusing classes - by
Bruce Eckel)
upcasting (in
Thinking in Java - Chapter :
reusing classes - by
Bruce Eckel)
why "upcasting"? (in
Thinking in Java - Chapter :
reusing classes - by
Bruce Eckel)
the final keyword (in
Thinking in Java - Chapter :
reusing classes - by
Bruce Eckel)
final data (in
Thinking in Java - Chapter :
reusing classes - by
Bruce Eckel)
final methods (in
Thinking in Java - Chapter :
reusing classes - by
Bruce Eckel)
final classes (in
Thinking in Java - Chapter :
reusing classes - by
Bruce Eckel)
final caution (in
Thinking in Java - Chapter :
reusing classes - by
Bruce Eckel)
initialization & class loading (in
Thinking in Java - Chapter :
reusing classes - by
Bruce Eckel)
initialization with inheritance (in
Thinking in Java - Chapter :
reusing classes - by
Bruce Eckel)
polymorphism (in
Thinking in Java, by
Bruce Eckel)
upcasting (in
Thinking in Java - Chapter :
polymorphism - by
Bruce Eckel)
why upcast? (in
Thinking in Java - Chapter :
polymorphism - by
Bruce Eckel)
the twist (in
Thinking in Java - Chapter :
polymorphism - by
Bruce Eckel)
method call binding (in
Thinking in Java - Chapter :
polymorphism - by
Bruce Eckel)
producing the right behavior (in
Thinking in Java - Chapter :
polymorphism - by
Bruce Eckel)
extensibility (in
Thinking in Java - Chapter :
polymorphism - by
Bruce Eckel)
overriding vs. overloading (in
Thinking in Java - Chapter :
polymorphism - by
Bruce Eckel)
abstract classes & methods (in
Thinking in Java - Chapter :
polymorphism - by
Bruce Eckel)
interfaces (in
Thinking in Java - Chapter :
polymorphism - by
Bruce Eckel)
"multiple inheritance" in Java (in
Thinking in Java - Chapter :
polymorphism - by
Bruce Eckel)
extending an interface with inheritance
(in 