Preface

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 head start 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.

New Developments

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 deemphasized 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 (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 reach 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.

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

Using This Book

This book is organized roughly as follows:

  • Chapters Chapter 1 and Chapter 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 Chapter 4 and Chapter 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 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.

  • Chapter 11 and Chapter 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.

Online Resources

There are many online sources for information about Java.

Oracle’s official website for Java topics is https://www.oracle.com/technetwork/java/index.html; 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!

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

  • Threads

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).

Using Code Examples

If you have a technical question or a problem using the code examples, please send email to .

This book is here to help you get your job done. In general, if example code is offered with this book, you may use it in your programs and documentation. You do not need to contact us for permission unless you’re reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing examples from O’Reilly books does require permission. Answering a question by citing this book and quoting example code does not require permission. Incorporating a significant amount of example code from this book into your product’s documentation does require permission.

We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example: “Learning Java, Fifth Edition, by Marc Loy, Patrick Niemeyer, and Daniel Leuck. Copyright 2020 Marc Loy, Patrick Niemeyer, and Daniel Leuck, 978-1-449-31924-3.”

If you feel your use of code examples falls outside fair use or the permission given above, feel free to contact us at .

O’Reilly Online Learning

Note

For more than 40 years, O’Reilly Media has provided technology and business training, knowledge, and insight to help companies succeed.

Our unique network of experts and innovators share their knowledge and expertise through books, articles, conferences, and our online learning platform. O’Reilly’s online learning platform gives you on-demand access to live training courses, in-depth learning paths, interactive coding environments, and a vast collection of text and video from O’Reilly and 200+ other publishers. For more information, please visit http://oreilly.com.

How to Contact Us

Please address comments and questions concerning this book to the publisher:

  • O’Reilly Media, Inc.
  • 1005 Gravenstein Highway North
  • Sebastopol, CA 95472
  • 800-998-9938 (in the United States or Canada)
  • 707-829-0515 (international or local)
  • 707-829-0104 (fax)

We have a web page for this book where we list errata and any additional information. You can access this page at http://oreil.ly/Java_5E.

The example code can be found separately on GitHub. There are two repositories for this book: the main examples and the web examples. More details on accessing and working the examples is provided in Appendix A.

Email to comment or ask technical questions about this book.

For more information about our books, courses, conferences, and news, see our website at http://www.oreilly.com.

Find us on Facebook: http://facebook.com/oreilly

Follow us on Twitter: http://twitter.com/oreillymedia

Watch us on YouTube: http://www.youtube.com/oreillymedia

Acknowledgments

Many people have contributed to putting this book together, both in its Exploring Java incarnation and in its current form as Learning Java. Foremost, we would like to thank Tim O’Reilly for giving us the opportunity to write this book. Thanks to Mike Loukides, the series editor, whose patience and experience continues to guide us. other folks from O’Reilly including Amelia Blevins, Zan McQuade, Corbin Collins, and Jessica Haberman have provided consistent wisdom and encouragement. We could not have asked for a more skillful or responsive team of people with whom to work.

The original version of the glossary came from David Flanagan’s book Java in a Nutshell (O’Reilly). We also borrowed several class hierarchy diagrams from David’s book. These diagrams were based on similar diagrams by Charles L. Perkins.

Warm thanks to Ron Becker for sound advice and interesting ideas as seen from the perspective of a layman well removed from the programming world. Thanks also to James Elliott and Dan Leuck for their excellent and timely feedback on the technical content of this edition. As with so many things in the programming world, extra eyes are indispensible and we are lucky to have had such attentive pairs in our corner.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.183.14