(This page has no text content)
2 1. Preface a. Who Should Read This Book b. New Developments i. New in This Edition (Java 11, 12, 13, 14) c. Using This Book d. Online Resources e. Conventions Used in This Book f. Using Code Examples g. O’Reilly Online Learning h. How to Contact Us i. Acknowledgments 2. 1. A Modern Language a. Enter Java i. Java’s Origins ii. Growing Up b. A Virtual Machine c. Java Compared with Other Languages d. Safety of Design i. Simplify, Simplify, Simplify… ii. Type Safety and Method Binding iii. Incremental Development iv. Dynamic Memory Management
3 v. Error Handling vi. Threads vii. Scalability e. Safety of Implementation i. The Verifier ii. Class Loaders iii. Security Managers f. Application and User-Level Security g. A Java Road Map i. The Past: Java 1.0–Java 11 ii. The Present: Java 14 iii. The Future iv. Availability 3. 2. A First Application a. Java Tools and Environment i. Installing the JDK ii. Installing OpenJDK on Linux iii. Installing OpenJDK on macOS iv. Installing OpenJDK on Windows v. Configuring IntelliJ IDEA and Creating a Project vi. Running the Project vii. Grabbing the Learning Java Examples b. HelloJava
4 i. Classes ii. The main() Method iii. Classes and Objects iv. Variables and Class Types v. HelloComponent vi. Inheritance vii. The JComponent Class viii. Relationships and Finger-Pointing ix. Package and Imports x. The paintComponent() Method c. HelloJava2: The Sequel i. Instance Variables ii. Constructors iii. Events iv. The repaint() Method v. Interfaces d. Goodbye and Hello Again 4. 3. Tools of the Trade a. JDK Environment b. The Java VM c. Running Java Applications i. System Properties d. The Classpath i. javap
5 ii. Modules e. The Java Compiler f. Trying Java g. JAR Files i. File Compression ii. The jar Utility iii. The pack200 Utility h. Building Up 5. 4. The Java Language a. Text Encoding b. Comments i. Javadoc Comments c. Variables and Constants d. Types i. Primitive Types ii. Reference Types iii. Inferring Types iv. Passing References v. A Word About Strings e. Statements and Expressions i. Statements ii. Expressions f. Arrays i. Array Types
6 ii. Array Creation and Initialization iii. Using Arrays iv. Anonymous Arrays v. Multidimensional Arrays g. Types and Classes and Arrays, Oh My! 6. 5. Objects in Java a. Classes i. Declaring and Instantiating Classes ii. Accessing Fields and Methods iii. Static Members b. Methods i. Local Variables ii. Shadowing iii. Static Methods iv. Initializing Local Variables v. Argument Passing and References vi. Wrappers for Primitive Types vii. Method Overloading c. Object Creation i. Constructors ii. Working with Overloaded Constructors d. Object Destruction i. Garbage Collection
7 e. Packages i. Importing Classes ii. Custom Packages iii. Member Visibility and Access iv. Compiling with Packages f. Advanced Class Design i. Subclassing and Inheritance ii. Interfaces iii. Inner Classes iv. Anonymous Inner Classes g. Organizing Content and Planning for Failure 7. 6. Error Handling and Logging a. Exceptions i. Exceptions and Error Classes ii. Exception Handling iii. Bubbling Up iv. Stack Traces v. Checked and Unchecked Exceptions vi. Throwing Exceptions vii. try Creep viii. The finally Clause ix. try with Resources x. Performance Issues b. Assertions
8 i. Enabling and Disabling Assertions ii. Using Assertions c. The Logging API i. Overview ii. Logging Levels iii. A Simple Example iv. Logging Setup Properties v. The Logger vi. Performance d. Real-World Exceptions 8. 7. Collections and Generics a. Collections i. The Collection Interface ii. Collection Types iii. The Map Interface b. Type Limitations i. Containers: Building a Better Mousetrap ii. Can Containers Be Fixed? c. Enter Generics i. Talking About Types d. “There Is No Spoon” i. Erasure ii. Raw Types
9 e. Parameterized Type Relationships i. Why Isn’t a List<Date> a List<Object>? f. Casts i. Converting Between Collections and Arrays ii. Iterator g. A Closer Look: The sort() Method h. Application: Trees on the Field i. Conclusion 9. 8. Text and Core Utilities a. Strings i. Constructing Strings ii. Strings from Things iii. Comparing Strings iv. Searching v. String Method Summary b. Things from Strings i. Parsing Primitive Numbers ii. Tokenizing Text c. Regular Expressions i. Regex Notation ii. The java.util.regex API d. Math Utilities
10 i. The java.lang.Math Class ii. Big/Precise Numbers e. Dates and Times i. Local Dates and Times ii. Comparing and Manipulating Dates and Times iii. Time Zones iv. Parsing and Formatting Dates and Times v. Parsing Errors vi. Timestamps f. Other Useful Utilities 10. 9. Threads a. Introducing Threads i. The Thread Class and the Runnable Interface ii. Controlling Threads iii. Death of a Thread b. Synchronization i. Serializing Access to Methods ii. Accessing Class and Instance Variables from Multiple Threads c. Scheduling and Priority i. Thread State ii. Time-Slicing
11 iii. Priorities iv. Yielding d. Thread Performance i. The Cost of Synchronization ii. Thread Resource Consumption e. Concurrency Utilities 11. 10. Desktop Applications a. Buttons and Sliders and Text Fields, Oh My! i. Component Hierarchies ii. Model View Controller Architecture iii. Labels and Buttons iv. Text Components v. Other Components b. Containers and Layouts i. Frames and Windows ii. JPanel iii. Layout Managers c. Events i. Mouse Events ii. Action Events iii. Change Events iv. Other Events d. Modals and Pop Ups i. Message Dialogs
12 ii. Confirmation Dialogs iii. Input Dialogs e. Threading Considerations i. SwingUtilities and Component Updates ii. Timers f. Next Steps i. Menus ii. Preferences iii. Custom Components and Java2D iv. JavaFX g. User Interface and User Experience 12. 11. Networking and I/O a. Streams i. Basic I/O ii. Character Streams iii. Stream Wrappers iv. The java.io.File Class v. File Streams vi. RandomAccessFile b. The NIO File API i. FileSystem and Path ii. NIO File Operations c. The NIO Package
13 i. Asynchronous I/O ii. Performance iii. Mapped and Locked Files iv. Channels v. Buffers vi. Character Encoders and Decoders vii. FileChannel d. Network Programming e. Sockets i. Clients and Servers ii. The DateAtHost Client iii. A Distributed Game f. More to Explore 13. 12. Programming for the Web a. Uniform Resource Locators b. The URL Class i. Stream Data ii. Getting the Content as an Object iii. Managing Connections iv. Handlers in Practice v. Useful Handler Frameworks c. Talking to Web Applications i. Using the GET Method ii. Using the POST Method
14 iii. The HttpURLConnection iv. SSL and Secure Web Communications d. Java Web Applications i. The Servlet Life Cycle ii. Servlets iii. The HelloClient Servlet iv. The Servlet Response v. Servlet Parameters vi. The ShowParameters Servlet vii. User Session Management viii. The ShowSession Servlet e. Servlet Containers i. Configuration with web.xml and Annotations ii. URL Pattern Mappings iii. Deploying HelloClient f. The World Wide Web Is, Well, Wide 14. 13. Expanding Java a. Java Releases i. JCP and JSRs b. Lambda Expressions i. Retrofitting Your Code c. Expanding Java Beyond the Core d. Final Wrap-Up and Next Steps
15 15. A. Code Examples and IntelliJ IDEA a. Grabbing the Main Code Examples b. Installing IntelliJ IDEA i. Installing on Linux ii. Installing on a macOS iii. Installing on Windows c. Importing the Examples d. Running the Examples e. Grabbing the Web Code Examples f. Working with Servlets 16. Glossary 17. Index
16 Learning Java FIFTH EDITION An Introduction to Real-World Programming with Java Marc Loy, Patrick Niemeyer, and Daniel Leuck
17 Learning Java by Marc Loy, Patrick Niemeyer, and Daniel Leuck Copyright © 2020 Marc Loy, Patrick Niemeyer, Daniel Leuck. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://oreilly.com). For more information, contact our corporate/institutional sales department: 800-998-9938 or corporate@oreilly.com. Acquisitions Editor: Suzanne McQuade Developmental Editor: Amelia Blevins Production Editor: Beth Kelly Copyeditor: Sonia Saruba Proofreader: Christina Edwards Indexer: Angela Howard Interior Designer: David Futato Cover Designer: Karen Montgomery
18 Illustrator: Rebecca Demarest May 2000: First Edition July 2002: Second Edition May 2005: Third Edition June 2013: Fourth Edition March 2020: Fifth Edition Revision History for the Fifth Edition 2020-03-27: First Release See http://oreilly.com/catalog/errata.csp?isbn=9781492056270 for release details. The O’Reilly logo is a registered trademark of O’Reilly Media, Inc. Learning Java, the cover image, and related trade dress are trademarks of O’Reilly Media, Inc. 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. 978-1-492-05627-0 [LSI]
19 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
20 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
Comments 0
Loading comments...
Reply to Comment
Edit Comment