File: converted PDF by CalibreIf 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 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 Preface 19 Who Should Read This Book 20 New Developments 21 New in This Edition (Java 11, 12, 13, 14) 22 Using This Book 22 Online Resources 24 Conventions Used in This Book 25 Using Code Examples 26 O’Reilly Online Learning 27 How to Contact Us 28 Acknowledgments 29 1. A Modern Language 30 Enter Java 31 Java’s Origins 32 Growing Up 34 A Virtual Machine 36 Java Compared with Other Languages 41 Safety of Design 47 Simplify, Simplify, Simplify... 48 Type Safety and Method Binding 50 Incremental Development 52 Dynamic Memory Management 52 Error Handling 54 Threads 55 Scalability 56 Safety of Implementation 56 The Verifier 59 Class Loaders 61 Security Managers 62 Application and User-Level Security 64 A Java Road Map 65 The Past: Java 1.0–Java 11 65 The Present: Java 14 68 The Future 71 Availability 72 2. A First Application 74 Java Tools and Environment 75 Installing the JDK 76 Installing OpenJDK on Linux 77 Installing OpenJDK on macOS 78 Installing OpenJDK on Windows 79 Configuring IntelliJ IDEA and Creating a Project 88 Running the Project 94 Grabbing the Learning Java Examples 95 HelloJava 98 Classes 103 The main() Method 104 Classes and Objects 107 Variables and Class Types 108 HelloComponent 109 Inheritance 111 The JComponent Class 112 Relationships and Finger-Pointing 114 Package and Imports 116 The paintComponent() Method 119 HelloJava2: The Sequel 121 Instance Variables 123 Constructors 124 Events 128 The repaint() Method 132 Interfaces 133 Goodbye and Hello Again 136 3. Tools of the Trade 137 JDK Environment 137 The Java VM 139 Running Java Applications 139 System Properties 142 The Classpath 143 javap 146 Modules 147 The Java Compiler 147 Trying Java 150 JAR Files 158 File Compression 159 The jar Utility 160 The pack200 Utility 165 Building Up 166 4. The Java Language 168 Text Encoding 169 Comments 172 Javadoc Comments 173 Variables and Constants 176 Types 179 Primitive Types 180 Reference Types 188 Inferring Types 190 Passing References 191 A Word About Strings 192 Statements and Expressions 193 Statements 194 Expressions 207 Arrays 216 Array Types 217 Array Creation and Initialization 218 Using Arrays 221 Anonymous Arrays 224 Multidimensional Arrays 225 Types and Classes and Arrays, Oh My! 228 5. Objects in Java 231 Classes 232 Declaring and Instantiating Classes 234 Accessing Fields and Methods 237 Static Members 244 Methods 248 Local Variables 249 Shadowing 250 Static Methods 252 Initializing Local Variables 256 Argument Passing and References 257 Wrappers for Primitive Types 260 Method Overloading 262 Object Creation 265 Constructors 265 Working with Overloaded Constructors 267 Object Destruction 270 Garbage Collection 271 Packages 272 Importing Classes 273 Custom Packages 276 Member Visibility and Access 278 Compiling with Packages 282 Advanced Class Design 283 Subclassing and Inheritance 283 Interfaces 292 Inner Classes 297 Anonymous Inner Classes 300 Organizing Content and Planning for Failure 302 6. Error Handling and Logging 308 Exceptions 309 Exceptions and Error Classes 310 Exception Handling 314 Bubbling Up 318 Stack Traces 320 Checked and Unchecked Exceptions 322 Throwing Exceptions 324 try Creep 329 The finally Clause 331 try with Resources 332 Performance Issues 335 Assertions 335 Enabling and Disabling Assertions 337 Using Assertions 338 The Logging API 340 Overview 340 Logging Levels 344 A Simple Example 345 Logging Setup Properties 347 The Logger 350 Performance 351 Real-World Exceptions 351 7. Collections and Generics 354 Collections 354 The Collection Interface 356 Collection Types 357 The Map Interface 360 Type Limitations 363 Containers: Building a Better Mousetrap 364 Can Containers Be Fixed? 366 Enter Generics 368 Talking About Types 373 “There Is No Spoon” 373 Erasure 374 Raw Types 377 Parameterized Type Relationships 379 Why Isn’t a List a List ? 382 Casts 384 Converting Between Collections and Arrays 386 Iterator 387 A Closer Look: The sort() Method 389 Application: Trees on the Field 389 Conclusion 394 8. Text and Core Utilities 395 Strings 395 Constructing Strings 396 Strings from Things 398 Comparing Strings 400 Searching 401 String Method Summary 402 Things from Strings 405 Parsing Primitive Numbers 406 Tokenizing Text 406 Regular Expressions 410 Regex Notation 411 The java.util.regex API 420 Math Utilities 427 The java.lang.Math Class 429 Big/Precise Numbers 436 Dates and Times 437 Local Dates and Times 438 Comparing and Manipulating Dates and Times 439 Time Zones 441 Parsing and Formatting Dates and Times 442 Parsing Errors 446 Timestamps 448 Other Useful Utilities 448 9. Threads 451 Introducing Threads 452 The Thread Class and the Runnable Interface 453 Controlling Threads 459 Death of a Thread 468 Synchronization 470 Serializing Access to Methods 471 Accessing Class and Instance Variables from Multiple Threads 479 Scheduling and Priority 480 Thread State 482 Time-Slicing 484 Priorities 486 Yielding 487 Thread Performance 487 The Cost of Synchronization 488 Thread Resource Consumption 489 Concurrency Utilities 490 10. Desktop Applications 494 Buttons and Sliders and Text Fields, Oh My! 495 Component Hierarchies 496 Model View Controller Architecture 498 Labels and Buttons 499 Text Components 507 Other Components 517 Containers and Layouts 523 Frames and Windows 523 JPanel 527 Layout Managers 529 Events 542 Mouse Events 543 Action Events 547 Change Events 551 Other Events 553 Modals and Pop Ups 554 Message Dialogs 555 Confirmation Dialogs 560 Input Dialogs 562 Threading Considerations 563 SwingUtilities and Component Updates 564 Timers 569 Next Steps 573 Menus 573 Preferences 576 Custom Components and Java2D 577 JavaFX 577 User Interface and User Experience 578 11. Networking and I/O 580 Streams 580 Basic I/O 584 Character Streams 587 Stream Wrappers 590 The java.io.File Class 597 File Streams 607 RandomAccessFile 611 The NIO File API 613 FileSystem and Path 614 NIO File Operations 617 The NIO Package 625 Asynchronous I/O 626 Performance 627 Mapped and Locked Files 627 Channels 628 Buffers 629 Character Encoders and Decoders 635 FileChannel 638 Network Programming 643 Sockets 646 Clients and Servers 648 The DateAtHost Client 655 A Distributed Game 658 More to Explore 675 12. Programming for the Web 677 Uniform Resource Locators 678 The URL Class 679 Stream Data 681 Getting the Content as an Object 682 Managing Connections 684 Handlers in Practice 685 Useful Handler Frameworks 687 Talking to Web Applications 687 Using the GET Method 688 Using the POST Method 690 The HttpURLConnection 695 SSL and Secure Web Communications 696 Java Web Applications 696 The Servlet Life Cycle 700 Servlets 701 The HelloClient Servlet 703 The Servlet Response 706 Servlet Parameters 708 The ShowParameters Servlet 710 User Session Management 713 The ShowSession Servlet 714 Servlet Containers 718 Configuration with web.xml and Annotations 720 URL Pattern Mappings 725 Deploying HelloClient 726 The World Wide Web Is, Well, Wide 728 13. Expanding Java 730 Java Releases 730 JCP and JSRs 731 Lambda Expressions 732 Retrofitting Your Code 733 Expanding Java Beyond the Core 742 Final Wrap-Up and Next Steps 743 A. Code Examples and IntelliJ IDEA 746 Grabbing the Main Code Examples 746 Installing IntelliJ IDEA 749 Installing on Linux 749 Installing on a macOS 752 Installing on Windows 754 Importing the Examples 756 Running the Examples 770 Grabbing the Web Code Examples 776 Working with Servlets 776 Glossary 784 Index 814