< Zurück | Inhalt | Weiter >

Chapter 3

An Experienced Programmer’s Introduction to Java


Here the reader is rapidly acquainted with the manner in which Java imple- ments the OO (Object-Oriented) concepts. The language’s statements are un- ceremoniously presented. Much deference is paid to other texts in print and on the Web, since this is well traveled ground. We then present a simple sample Java application that will be used throughout the rest of this introductory part as an example that can be easily built in all of the Java environments available for Linux.



• Java syntax and semantics for the familiar (to an experienced programmer) programming constructs.

• How Java implements the OO: buzzwords of inheritance, encapsulation, and polymorphism.

• How Java deals with the absence of C++-style multiple inheritance.


• Why the absence of templates in Java is not as crippling as a C++ programmer might suppose.

• How final is better than virtual and how interfaces are often better than multiple inheritance.

This is going to be a whirlwind tour. Our book assumes that you already know programming in general, and have had some exposure to OO program- ming. We are going to distill into a single chapter material that comprises sig- nificant portions of other books. In particular, if there are concepts here that you are not already familiar with, look at Chapters 1–9 of Bruce Eckel’s won- derful book, Thinking in Java, 3rd ed., published by Prentice Hall PTR (ISBN 0-131-00287-2). It is, genuinely, one of the best books on the market for learning the Java language and the design principles Java embodies.

If you are somewhat new to programming, but technically quite adept (maybe a system administrator or database administrator with little formal programming background), you may want to supplement your reading with a book that, unlike Eckel’s, is targeted more toward the novice programmer. We like Java Software Solutions: Foundations of Program Design, 3rd ed., by John Lewis and William Loftus, Addison-Wesley, 2003 (ISBN 0-201-78129-8). It will introduce the concepts behind the programming constructs, whereas we will assume that you know these concepts so we can focus only on the Java syntax.


Before the object-oriented structures, Java (like C) has a small number of fundamental statements and (again, like C and C++) some fundamental “nonobject” data types.1


1. The existence of these nonobject data types is another thing that brings up criticism of the Java language. Since Java does not have C++’s operator overloading features, you cannot use objects in standard algebraic expressions. I’m not sure if the inclusion of scalar classes was mo- tivated by speed, or by the lack of operator overloading. Whatever the reason, like any other design compromise, it has both advantages and disadvantages, as we shall see throughout the book.


3.2.1 Scalar Types

3.2.2 Object Types

3.2.3 Statements

3.2.4 Error Handling, Java Style

3.4.1 Encapsulation

3.4.2 Inheritance

3.4.3 Polymorphism