The Java® Tutorial: A Short Course on the Basics, 6th Edition

Book description

The Java® Tutorial, 6th Edition, is based on the Java Platform, Standard Edition (Java SE) 8. This revised and updated edition introduces the new features added to the platform, including lambda expressions, default methods, aggregate operations, and more. An accessible and practical guide for programmers of any level, this book focuses on how to use the rich environment provided by Java to build applications, applets, and components.

Expanded coverage includes a chapter on the Date-Time API and a new chapter on annotations, with sections on type annotations and pluggable type systems as well as repeating annotations.

In addition, the updated sections “Security in Rich Internet Applications” and “Guidelines for Securing Rich Internet Applications” address key security topics. The latest deployment best practices are described in the chapter “Deployment in Depth.”

If you plan to take one of the Java SE 8 certification exams, this book can help. A special appendix, “Preparing for Java Programming Language Certification,” details the items covered on the available exams. Check online for updates.

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. This book is based on the online tutorial hosted on Oracle Corporation’s website at http://docs.oracle.com/javase/tutorial.

Table of contents

  1. About This eBook
  2. Title Page
  3. Copyright Page
  4. Preface
    1. Who Should Read This Book?
    2. How to Use This Book
    3. Acknowledgments
  5. About the Authors
  6. 1. Getting Started
    1. The Java Technology Phenomenon
      1. The Java Programming Language
      2. The Java Platform
      3. What Can Java Technology Do?
      4. How Will Java Technology Change My Life?
    2. The “Hello World!” Application
      1. “Hello World!” for the NetBeans IDE
      2. “Hello World!” for Microsoft Windows
      3. “Hello World!” for Solaris and Linux
    3. A Closer Look at the “Hello World!” Application
      1. Source Code Comments
      2. The HelloWorldApp Class Definition
      3. The main Method
    4. Common Problems (and Their Solutions)
      1. Compiler Problems
      2. Runtime Problems
    5. Questions and Exercises: Getting Started
      1. Questions
      2. Exercises
      3. Answers
  7. 2. Object-Oriented Programming Concepts
    1. What Is an Object?
    2. What Is a Class?
    3. What Is Inheritance?
    4. What Is an Interface?
    5. What Is a Package?
    6. Questions and Exercises: Object-Oriented Programming Concepts
      1. Questions
      2. Exercises
      3. Answers
  8. 3. Language Basics
    1. Variables
      1. Naming
      2. Primitive Data Types
      3. Arrays
      4. Summary of Variables
      5. Questions and Exercises: Variables
    2. Operators
      1. Assignment, Arithmetic, and Unary Operators
      2. Equality, Relational, and Conditional Operators
      3. Bitwise and Bit Shift Operators
      4. Summary of Operators
      5. Questions and Exercises: Operators
    3. Expressions, Statements, and Blocks
      1. Expressions
      2. Statements
      3. Blocks
      4. Questions and Exercises: Expressions, Statements, and Blocks
    4. Control Flow Statements
      1. The if-then and if-then-else Statements
      2. The switch Statement
      3. The while and do-while Statements
      4. The for Statement
      5. Branching Statements
      6. Summary of Control Flow Statements
      7. Questions and Exercises: Control Flow Statements
  9. 4. Classes and Objects
    1. Classes
      1. Declaring Classes
      2. Declaring Member Variables
      3. Defining Methods
      4. Providing Constructors for Your Classes
      5. Passing Information to a Method or a Constructor
    2. Objects
      1. Creating Objects
      2. Using Objects
    3. More on Classes
      1. Returning a Value from a Method
      2. Using the this Keyword
      3. Controlling Access to Members of a Class
      4. Understanding Class Members
      5. Initializing Fields
      6. Summary of Creating and Using Classes and Objects
      7. Questions and Exercises: Classes
      8. Questions and Exercises: Objects
    4. Nested Classes
      1. Why Use Nested Classes?
      2. Static Nested Classes
      3. Inner Classes
      4. Shadowing
      5. Serialization
      6. Inner Class Example
      7. Local and Anonymous Classes
      8. Modifiers
      9. Local Classes
      10. Anonymous Classes
      11. Lambda Expressions
      12. When to Use Nested Classes, Local Classes, Anonymous Classes, and Lambda Expressions
      13. Questions and Exercises: Nested Classes
    5. Enum Types
      1. Questions and Exercises: Enum Types
  10. 5. Annotations
    1. Annotations Basics
      1. The Format of an Annotation
      2. Where Annotations Can Be Used
    2. Declaring an Annotation Type
    3. Predefined Annotation Types
      1. Annotation Types Used by the Java Language
      2. Annotations That Apply to Other Annotations
    4. Type Annotations and Pluggable Type Systems
    5. Repeating Annotations
      1. Step 1: Declare a Repeatable Annotation Type
      2. Step 2: Declare the Containing Annotation Type
      3. Retrieving Annotations
      4. Design Considerations
    6. Questions and Exercises: Annotations
      1. Questions
      2. Exercise
      3. Answers
  11. 6. Interfaces and Inheritance
    1. Interfaces
      1. Interfaces in Java
      2. Interfaces as APIs
      3. Defining an Interface
      4. Implementing an Interface
      5. Using an Interface as a Type
      6. Evolving Interfaces
      7. Default Methods
      8. Summary of Interfaces
      9. Questions and Exercises: Interfaces
    2. Inheritance
      1. The Java Platform Class Hierarchy
      2. An Example of Inheritance
      3. What You Can Do in a Subclass
      4. Private Members in a Superclass
      5. Casting Objects
      6. Multiple Inheritance of State, Implementation, and Type
      7. Overriding and Hiding Methods
      8. Polymorphism
      9. Hiding Fields
      10. Using the Keyword super
      11. Object as a Superclass
      12. Writing Final Classes and Methods
      13. Abstract Methods and Classes
      14. Summary of Inheritance
      15. Questions and Exercises: Inheritance
  12. 7. Generics
    1. Why Use Generics?
    2. Generic Types
      1. A Simple Box Class
      2. A Generic Version of the Box Class
      3. Type Parameter Naming Conventions
      4. Invoking and Instantiating a Generic Type
      5. The Diamond
      6. Multiple Type Parameters
      7. Parameterized Types
      8. Raw Types
    3. Generic Methods
    4. Bounded Type Parameters
      1. Multiple Bounds
      2. Generic Methods and Bounded Type Parameters
    5. Generics, Inheritance, and Subtypes
      1. Generic Classes and Subtyping
    6. Type Inference
      1. Type Inference and Generic Methods
      2. Type Inference and Instantiation of Generic Classes
      3. Type Inference and Generic Constructors of Generic and Nongeneric Classes
      4. Target Types
    7. Wildcards
      1. Upper-Bounded Wildcards
      2. Unbounded Wildcards
      3. Lower-Bounded Wildcards
      4. Wildcards and Subtyping
      5. Wildcard Capture and Helper Methods
      6. Guidelines for Wildcard Use
    8. Type Erasure
      1. Erasure of Generic Types
      2. Erasure of Generic Methods
      3. Effects of Type Erasure and Bridge Methods
      4. Nonreifiable Types and Varargs Methods
    9. Restrictions on Generics
      1. Cannot Instantiate Generic Types with Primitive Types
      2. Cannot Create Instances of Type Parameters
      3. Cannot Declare Static Fields Whose Types Are Type Parameters
      4. Cannot Use Casts or instanceof with Parameterized Types
      5. Cannot Create Arrays of Parameterized Types
      6. Cannot Create, Catch, or Throw Objects of Parameterized Types
      7. Cannot Overload a Method Where the Formal Parameter Types of Each Overload Erase to the Same Raw Type
    10. Questions and Exercises: Generics
      1. Answers
  13. 8. Packages
    1. Creating and Using Packages
      1. Creating a Package
      2. Naming a Package
      3. Using Package Members
      4. Managing Source and Class Files
      5. Summary of Creating and Using Packages
    2. Questions and Exercises: Creating and Using Packages
      1. Questions
      2. Exercises
      3. Answers
  14. 9. Numbers and Strings
    1. Numbers
      1. The Numbers Classes
      2. Formatting Numeric Print Output
      3. Beyond Basic Arithmetic
      4. Autoboxing and Unboxing
      5. Summary of Numbers
      6. Questions and Exercises: Numbers
    2. Characters
      1. Escape Sequences
    3. Strings
      1. Creating Strings
      2. String Length
      3. Concatenating Strings
      4. Creating Format Strings
      5. Converting between Numbers and Strings
      6. Manipulating Characters in a String
      7. Comparing Strings and Portions of Strings
      8. The StringBuilder Class
      9. Summary of Characters and Strings
      10. Questions and Exercises: Characters and Strings
  15. 10. Exceptions
    1. What Is an Exception?
    2. The Catch or Specify Requirement
      1. The Three Kinds of Exceptions
      2. Bypassing Catch or Specify
    3. Catching and Handling Exceptions
      1. The try Block
      2. The catch Blocks
      3. The finally Block
      4. The try-with-resources Statement
      5. Putting It All Together
    4. Specifying the Exceptions Thrown by a Method
    5. How to Throw Exceptions
      1. The throw Statement
      2. Throwable Class and Its Subclasses
      3. Error Class
      4. Exception Class
      5. Chained Exceptions
      6. Creating Exception Classes
    6. Unchecked Exceptions: The Controversy
    7. Advantages of Exceptions
      1. Advantage 1: Separating Error-Handling Code from “Regular” Code
      2. Advantage 2: Propagating Errors Up the Call Stack
      3. Advantage 3: Grouping and Differentiating Error Types
    8. Summary
    9. Questions and Exercises: Exceptions
      1. Questions
      2. Exercises
      3. Answers
  16. 11. Basic I/O and NIO.2
    1. I/O Streams
      1. Byte Streams
      2. Character Streams
      3. Buffered Streams
      4. Scanning and Formatting
      5. I/O from the Command Line
      6. Data Streams
      7. Object Streams
    2. File I/O (Featuring NIO.2)
      1. What Is a Path? (And Other File System Facts)
      2. The Path Class
      3. File Operations
      4. Checking a File or Directory
      5. Deleting a File or Directory
      6. Copying a File or Directory
      7. Moving a File or Directory
      8. Managing Metadata (File and File Store Attributes)
      9. Reading, Writing, and Creating Files
      10. Random Access Files
      11. Creating and Reading Directories
      12. Links, Symbolic or Otherwise
      13. Walking the File Tree
      14. Finding Files
      15. Watching a Directory for Changes
      16. Other Useful Methods
      17. Legacy File I/O Code
    3. Summary
    4. Questions and Exercises: Basic I/O
      1. Questions
      2. Exercises
      3. Answers
  17. 12. Collections
    1. Introduction to Collections
      1. What Is a Collections Framework?
      2. Benefits of the Java Collections Framework
    2. Interfaces
      1. The Collection Interface
      2. Traversing Collections
      3. Collection Interface Bulk Operations
      4. Collection Interface Array Operations
      5. The Set Interface
      6. The List Interface
      7. The Queue Interface
      8. The Deque Interface
      9. The Map Interface
      10. Object Ordering
      11. The SortedSet Interface
      12. The SortedMap Interface
      13. Summary of Interfaces
      14. Questions and Exercises: Interfaces
    3. Aggregate Operations
      1. Pipelines and Streams
      2. Differences between Aggregate Operations and Iterators
      3. Reduction
      4. Parallelism
      5. Side Effects
      6. Questions and Exercises: Aggregate Operations
    4. Implementations
      1. Set Implementations
      2. List Implementations
      3. Map Implementations
      4. Queue Implementations
      5. Deque Implementations
      6. Wrapper Implementations
      7. Convenience Implementations
      8. Summary of Implementations
      9. Questions and Exercises: Implementations
    5. Algorithms
      1. Sorting
      2. Shuffling
      3. Routine Data Manipulation
      4. Searching
      5. Composition
      6. Finding Extreme Values
    6. Custom Collection Implementations
      1. Reasons to Write an Implementation
      2. How to Write a Custom Implementation
    7. Interoperability
      1. Compatibility
      2. API Design
  18. 13. Concurrency
    1. Processes and Threads
      1. Processes
      2. Threads
    2. Thread Objects
      1. Defining and Starting a Thread
      2. Pausing Execution with Sleep
      3. Interrupts
      4. Joins
      5. The SimpleThreads Example
    3. Synchronization
      1. Thread Interference
      2. Memory Consistency Errors
      3. Synchronized Methods
      4. Intrinsic Locks and Synchronization
      5. Atomic Access
    4. Liveness
      1. Deadlock
      2. Starvation and Livelock
    5. Guarded Blocks
    6. Immutable Objects
      1. A Synchronized Class Example
      2. A Strategy for Defining Immutable Objects
    7. High-Level Concurrency Objects
      1. Lock Objects
      2. Executors
      3. Concurrent Collections
      4. Atomic Variables
      5. Concurrent Random Numbers
    8. Questions and Exercises: Concurrency
      1. Question
      2. Exercises
      3. Answers
  19. 14. Regular Expressions
    1. Introduction
      1. What Are Regular Expressions?
      2. How Are Regular Expressions Represented in This Package?
    2. Test Harness
    3. String Literals
      1. Metacharacters
    4. Character Classes
      1. Simple Classes
    5. Predefined Character Classes
    6. Quantifiers
      1. Zero-Length Matches
      2. Capturing Groups and Character Classes with Quantifiers
      3. Differences among Greedy, Reluctant, and Possessive Quantifiers
    7. Capturing Groups
      1. Numbering
      2. Backreferences
    8. Boundary Matchers
    9. Methods of the Pattern Class
      1. Creating a Pattern with Flags
      2. Embedded Flag Expressions
      3. Using the matches(String,CharSequence) Method
      4. Using the split(String) Method
      5. Other Utility Methods
      6. Pattern Method Equivalents in java.lang.String
    10. Methods of the Matcher Class
      1. Index Methods
      2. Study Methods
      3. Replacement Methods
      4. Using the start and end Methods
      5. Using the matches and lookingAt Methods
      6. Using replaceFirst(String) and replaceAll(String)
      7. Using appendReplacement(StringBuffer,String) and appendTail(StringBuffer)
      8. Matcher Method Equivalents in java.lang.String
    11. Methods of the PatternSyntaxException Class
    12. Unicode Support
      1. Matching a Specific Code Point
      2. Unicode Character Properties
    13. Questions and Exercises: Regular Expressions
      1. Questions
      2. Exercise
      3. Answers
  20. 15. The Platform Environment
    1. Configuration Utilities
      1. Properties
      2. Command-Line Arguments
      3. Environment Variables
      4. Other Configuration Utilities
    2. System Utilities
      1. Command-Line I/O Objects
      2. System Properties
      3. The Security Manager
      4. Miscellaneous Methods in System
    3. PATH and CLASSPATH
      1. Update the PATH Environment Variable (Microsoft Windows)
      2. Update the PATH Variable (Solaris, Linux, and OS X)
      3. Checking the CLASSPATH Variable (All Platforms)
    4. Questions and Exercises: The Platform Environment
      1. Question
      2. Exercise
      3. Answers
  21. 16. Packaging Programs in JAR Files
    1. Using JAR Files: The Basics
      1. Creating a JAR File
      2. Viewing the Contents of a JAR File
      3. Extracting the Contents of a JAR File
      4. Updating a JAR File
      5. Running JAR-Packaged Software
    2. Working with Manifest Files: The Basics
      1. Understanding the Default Manifest
      2. Modifying a Manifest File
      3. Setting an Application’s Entry Point
      4. Adding Classes to the JAR File’s Class Path
      5. Setting Package Version Information
      6. Sealing Packages within a JAR File
      7. Enhancing Security with Manifest Attributes
    3. Signing and Verifying JAR Files
      1. Understanding Signing and Verification
      2. Signing JAR Files
      3. Verifying Signed JAR Files
    4. Using JAR-Related APIs
      1. An Example: The JarRunner Application
      2. The JarClassLoader Class
      3. The JarRunner Class
    5. Questions and Exercises: Packaging Programs in JAR Files
      1. Questions
      2. Answers
  22. 17. Java Web Start
    1. Additional References
    2. Developing a Java Web Start Application
      1. Creating the Top JPanel Class
      2. Creating the Application
      3. Benefits of Separating Core Functionality from the Final Deployment Mechanism
      4. Retrieving Resources
    3. Deploying a Java Web Start Application
      1. Setting Up a Web Server
    4. Displaying a Customized Loading Progress Indicator
      1. Developing a Customized Loading Progress Indicator
      2. Specifying a Customized Loading Progress Indicator for a Java Web Start Application
    5. Running a Java Web Start Application
      1. Running a Java Web Start Application from a Browser
      2. Running a Java Web Start Application from the Java Cache Viewer
      3. Running a Java Web Start Application from the Desktop
    6. Java Web Start and Security
      1. Dynamic Downloading of HTTPS Certificates
    7. Common Java Web Start Problems
      1. “My Browser Shows the Java Network Launch Protocol (JNLP) File for My Application as Plain Text”
      2. “When I Try to Launch My JNLP File, I Get the Following Error”
    8. Questions and Exercises: Java Web Start
      1. Questions
      2. Exercises
      3. Answers
  23. 18. Applets
    1. Getting Started with Applets
      1. Defining an Applet Subclass
      2. Methods for Milestones
      3. Life Cycle of an Applet
      4. Applet’s Execution Environment
      5. Developing an Applet
      6. Deploying an Applet
    2. Doing More with Applets
      1. Finding and Loading Data Files
      2. Defining and Using Applet Parameters
      3. Displaying Short Status Strings
      4. Displaying Documents in the Browser
      5. Invoking JavaScript Code from an Applet
      6. Invoking Applet Methods from JavaScript Code
      7. Handling Initialization Status with Event Handlers
      8. Manipulating DOM of Applet’s Web Page
      9. Displaying a Customized Loading Progress Indicator
      10. Writing Diagnostics to Standard Output and Error Streams
      11. Developing Draggable Applets
      12. Communicating with Other Applets
      13. Working with a Server-Side Application
      14. What Applets Can and Cannot Do
    3. Solving Common Applet Problems
      1. “My Applet Does Not Display”
      2. “The Java Console Log Displays java.lang.ClassNotFoundException”
      3. “I Was Able to Build the Code Once, but Now the Build Fails Even Though There Are No Compilation Errors”
      4. “When I Try to Load a Web Page That Has an Applet, My Browser Redirects Me to www.java.com without Any Warning”
      5. “I Fixed Some Bugs and Rebuilt My Applet’s Source Code. When I Reload the Applet’s Web Page, My Fixes Are Not Showing Up”
    4. Questions and Exercises: Applets
      1. Questions
      2. Exercise
      3. Answers
  24. 19. Doing More with Java Rich Internet Applications
    1. Setting Trusted Arguments and Secure Properties
      1. System Properties
    2. JNLP API
      1. Accessing the Client Using the JNLP API
    3. Cookies
      1. Types of Cookies
      2. Cookie Support in RIAs
      3. Accessing Cookies
    4. Customizing the Loading Experience
    5. Security in Rich Internet Applications
    6. Guidelines for Securing RIAs
      1. Follow Secure Coding Guidelines
      2. Test with the Latest Version of the JRE
      3. Include Manifest Attributes
      4. Use a Signed JNLP File
      5. Sign and Time Stamp JAR Files
      6. Use the HTTPS Protocol
      7. Avoid Local RIAs
    7. Questions and Exercises: Doing More with Rich Internet Applications
      1. Questions
      2. Exercise
      3. Answers
  25. 20. Deployment in Depth
    1. User Acceptance of RIAs
    2. Deployment Toolkit
      1. Location of Deployment Toolkit Script
      2. Deploying an Applet
      3. Deploying a Java Web Start Application
      4. Checking the Client JRE Software Version
    3. Java Network Launch Protocol
      1. Structure of the JNLP File
    4. Deployment Best Practices
      1. Reducing the Download Time
      2. Avoiding Unnecessary Update Checks
      3. Ensuring the Presence of the JRE Software
    5. Questions and Exercises: Deployment in Depth
      1. Questions
      2. Exercise
      3. Answers
  26. 21. Date-Time
    1. Date-Time Overview
    2. Date-Time Design Principles
      1. Clear
      2. Fluent
      3. Immutable
      4. Extensible
    3. The Date-Time Packages
    4. Method Naming Conventions
    5. Standard Calendar
    6. Overview
    7. DayOfWeek and Month Enums
      1. DayOfWeek
      2. Month
    8. Date Classes
      1. LocalDate
      2. YearMonth
      3. MonthDay
      4. Year
    9. Date and Time Classes
      1. LocalTime
      2. LocalDateTime
    10. Time Zone and Offset Classes
      1. ZoneId and ZoneOffset
      2. The Date-Time Classes
    11. Instant Class
    12. Parsing and Formatting
      1. Parsing
      2. Formatting
    13. The Temporal Package
      1. Temporal and TemporalAccessor
      2. ChronoField and IsoFields
      3. ChronoUnit
      4. Temporal Adjuster
      5. Temporal Query
    14. Period and Duration
      1. Duration
      2. ChronoUnit
      3. Period
    15. Clock
    16. Non-ISO Date Conversion
      1. Converting to a Non-ISO-Based Date
      2. Converting to an ISO-Based Date
    17. Legacy Date-Time Code
      1. Interoperability with Legacy Code
      2. Mapping java.util Date and Time Functionality to java.time
      3. Date and Time Formatting
    18. Summary
    19. Questions and Exercises: Date-Time
      1. Questions
      2. Exercises
      3. Answers
  27. 22. Introduction to JavaFX
  28. Appendix. Preparation for Java Programming Language Certification
    1. Programmer Level I Exam
      1. Section 1: Java Basics
      2. Section 2: Working with Java Data Types
      3. Section 3: Using Operators and Decision Constructs
      4. Section 4: Creating and Using Arrays
      5. Section 5: Using Loop Constructs
      6. Section 6: Working with Methods and Encapsulation
      7. Section 7: Working with Inheritance
      8. Section 8: Handling Exceptions
      9. Section 9: Working with Selected Classes from the Java API
    2. Programmer Level II Exam
    3. Java SE 8 Upgrade Exam
      1. Section 1: Lambda Expressions
      2. Section 2: Using Built-In Lambda Types
      3. Section 3: Filtering Collections with Lambdas
      4. Section 4: Collection Operations with Lambda
      5. Section 5: Parallel Streams
      6. Section 6: Lambda Cookbook
      7. Section 7: Method Enhancements
      8. Section 8: Use Java SE 8 Date/Time API
      9. Section 9: JavaScript on Java with Nashorn
  29. Index
  30. Code Snippets

Product information

  • Title: The Java® Tutorial: A Short Course on the Basics, 6th Edition
  • Author(s): Raymond Gallardo, Scott Hommel, Sowmya Kannan, Joni Gordon, Sharon Biocca Zakhour
  • Release date: December 2014
  • Publisher(s): Addison-Wesley Professional
  • ISBN: 9780134034706

You might also like

book

Guru's Guide to SQL Server™ Stored Procedures, XML, and HTML, The

by Ken Henderson

"This is a book that deserves a prominent place by anyone who aspires to be a …

article

Reinventing the Organization for GenAI and LLMs

by Ethan Mollick

Previous technology breakthroughs did not upend organizational structure, but generative AI and LLMs will. We now …

book

リーダブルコード ―より良いコードを書くためのシンプルで実践的なテクニック

by Dustin Boswell, Trevor Foucher, 角 征典

美しいコードを見ると感動する。優れたコードは見た瞬間に何をしているかが伝わってくる。そういうコードは使うのが楽しいし、自分のコードもそうあるべきだと思わせてくれる。本書の目的は、君のコードを良くすることだ。(本書「はじめに」より) コードは理解しやすくなければならない。本書はこの原則を日々のコーディングの様々な場面に当てはめる方法を紹介します。名前の付け方、コメントの書き方など表面上の改善について。コードを動かすための制御フロー、論理式、変数などループとロジックについて。またコードを再構成するための方法。さらにテストの書き方などについて、楽しいイラストと共に説明しています。日本語版ではRubyやgroongaのコミッタとしても著名な須藤功平氏による解説を収録。

article

Three Ways to Sell Value in B2B Markets

by Joona Keränen, Harri Terho, Antti Saurama

As customers face pressure to reduce costs while maintaining profitability, value-based selling (VBS) has become critical …