Table of Contents
| List of Figures | |
| List of Tables | |
| List of Examples | |
| Foreword | |
| Preface | |
| 1 | Basics of Java Programming | 1 |
| 2 | Language Fundamentals | 19 |
| 3 | Operators and Assignments | 41 |
| 4 | Declarations and Access Control | 99 |
| 5 | Control Flow, Exception Handling, an Assertions | 157 |
| 6 | Object-oriented Programming | 225 |
| 7 | Nested Classes and Interfaces | 283 |
| 8 | Object Lifetime | 317 |
| 9 | Threads | 349 |
| 10 | Fundamental Classes | 387 |
| 11 | Collections and Maps | 427 |
| A | Taking the SCPJ2 1.4 Exam | 489 |
| B | Objectives for the SCPJ2 1.4 Exam | 497 |
| C | Objectives for the Java 2 Platform Upgrade Exam | 505 |
| D | Annotated Answers to Review Questions | 511 |
| E | Solutions to Programming Exercises | 545 |
| F | Mock Exam | 561 |
| G | Number Systems and Number Representation | 593 |
| H | About the CD | 601 |
| Index | 603 |
Forewords & Introductions
Writing the Second Edition
The exam for the Sun Certified Programmer for Java 2 (SCPJ2) Platform has changed considerably since the first edition of this book was published. The most noticeable change is the removal of GUI and I/O from the exam, and shifting of emphasis towards the core features of the language. In our opinion, the new exam demands an even greater understanding and actual experience of the language, rather than mere spewing of factual details. Proficiency in the language is the key to success.
In preparing for the second edition of the book, we have seriously taken into consideration the evolution of the SCPJ2 exam.
The I/O and GUI topics of the first edition have been eliminated, as they have no relevance for the SCPJ2 exam. These topics are more relevant for the Sun Certified Developer for Java 2 (SCDJ2) Platform exam, and therefore delegated to a possible future volume on the Developer exam topics.
Since emphasis of the SCPJ2 exam is on the core features of Java, the second edition provides an even greater in-depth coverage of the relevant topics. The book covers not just the exam objectives, but also supplementary topics which have a bearing on the exam.
The second edition is still a one-source guide for the SCPJ2 exam: it provides a mixture of theory and practice for the exam. The book can be used to learn Java, accomplish the SCPJ2 exam, and afterwards come in handy as a language reference. To facilitate preparation for the exam, the second edition includes a CD with a wide range of additional resources. The book also has an appendix devoted to the SCPJ2 Upgrade exam.
We have taken into consideration the reader input andfeedback we have received. The many hours spent in handling the deluge of e-mail have not been in vain. Every single e-mail is appreciated and is hereby acknowledged.
Preparing the second edition dispelled all our illusions about second editions being, to put it colloquially, a piece of cake. Every sentence from the first edition has been weighed carefully, and not many paragraphs have escaped rewriting. Basic elements of UML (Unified Modeling Language) are also extensively employed in this edition. Numerous new review questions have been added. In covering the new topics and expanding the existing ones, new examples, figures and tables were also specifically created for the second edition.
About This Book
This book provides an extensive coverage of the Java programming language and its core APIs (Application Programming Interfaces), with particular emphasis on its syntax and usage. The book is primarily intended for professionals who want to prepare for the Sun Certified Programmer for Java 2 Platform exam (referred to as the SCPJ2 exam), but it is readily accessible to any programmer who wants to master the language. For both purposes, it provides an in-depth coverage of essential features of the language and its core APIs.
There is a great and increasing demand for certified Java programmers. Sun Microsystems has defined the SCPJ2 exam, which professionals can take to validate their skills. The certification provides the IT industry the standard to use for hiring such professionals, and allows the professionals to turn their Java skills into credentials that are important for career advancement.
The book helps the reader master all core features of the Java language, and this mastering of the language can culminate in accomplishing the exam. It provides an extensive coverage of all the objectives defined for the exam by Sun. Since the exam objectives are selective, they do not cover many of the essential features of Java. The book provides extensive coverage of additional topics that every Java programmer should master in order to be proficient in this field. In this regard, the book is a comprehensive primer for learning the Java programming language.
This book is not a complete reference for Java, as it does not attempt to list every member of every class from the Java SDK (System Development Kit) API documentation. Its purpose is not to document the Java SDK APIs. It is also not a book on teaching programming techniques. Its emphasis is on the Java programming language features, their syntax and correct usage.
The book assumes a background in programming. We believe the exam is accessible to any programmer who works through the book. A Java programmer can easily skip over material which is well understood, and concentrate on parts that need reinforcing, whereas a programmer new to Java will find the concepts explained from basic principles.
Each topic is explained and discussed thoroughly with examples, and backed by review questions and exercises to reinforce the concepts. The book is not biased toward any particular platform, but provides platform-specific details where necessary.
Using the Book
The reader can choose a linear or a non-linear route through the book, depending on his or her programming background. Non-Java programmers wishing to migrate to Java can read Chapter 1, which provides a short introduction to object-oriented programming concepts, and the procedure for compiling and running Java applets and standalone applications. For those preparing for the SCPJ2 exam, the book has a separate appendix providing all the pertinent information on taking the exam.
The table of contents, listings of tables, examples and figures, and a comprehensive index facilitate locating topics discussed in the book.
In particular, we draw attention to the following features of the book:
Exam and Supplementary Objectives
Exam objectives are stated clearly at the start of every chapter, together with any supplementary objectives. The objectives are defined by Sun and are organized into major sections, detailing the curriculum for the examination. The book is organized into chapters that logically follow the order of these major sections. The objectives are reproduced in a separate appendix where, for each section of the syllabus, study notes are included to point the reader to topics essential for the exam. We believe that the supplementary objectives are important to the ultimate goal of mastering the language.Review Questions
Review questions are provided after every major topic, in order to test and reinforce the material. These review questions reflect the kind of questions that can be asked on the actual exam. Annotated answers to the review questions are provided in a separate appendix.
Example Source Code
We encourage experimenting with the code examples in order to reinforce the material from the book. These can be downloaded from the book web site.Java code is written in a mono spaced font. Li of code in the examples or in code snippets are referenced in the text by a number, which is specified by using a single-line comment in the code. For example, in the code snippet below, the call to the method doSomethingInteresting() hopefully does something interesting at (1).
// ...
doSomethingInteresting(); // (1)
// ...Names of classes and interfaces start with an uppercase letter. Names of packages, variables and methods start with a lowercase letter. Constants are all in uppercase letters.
Chapter Summary
Each chapter concludes with a summary of the topics, pointing out the major concepts discussed in the chapter.
Programming Exercises
Programming exercises at the end of each chapter provide the opportunity to put concepts into practice. Solutions to the programming exercises are provided in a separate appendix.
Sample Exam
A complete sample exam is provided in a separate appendix, which the reader can try when she is ready.
Java 2 SDK API
A vertical gray bar is used to highlight methods and fields found in the classes of the core Java APIs.Any explanation following the API information is also similarly highlighted.
In order to obtain optimal benefit from using this book in preparing for the SCPJ2 exam, we strongly recommend installing the latest version (at least 1.4) of the SDK and its accompanying API documentation. The book focuses solely on Java 2, and does not acknowledge previous versions.
Java 2 Platform Upgrade Exam
For those who have taken the Sun Certified Programmer for Java 2 Platform 1.2 Exam, and would like to prepare for the Sun Certified Programmer for Java 2 Platform Upgrade Exam, we have provided an appendix with details of the upgrade exam. The appendix contains the upgrade exam objectives, and for each section of the syllabus, study notes are included to point the reader to topics essential for the upgrade exam.
Accompanying CD
The accompanying CD contains a wealth of information to help prepare for the exam, and provides numerous sample exams for the candidate to test her skills. The software included simulates near exam-like conditions in order to acquaint the candidate with the exam environment.