By Marc Loy, Patrick Niemeyer, and Daniel Leuck
“If you’re new to Java—or new to programming—this best-selling book will guide you through the language features and APIs of Java 11. With fun, compelling, and realistic examples, authors Marc Loy, Patrick Niemeyer, and Daniel Leuck introduce you to Java fundamentals—including its class libraries, programming techniques, and idioms—with an eye toward building real applications.
You’ll learn powerful new ways to manage resources and exceptions in your applications—along with core language features included in recent Java versions.
- Develop with Java, using the compiler, interpreter, and other tools
- Explore Java’s built-in thread facilities and concurrency package
- Learn text processing and the powerful regular expressions API
- Write advanced networked or web-based applications and services
About the Author
Marc Loy started with Java training at Sun Microsystems in the early days (shout out to HotJava!) and never looked back. He authored a number of early Java books and training courses, working with a wide variety of companies across the US, Europe and Asia along the way. For O’Reilly, Marc has served as co-author on Java Swing and Learning GNU Emacs. Currently in Ohio, Marc is a software developer and trainer specializing in user experience design and mobile applications.
Patrick Niemeyer became involved with Oak (Java’s predecessor) while working at Southwestern Bell Technology Resources. He is an independent consultant and author in the areas of networking and distributed applications. Pat is the author of BeanShell, a popular Java scripting language, as well as various other free goodies on the Net. Most recently, Pat has been developing enterprise architecture for A.G. Edwards. He currently lives in the Central West End area of St. Louis with various creatures.
Dan Leuck is the CEO of Ikayzo, a Tokyo and Honolulu-based interactive design and software development firm with customers including Sony, Oracle, Nomura, PIMCO and the federal government. He previously served as Senior Vice President of Research and Development for Tokyo-based ValueCommerce, Asia’s largest online marketing company, Global Head of Development for London-based LastMinute.com, Europe’s largest B2C website, and President of the US division of DML. Daniel has extensive experience managing teams of 150+ developers in five countries. He has served on numerous advisory boards and panels for companies such as Macromedia and Sun Microsystems. Daniel is active in the Java community, is a contributor to BeanShell, the project lead for SDL, and sits on numerous Java Community Process expert groups.
- Print length : 848 pages
- Publication date : March 30, 2020
- Publisher : O’Reilly Media; 5th edition (March 30, 2020)
- ASIN : B086L2NYWR
“This book is about the Java programming language and environment. Whether you are a software developer or just someone who uses the internet in your daily life, you’ve undoubtedly heard about Java. Its introduction was one of the most exciting developments in the history of the web, and Java applications have powered much of the growth of business on the internet. Java is, arguably, the most popular programming language in the world, used by millions of developers on almost every kind of computer imaginable. Java has surpassed languages such as C++ and Visual Basic in terms of developer demand and has become the de facto language for certain kinds of development — especially for web-based services. Most universities are now using Java in their introductory courses alongside the other important modern languages. Perhaps you are using this text in one of your classes right now!
This book gives you a thorough grounding in Java fundamentals and APIs. Learning Java, Fifth Edition, attempts to live up to its name by mapping out the Java language and its class libraries, programming techniques, and idioms. We’ll dig deep into interesting areas and at least scratch the surface of other popular topics. Other titles from O’Reilly pick up where we leave off and provide more comprehensive information on specific areas and applications of Java.
Whenever possible, we provide compelling, realistic, and fun examples and avoid merely cataloging features. The examples are simple, but hint at what can be done. We won’t be developing the next great “killer app” in these pages, but we hope to give you a starting point for many hours of experimentation and inspired tinkering that will lead you to develop one yourself.
Who Should Read This Book
“This book is for computer professionals, students, technical people, and Finnish hackers. It’s for everyone who has a need for hands-on experience with the Java language with an eye toward building real applications. This book could also be considered a crash course in object-oriented programming, networking, and user interfaces. As you learn about Java, you’ll also learn a powerful and practical approach to software development, beginning with a deep understanding of the fundamentals of Java and its APIs.
Superficially, Java looks like C or C++, so you’ll have a tiny headstart in using this book if you have some experience with one of these languages. If you do not, don’t worry. Don’t make too much of the syntactic similarities between Java and C or C++. In many respects, Java acts like more dynamic languages such as Smalltalk and Lisp. Knowledge of another object-oriented programming language should certainly help, although you may have to change some ideas and unlearn a few habits. Java is considerably simpler than languages such as C++ and Smalltalk. If you learn well from concise examples and personal experimentation, we think you’ll like this book.
The last part of this book branches out to discuss Java in the context of web applications, web services, and request processing, so you should be familiar with the basic ideas behind web browsers, servers, and documents.” (B086L2NYWR)
“This edition of Learning Java is actually the seventh edition — updated and retitled — of our original, popular Exploring Java. With each edition, we’ve taken great care not only to add new material covering additional features, but to thoroughly revise and update the existing content to synthesize the coverage and add years of real-world perspective and experience to these pages.
One noticeable change in recent editions is that we’ve de-emphasized the use of applets, reflecting their diminished role in recent years in creating interactive web pages. In contrast, we’ve greatly expanded our coverage of Java web applications and web services, which are now mature technologies.
We cover all of the important features of the latest “long-term support” release of Java, officially called Java Standard Edition (SE) 11, OpenJDK 11, but we also add in a few details from the “feature” releases of Java 12, Java 13, and Java 14. Sun Microsystems (Java’s keeper before Oracle) has changed the naming scheme many times over the years. Sun coined the term Java 2 to cover the major new features introduced in Java version 1.2 and dropped the term JDK in favor of SDK. With the sixth release, Sun skipped from Java version 1.4 to Java 5.0, but reprieved the term JDK and kept its numbering convention there. After that, we had Java 6, Java 7, and so on, and now we are at Java 14.
This release of Java reflects a mature language with occasional syntactic changes and updates to APIs and libraries. We’ve tried to capture these new features and update every example in this book to reflect not only the current Java practice, but style as well.” (B086L2NYWR)
New in This Edition (Java 11, 12, 13, 14)
“This edition of the book continues our tradition of rework to be as complete and up-to-date as possible. It incorporates changes from both the Java 11 — again, the long-term support version — and Java 12, 13, and 14 feature releases. (More on the specifics of the Java features included and excluded in recent releases in Chapter 13.) New topics in this edition include:
New language features, including type inference in generics and improved exception handling and automatic resource management syntax
New interactive playground, jshell, for trying out code snippets
The proposed switch expression
Basic lambda expressions
Updated examples and analysis throughout the book” (B086L2NYWR)
Using This Book
“This book is organized roughly as follows:
Chapters 1 and 2 provide a basic introduction to Java concepts and a tutorial to give you a jump-start on Java programming.
Chapter 3 discusses fundamental tools for developing with Java (the compiler, the interpreter, jshell, and the JAR file package).
Chapters 4 and 5 introduce programming fundamentals, then describe the Java language itself, beginning with the basic syntax and then covering classes and objects, exceptions, arrays, enumerations, annotations, and much more.
Chapter 6 covers exceptions, errors, and the logging facilities native to Java.
Chapter 7 covers collections alongside generics and parameterized types in Java.
Chapter 8 covers text processing, formatting, scanning, string utilities, and much of the core API utilities.
Chapter 9 covers the language’s built-in thread facilities.
Chapter 10 covers the basics of graphical user interface (GUI) development with Swing.
Chapter 11 covers Java I/O, streams, files, sockets, networking, and the NIO package.
Chapter 12 covers web applications using servlets, servlet filters, and WAR files, as well as web services.
Chapter 13 introduces the Java Community Process and highlights how to track future changes to Java while helping you retrofit existing code with new features, such as the lambda expressions introduced in Java 8.
If you’re like us, you don’t read books from front to back. If you’re really like us, you usually don’t read the preface at all. However, on the off chance that you will see this in time, here are a few suggestions:
If you are already a programmer and just need to learn Java in the next five minutes, you are probably looking for the examples. You might want to start by glancing at the tutorial in Chapter 2. If that doesn’t float your boat, you should at least look at the information in Chapter 3, which explains how to use the compiler and interpreter. This should get you started.
Chapters 11 and 12 are the places to head if you are interested in writing network or web-based applications and services. Networking remains one of the more interesting and important parts of Java.
Chapter 10 discusses Java’s graphics features and component architecture. You should read this if you are interested in writing desktop graphical Java applications.
Chapter 13 discusses how to stay on top of changes to the Java language itself, regardless of your particular focus.” (B086L2NYWR)
“There are many online sources for information about Java.
Oracle’s official website for Java topics is https://oreil.ly/Lo8QZ; look here for the software, updates, and Java releases. This is where you’ll find the reference implementation of the JDK, which includes the compiler, the interpreter, and other tools.
Oracle also maintains the OpenJDK site. This is the primary open source version of Java and the associated tools. We’ll be using the OpenJDK for all the examples in this book.
You should also visit O’Reilly’s site at http://oreilly.com/. There you’ll find information about other O’Reilly books for both Java and a growing array of other topics. You should also check out the online learning and conference options — O’Reilly is a real champion for education in all its forms.
And of course, you can check the home page for Learning Java!” (B086L2NYWR)
Conventions Used in This Book
“The font conventions used in this book are quite simple.
Italic is used for:
Pathnames, filenames, and program names
Internet addresses, such as domain names and URLs
New terms where they are defined
Program names, compilers, interpreters, utilities, and commands
Constant width is used for:
Anything that might appear in a Java program, including method names, variable names, and class names
Tags that might appear in an HTML or XML document
Keywords, objects, and environment variables
Constant width bold is used for:
Text that is typed by the user on the command line or in a dialog
Constant width italic is used for:
Replaceable items in code
In the main body of text, we always use a pair of empty parentheses after a method name to distinguish methods from variables and other creatures.
In the Java source listings, we follow the coding conventions most frequently used in the Java community. Class names begin with capital letters; variable and method names begin with lowercase. All the letters in the names of constants are capitalized. We don’t use underscores to separate words in a long name; following common practice, we capitalize individual words (after the first) and run the words together. For example: thisIsAVariable, thisIsAMethod(), ThisIsAClass, and THIS_IS_A_CONSTANT. Also, note that we differentiate between static and nonstatic methods when we refer to them. Unlike some books, we never write Foo.bar() to mean the bar() method of Foo unless bar() is a static method (paralleling the Java syntax in that case).” (B086L2NYWR)
Table of Contents:
Preface Who Should Read This Book
New Developments New in This Edition (Java 11, 12, 13, 14)
Using This Book
Conventions Used in This Book
Using Code Examples
O’Reilly Online Learning
How to Contact Us
- A Modern Language Enter Java Java’s Origins
A Virtual Machine
Java Compared with Other Languages
Safety of Design Simplify, Simplify, Simplify…
Type Safety and Method Binding
Dynamic Memory Management
Safety of Implementation The Verifier
Application and User-Level Security
A Java Road Map The Past: Java 1.0–Java 11
The Present: Java 14
- A First Application Java Tools and Environment Installing the JDK
Installing OpenJDK on Linux
Installing OpenJDK on macOS
Installing OpenJDK on Windows
Configuring IntelliJ IDEA and Creating a Project
Running the Project
Grabbing the Learning Java Examples
The main() Method
Classes and Objects
Variables and Class Types
The JComponent Class
Relationships and Finger-Pointing
Package and Imports
The paintComponent() Method
HelloJava2: The Sequel Instance Variables
The repaint() Method
Goodbye and Hello Again
- Tools of the Trade JDK Environment
The Java VM
Running Java Applications System Properties
The Classpath javap
The Java Compiler
The jar Utility
The pack200 Utility
- The Java Language Text Encoding
Comments Javadoc Comments
Variables and Constants
Types Primitive Types
A Word About Strings
Statements and Expressions Statements
Arrays Array Types
Array Creation and Initialization
Types and Classes and Arrays, Oh My!
- Objects in Java Classes Declaring and Instantiating Classes
Accessing Fields and Methods
Methods Local Variables
Initializing Local Variables
Argument Passing and References
Wrappers for Primitive Types
Object Creation Constructors
Working with Overloaded Constructors
Object Destruction Garbage Collection
Packages Importing Classes
Member Visibility and Access
Compiling with Packages
Advanced Class Design Subclassing and Inheritance
Anonymous Inner Classes
Organizing Content and Planning for Failure
- Error Handling and Logging Exceptions Exceptions and Error Classes
Checked and Unchecked Exceptions
The finally Clause
try with Resources
Assertions Enabling and Disabling Assertions
The Logging API Overview
A Simple Example
Logging Setup Properties
- Collections and Generics Collections The Collection Interface
The Map Interface
Type Limitations Containers: Building a Better Mousetrap
Can Containers Be Fixed?
Enter Generics Talking About Types
“There Is No Spoon” Erasure
Parameterized Type Relationships Why Isn’t a List a List