1Download from www.finelybook.com 7450911@qq.com
Playlists
Java in a Nutshell, Seventh Edition
History
by Ben Evans and David Flanagan
Topics
Copyright © 2019 Benjamin J. Evans and David Flanagan. All rights reserved.
Tutorials
Printed in the United States of America.
Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.
Offers & Deals
Highlights
O’Reilly books may be purchased for educational, business, or sales promotional use. Online
editions are also available for most titles (http://oreilly.com/safari). For more information,
contact our corporate/institutional sales department: 8009989938 or corporate@oreilly.com.
Settings
Editor: Virginia Wilson
Support
Production Editor: Justin Billing
Sign Out
Copyeditor: Jasmine Kwityn
Proofreader: Rachel Monaghan
Indexer: WordCo Indexing Services, Inc.
Interior Designer: David Futato
Cover Designer: Karen Montgomery
Illustrator: Rebecca Demarest
December 2018: Seventh Edition
Revision History for the Seventh Edition
20181130: First Release
See http://oreilly.com/catalog/errata.csp?isbn=9781492037255 for release details.
The O’Reilly logo is a registered trademark of O’Reilly Media, Inc. Java in a Nutshell, the
cover image, and related trade dress are trademarks of O’Reilly Media, Inc.
The views expressed in this work are those of the authors, and do not represent the publisher’s
2Download from www.finelybook.com 7450911@qq.com
views. While the publisher and the authors have used good faith efforts to ensure that the
information and instructions contained in this work are accurate, the publisher and the authors
disclaim all responsibility for errors or omissions, including without limitation responsibility for
damages resulting from the use of or reliance on this work. Use of the information and
instructions contained in this work is at your own risk. If any code samples or other technology
this work contains or describes is subject to open source licenses or the intellectual property
rights of others, it is your responsibility to ensure that your use thereof complies with such
licenses and/or rights.
9781492037255
[LSI]
3Download from www.finelybook.com 7450911@qq.com
History
Part I. Introducing Java
Topics
Tutorials
Part I is an introduction to the Java language and the Java platform. These chapters provide
enough information for you to get started using Java right away:
Chapter 1, Introduction to the Java Environment
Offers & Deals
Chapter 2, Java Syntax from the Ground Up
Highlights
Chapter 3, ObjectOriented Programming in Java
Chapter 4, The Java Type System
Settings
Chapter 5, Introduction to ObjectOriented Design in Java
Chapter 6, Java’s Approach to Memory and Concurrency
Support
Sign Out
4Download from www.finelybook.com 7450911@qq.com
History
Chapter 1. Introduction to the Java
Environment
Topics
Welcome to Java 11.
Tutorials
Offers & Deals
That version number probably surprises you as much as it does us. It seems like only yesterday
that Java 5 was the new thing, and yet here we are, 14 years and 6 major versions later.
Highlights
You may be coming to the Java ecosystem from another language, or maybe this is your first
programming language. Whatever road you may have traveled to get here, welcome—we’re
glad you’ve arrived.
Settings
Support
Java is a powerful, generalpurpose programming environment. It is one of the most widely
used programming languages in the world, and has been exceptionally successful in business
and enterprise computing.
Sign Out
In this chapter, we’ll set the scene by describing the Java language (which programmers write
their applications in), the Java Virtual Machine (which executes those applications), and the
Java ecosystem (which provides a lot of the value of the programming environment to
development teams).
We’ll briefly cover the history of the Java language and virtual machine, before moving on to
discuss the lifecycle of a Java program and clear up some common questions about the
differences between Java and other environments.
At the end of the chapter, we’ll introduce Java security, and discuss some of the aspects of Java
that relate to secure coding.
The Language, the JVM, and the Ecosystem
.
The Java programming environment has been around since the late 1990s. It comprises the Java
language, and the supporting runtime, otherwise known as the Java Virtual Machine (JVM).
At the time that Java was initially developed, this split was considered novel, but recent trends
5Download from www.finelybook.com 7450911@qq.com
in software development have made it more commonplace. Notably, Microsoft’s .NET
environment, announced a few years after Java, adopted a very similar approach to platform
architecture.
One important difference between Microsoft’s .NET platform and Java is that Java was always
conceived as a relatively open ecosystem of multiple vendors, albeit led by a steward who owns
the technology. Throughout Java’s history, these vendors have both cooperated and competed
on aspects of Java technology.
One of the main reasons for the success of Java is that this ecosystem is a standardized
environment. This means there are specifications for the technologies that comprise the
environment. These standards give the developer and consumer confidence that the technology
will be compatible with other components, even if they come from a different technology
vendor.
The current steward of Java is Oracle Corporation (who acquired Sun Microsystems, the
originator of Java). Other corporations, such as Red Hat, IBM, Amazon, AliBaba, SAP, Azul
Systems, and Fujitsu are also heavily involved in producing implementations of standardized
Java technologies.
TIP
As of Java 11, the primary reference implementation of Java is the open source
OpenJDK, which many of these companies collaborate on and base their shipping
products upon.
Java actually comprises several different but related environments and specifications, such as
Java Mobile Edition (Java ME),
(Java EE).
related to when certain features were introduced into the platform.
In this book, we’ll only cover Java SE, version 11, with some historical notes
2
1
Java Standard Edition (Java SE), and Java Enterprise Edition
We will have more to say about standardization later, so let’s move on to discuss the Java
language and JVM as separate but related concepts.
What Is the Java Language?
Java programs are written as source code in the Java language. This is a humanreadable
programming language, which is strictly class based and object oriented. The language syntax is
deliberately modeled on that of C and C++ and it was explicitly intended to be familiar to
6Download from www.finelybook.com 7450911@qq.com
programmers coming from those languages.
NOTE
Although the source code is similar to C++, in practice Java includes features and a
managed runtime that has more in common with more dynamic languages such as
Smalltalk.
Java is considered to be relatively easy to read and write (if occasionally a bit verbose). It has a
rigid grammar and simple program structure, and is intended to be easy to learn and to teach. It
builds on industry experience with languages like C++ and tries to remove complex features as
well as preserving “what works” from previous programming languages.
Overall, Java is intended to provide a stable, solid base for companies to develop business
critical applications. As a programming language, it has a relatively conservative design and a
slow rate of change. These properties are a conscious attempt to serve the goal of protecting the
investment that organizations have made in Java technology.
The language has undergone gradual revision (but no complete rewrites) since its inception in
1996. This does mean that some of Java’s original design choices, which were expedient in the
late 1990s, are still affecting the language today—see Chapters 2 and 3 for more details.
Java 8 added the most radical changes seen in the language for almost a decade (some would
say since the birth of Java). Features like lambda expressions and the overhaul of the core
Collections code were enormously popular and changed forever the way that Java developers
write code. Since then, the platform has produced a release (Java 9) that adds a major (and long
delayed) feature: the platform modules system (JPMS).
With that release, the project has transitioned to a new, much faster release model where new
Java versions are released every six months—bringing us up to Java 11. The Java language is
governed by the Java Language Specification (JLS), which defines how a conforming
implementation must behave.
What Is the JVM?
The JVM is a program that provides the runtime environment necessary for Java programs to
execute. Java programs cannot run unless there is a JVM available for the appropriate hardware
and OS platform we wish to execute on.
7Download from www.finelybook.com 7450911@qq.com
Fortunately, the JVM has been ported to run on a large number of environments—anything from
a settop box or Bluray player to a huge mainframe will probably have a JVM available for it.
Java programs are typically started from a command line like this:
java
This brings up the JVM as an operating system process that provides the Java runtime
environment, and then executes our program in the context of the freshly started (and empty)
virtual machine.
It is important to understand that when the JVM takes in a Java program for execution, the
program is not provided as Java language source code. Instead, the Java language source must
have been converted (or compiled) into a form known as Java bytecode. Java bytecode must be
supplied to the JVM in a format called class files (which always have a .class extension).
The JVM provides an execution environment for the program. It starts an interpreter for the
bytecode form of the program that steps through one bytecode instruction at a time. However,
production JVMs also provide a runtime compiler that will accelerate the important parts of the
program by replacing them with equivalent compiled machine code.
You should also be aware that both the JVM and the user program are capable of spawning
additional threads of execution, so that a user program may have many different functions
running simultaneously.
The design of the JVM built on many years of experience with earlier programming
environments, notably C and C++, so we can think of it as having several different goals—
which are all intended to make life easier for the programmer:
Comprise a container for application code to run inside
Provide a secure and reliable execution environment as compared to C/C++
Take memory management out of the hands of developers
Provide a crossplatform execution environment
These objectives are often mentioned together in discussions of the platform.
We’ve already mentioned the first of these goals, when we discussed the JVM and its bytecode
interpreter—it functions as the container for application code.
8Download from www.finelybook.com 7450911@qq.com