Rough Cuts are manuscripts that are developed but not yet published, available through Safari. Rough Cuts provide you access to the very latest information on a given topic and offer you the opportunity to interact with the author to influence the final publication.
This is the Rough Cut version of the printed book.
The Java®Tutorial, Fifth Edition, is based on Release 7 of the Java Platform Standard Edition. This revised and updated edition introduces the new features added to the platform, including a section on NIO.2, the new file I/O API, and information on migrating legacy code to the new API. The deployment coverage has also been expanded, with new chapters such as “Doing More with Rich Internet Applications” and “Deployment in Depth,” and a section on the fork/join feature has been added to the chapter on concurrency. Information reflecting Project Coin developments, including the new try-with-resources statement, the ability to catch more than one type of exception with a single exception handler, support for binary literals, and diamond syntax, which results in cleaner generics code, has been added where appropriate. The chapters covering generics, Java Web Start, and applets have also been updated. In addition, if you plan to take one of the Java SE 7 certification exams, this guide can help. A special appendix, “Preparing for Java Programming Language Certification,” lists the three exams available, details the items covered on each exam, and provides cross-references to where more information about each topic appears in the text. All of the material has been thoroughly reviewed by members of Oracle Java engineering to ensure that the information is accurate and up to date.
Preface xxi
About the Authors xxv
Chapter 1: Getting Started 1
The Java Technology Phenomenon 1
The “Hello World!” Application 5
A Closer Look at the “Hello World!” Application 22
Common Problems (and Their Solutions) 25
Compiler Problems 25
Runtime Problems 27
Questions and Exercises: Getting Started 29
Chapter 2: Object-Oriented Programming Concepts 31
What Is an Object? 32
What Is a Class? 34
What Is Inheritance? 36
What Is an Interface? 37
What Is a Package? 38
Questions and Exercises: Object- Oriented Programming Concepts 38
Chapter 3: Language Basics 41
Variables 42
Operators 55
Expressions, Statements, and Blocks 65
Control Flow Statements 68
Chapter 4: Classes and Objects 85
Classes 86
Objects 98
More on Classes 106
Nested Classes 120
Enum Types 126
Annotations 129
Chapter 5: Interfaces and Inheritance 135
Interfaces 135
Inheritance 143
Chapter 6: Generics 163
Why Use Generics? 164
Generic Types 164
Generic Methods 170
Bounded Type Parameters 171
Generics, Inheritance, and Subtypes 173
Type Inference 176
Wildcards 179
Type Erasure 188
Restrictions on Generics 196
Questions and Exercises: Generics 200
Chapter 7: Packages 203
Creating a Package 205
Naming a Package 206
Using Package Members 207
Managing Source and Class Files 211
Questions and Exercises: Creating and Using Packages 214
Chapter 8: Numbers and Strings 217
Numbers 217
Characters 234
Strings 236
Questions and Exercises: Characters and Strings 254
Chapter 9: Exceptions 257
What Is an Exception? 258
The Catch or Specify Requirement 258
Catching and Handling Exceptions 261
Specifying the Exceptions Thrown by a Method 272
How to Throw Exceptions 273
Unchecked Exceptions: The Controversy 279
Advantages of Exceptions 280
Summary of Exceptions 285
Questions and Exercises: Exceptions 285