HAPPY BOOKSGIVING
Use code BOOKSGIVING during checkout to save 40%-55% on books and eBooks. Shop now.
Register your product to gain access to bonus material or receive a coupon.
A new edition of this title is available, ISBN-10: 0321556054 ISBN-13: 9780321556059
Download the Sample Chapter related to this title.
List of Tables.
List of Examples.
List of Figures.
Foreword.
Preface.
Writing the Second Edition.
About This Book.
Using the Book.
Book Web Sites.
Request for Feedback.
About the Authors.
Acknowledgments (First Edition).
Acknowledgments (Second Edition).
Introduction.
Classes.
Declaring Members: Fields and Methods.
Objects.
Class Instantiation.
Object References.
Instance Members.
Invoking Methods.
Static Members.
Inheritance.
Aggregation.
Tenets of Java.
Review Questions.
Java Programs.
Sample Java Application.
Essential Elements of a Java Application.
Compiling and Running an Application.
Review Questions.
Chapter Summary.
Programming Exercises.
Basic Language Elements.
Lexical Tokens.
Identifiers.
Keywords.
Literals.
Integer Literals.
Floating-point Literals.
Boolean Literals.
Character Literals.
String Literals.
White Spaces.
Comments.
Review Questions.
Primitive Data Types.
Integer Types.
Character Type.
Floating-point Types.
Boolean Type.
Review Questions.
Variable Declarations.
Declaring and Initializing Variables.
Object Reference Variables.
Lifetime of Variables.
Initial Values for Variables.
Default Values for Fields.
Initializing Local Variables of Primitive Data Types.
Initializing Local Reference Variables.
Review Questions.
Java Source File Structure.
Review Questions.
The main() Method.
Review Questions.
Chapter Summary.
Programming Exercises.
Precedence and Associativity Rules for Operators.
Evaluation order of operands.
Conversions.
Unary Cast Operator: (type).
Narrowing and Widening Conversions.
Numeric Promotions.
Type Conversion Contexts.
Simple Assignment Operator =.
Assigning Primitive Values.
Assigning References.
Multiple Assignments.
Numeric Type Conversions on Assignment.
Review Questions.
Arithmetic Operators: *, /, %, +, -.
Arithmetic Operator Precedence and Associativity.
Evaluation Order in Arithmetic Expressions.
Range of Numeric Values.
Unary Arithmetic Operators: -, +.
Multiplicative Binary Operators: *, /, %.
Additive Binary Operators: +, -.
Numeric Promotions in Arithmetic Expressions.
Arithmetic Compound Assignment Operators: *=, /=, %=, +=, -=.
Review Questions.
The Binary String Concatenation Operator +.
Variable Increment and Decrement Operators: ++, --.
Increment Operator ++.
Decrement Operator --.
Review Questions.
Boolean Expressions.
Relational Operators: <, <=, >, >=.
Equality.
Primitive Data Value Equality: ==, !=.
Object Reference Equality: ==, !=.
Object Value Equality.
Boolean Logical Operators: !, ^, &, |.
Operand Evaluation for Boolean Logical Operators.
Boolean Logical Compound Assignment Operators: &=, ^=, |=.
Conditional Operators: &&, ||.
Short-circuit Evaluation.
Review Questions.
Integer Bitwise Operators: ~, &, |, ^.
Bitwise Compound Assignment Operators: &=, ^=, |=.
Shift Operators: <<, >>, >>>.
The Shift-left Operator <<.
The Shift-right-with-sign-fill Operator >>.
The Shift-right-with-zero-fill Operator >>>.
Shift Compound Assignment Operators: <<=, >>=, >>>=.
The Conditional Operator ? :.
Other Operators: new, [], instanceof.
Review Questions.
Parameter Passing.
Passing Primitive Data Values.
Passing Object Reference Values.
Passing Array References.
Array Elements as Actual Parameters.
final Parameters.
Program Arguments.
Review Questions.
Chapter Summary.
Programming Exercises.
Arrays.
Declaring Array Variables.
Constructing an Array.
Initializing an Array.
Using an Array.
Anonymous Arrays.
Multidimensional Arrays.
Review Questions.
Defining Classes.
Defining Methods.
Statements.
Instance Methods and Object Reference this.
Method Overloading.
Constructors.
Default Constructor.
Overloaded Constructors.
Scope Rules.
Class Scope for Members.
Block Scope for Local Variables.
Review Questions.
Packages.
Defining Packages.
Using Packages.
Compiling and Running Code from Packages.
Accessibility Modifiers for Top-level Classes and Interfaces.
Other Modifiers for Classes.
abstract Classes.
final Classes.
Review Questions.
Member Accessibility Modifiers.
public Members.
protected Members.
Default Accessibility for Members.
private Members.
Review Questions.
Other Modifiers for Members.
static Members.
final Members.
abstract Methods.
synchronized Methods.
native Methods.
transient Fields.
volatile Fields.
Review Questions.
Chapter Summary.
Programming Exercises.
Overview of Control Flow Statements.
Selection Statements.
Simple if Statement.
if-else Statement.
switch Statement.
Review Questions.
Iteration Statements.
while Statement.
do-while Statement.
for Statement.
Transfer Statements.
Labeled Statements.
break Statement.
continue Statement.
return Statement.
Review Questions.
Stack-based Execution and Exception Propagation.
Exception Types.
Class Exception.
Class RuntimeException.
Class Error.
Checked and Unchecked Exceptions.
Defining New Exceptions.
Exception Handling: try, catch and finally.
try Block.
catch Block.
finally Block.
throw Statement.
throws Clause.
Review Questions.
Assertions.
assert Statement and AssertionError Class.
Compiling Assertions.
Runtime Enabling and Disabling of Assertions.
Using Assertions.
Review Questions.
Chapter Summary.
Programming Exercises.
Single Implementation Inheritance.
Object-oriented Programming Concepts.
Review Questions.
Overriding and Hiding Members.
Instance Method Overriding.
Field Hiding.
Static Method Hiding.
Overriding vs. Overloading.
Object Reference super.
Review Questions.
Chaining Constructors using this() and super().
this() Constructor Call.
super() Constructor Call.
Review Questions.
Interfaces.
Defining Interfaces.
Method Prototype Declarations.
Implementing Interfaces.
Extending Interfaces.
Constants in Interfaces.
Review Questions.
Completing the Type Hierarchy.
Assigning, Passing and Casting Reference Values.
Reference Value Assignment Conversions.
Parameter Passing Conversions.
Reference Casting and instanceof Operator.
Converting References of Class and Interface Types.
Review Questions.
Polymorphism and Dynamic Method Lookup.
Review Questions.
Inheritance vs. Aggregation.
Encapsulation.
Choosing between Inheritance and Aggregation.
Review Questions.
Chapter Summary.
Programming Exercises.
Overview of Nested Classes and Interfaces.
Static Member Classes and Interfaces.
Declaring and Using Static Member Classes and Interfaces.
Accessing Members in Enclosing Context.
Non-static Member Classes.
Instantiating Non-static Member Classes.
Accessing Members in Enclosing Context.
Review Questions.
Local Classes.
Accessing Declarations in Enclosing Context.
Instantiating Local Classes.
Anonymous Classes.
Extending an Existing Class.
Implementing an Interface.
Instantiating Anonymous Classes.
Accessing Declarations in Enclosing Context.
Review Questions.
Chapter Summary.
Programming Exercise.
Garbage Collection.
Reachable References.
Facilitating Garbage Collection.
Object Finalization.
Finalizer Chaining.
Invoking Garbage Collection.
Review Questions.
Initializers.
Field Initializer Expressions.
Static Initializer Blocks.
Instance Initializer Blocks.
Constructing Initial Object State.
Review Questions.
Chapter Summary.
Multitasking.
Overview of Threads.
The Main Thread.
Thread Creation.
Implementing the Runnable Interface.
Extending the Thread Class.
Review Questions.
Synchronization.
Locks.
Synchronized Methods.
Synchronized Blocks.
Review Questions.
Thread Transitions.
Thread States.
Thread Priorities.
Thread Scheduler.
Running and Yielding.
Sleeping and Waking Up.
Waiting and Notifying.
Joining.
Blocking for I/O.
Thread Termination.
Deadlocks.
Review Questions.
Chapter Summary.
Programming Exercises.
Overview of the java.lang package.
The Object Class.
Review Questions.
The Wrapper Classes.
Common Wrapper Class Constructors.
Common Wrapper Class Utility Methods.
Numeric Wrapper Classes.
Character Class.
Boolean Class.
Review Questions.
The Math Class.
Miscellaneous Rounding Functions.
Exponential Functions.
Trigonometry Functions.
Pseudorandom Number Generator.
Review Questions.
The String Class.
Creating and Initializing Strings.
Reading Characters from a String.
Comparing Strings.
Character Case in a String.
Concatenation of Strings.
Searching for Characters and Substrings.
Extracting Substrings.
Converting Primitive Values and Objects to Strings.
Review Questions.
The StringBuffer Class.
Constructing String Buffers.
Reading and Changing Characters in String Buffers.
Constructing Strings from String Buffers.
Appending, Inserting and Deleting Characters in String Buffers.
Controlling String Buffer Capacity.
Review Questions.
Chapter Summary.
Programming Exercises.
The Collections Framework.
Core interfaces.
Implementations.
Collections.
Basic Operations.
Bulk Operations.
Array Operations.
Iterators.
Review Questions.
Sets.
HashSet and LinkedHashSet.
Lists.
ArrayList, LinkedList and Vector.
Review Questions.
Maps.
Basic Operations.
Bulk Operations.
Collection Views.
HashMap, LinkedHashMap and Hashtable.
Sorted Sets and Sorted Maps.
The Comparator Interface.
The Comparable Interface.
The SortedSet Interface.
The SortedMap Interface.
TreeSet and TreeMap.
Review Questions.
Implementing the equals(), hashCode() and compareTo() methods.
The equals() method.
The hashCode() method.
The compareTo() method.
Working with Collections.
Synchronized Collection Decorators.
Unmodifiable Collection Decorators.
Sorting Collections.
Searching in Collections.
Singleton Collections.
Other Utility Methods in the Collections Class.
Utility Methods in the Arrays Class.
Abstract Implementations.
Review Questions.
Chapter Summary.
Programming Exercise.
Preparing for the programmer exam.
Registering for the exam.
Obtaining an Exam Voucher.
Signing Up for the Test.
Contact Information.
After Taking the Exam.
How the Examination Is Conducted.
The Testing Locations.
Utilizing the Allotted time.
The Exam Program.
The Questions.
Types of Questions Asked.
Types of answers expected.
Topics Covered by the Questions.
Moving on to the developer exam.
Number Systems.
Binary, Octal and Hexadecimal Number System.
Converting Binary Numbers to Decimals.
Converting Octal and Hexadecimal Numbers to Decimals.
Relationship between Binary, Octal and Hex Numbers.
Converting Decimals.
Converting Decimals to Binary Numbers.
Converting Decimals to Octal and Hexadecimal Numbers.
Representing Integers.
Calculating 2's Complement.
Contents of the CD.
Installing the Programs.
Using the Programs.
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 and feedback 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.
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.
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:
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 CodeWe 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. Lines 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.
Download the Foreword
file related to this title.
Download the Index
file related to this title.