About This eBook....3
Preface....15
Acknowledgments....18
1. Fundamental Programming Structures....19
1.1. Our First Program....20
1.2. Primitive Types....41
1.3. Variables....48
1.4. Arithmetic Operations....55
1.5. Strings....70
1.6. Input and Output....94
1.7. Control Flow....106
1.8. Arrays and Array Lists....124
1.9. Functional Decomposition....146
Exercises....151
2. Object-Oriented Programming....155
2.1. Working with Objects....156
2.2. Implementing Classes....165
2.3. Object Construction....176
2.4. Records....186
2.5. Static Variables and Methods....193
2.6. Packages....202
2.7. Nested Classes....217
2.8. Documentation Comments....228
Exercises....240
3. Interfaces and Lambda Expressions....245
3.1. Interfaces....246
3.2. Static, Default, and Private Methods....264
3.3. Examples of Interfaces....274
3.4. Lambda Expressions....284
3.5. Method and Constructor References....291
3.6. Processing Lambda Expressions....297
3.7. Lambda Expressions and Variable Scope....307
3.8. Higher-Order Functions....315
3.9. Local and Anonymous Classes....319
Exercises....323
4. Inheritance and Reflection....327
4.1. Extending a Class....329
4.2. Inheritance Hierarchies....342
4.3. Object: The Cosmic Superclass....356
4.4. Enumerations....378
4.5. Pattern Matching....387
4.6. Runtime Type Information and Resources....399
4.7. Reflection....422
Exercises....440
5. Exceptions, Assertions, and Logging....444
5.1. Exception Handling....445
5.2. Assertions....469
5.3. Logging....473
Exercises....495
6. Generic Programming....500
6.1. Generic Classes....501
6.2. Generic Methods....503
6.3. Type Bounds....505
6.4. Type Variance and Wildcards....507
6.5. Generics in the Java Virtual Machine....519
6.6. Restrictions on Generics....525
6.7. Reflection and Generics....540
Exercises....546
7. Collections....553
7.1. An Overview of the Collections Framework....553
7.2. Iterators....571
7.3. Sets....574
7.4. Maps....579
7.5. Other Collections....593
7.6. Views....606
Exercises....612
8. Streams....616
8.1. From Iterating to Stream Operations....617
8.2. Stream Creation....620
8.3. The filter, map, and flatMap Methods....627
8.4. Extracting Substreams and Combining Streams....631
8.5. Other Stream Transformations....633
8.6. Simple Reductions....635
8.7. The Optional Type....637
8.8. Collecting Results....648
8.9. Collecting into Maps....651
8.10. Grouping and Partitioning....654
8.11. Downstream Collectors....656
8.12. Reduction Operations....661
8.13. Primitive Type Streams....665
8.14. Parallel Streams....668
Exercises....675
9. Processing Input and Output....679
9.1. Input/Output Streams, Readers, and Writers....680
9.2. Paths, Files, and Directories....703
9.3. HTTP Connections....720
9.4. Regular Expressions....728
9.5. Serialization....751
Exercises....773
10. Concurrent Programming....777
10.1. Concurrent Tasks....779
10.2. Thread Safety....792
10.3. Threadsafe Data Structures....804
10.4. Parallel Algorithms....815
10.5. Asynchronous Computations....818
10.6. Atomic Counters and Accumulators....833
10.7. Locks and Conditions....839
10.8. Threads....850
10.9. Processes....857
Exercises....866
11. Annotations....877
11.1. Using Annotations....879
11.2. Defining Annotations....890
11.3. Annotations in the Java API....895
11.4. Processing Annotations at Runtime....907
11.5. Source-Level Annotation Processing....913
Exercises....922
12. The Java Platform Module System....924
12.1. The Module Concept....925
12.2. Naming Modules....928
12.3. The Modular “Hello, World!” Program....930
12.4. Requiring Modules....933
12.5. Exporting Packages....937
12.6. Modules and Reflective Access....943
12.7. Modular JARs....949
12.8. Automatic Modules....952
12.9. The Unnamed Module....955
12.10. Command-Line Flags for Migration....956
12.11. Transitive and Static Requirements....959
12.12. Qualified Exporting and Opening....963
12.13. Service Loading....964
12.14. Tools for Working with Modules....967
Exercises....971
Index....974
Modern Java introduces major enhancements that impact the core Java technologies and APIs at the heart of the Java platform. Many old Java idioms are no longer needed, and new features and programming paradigms can make you far more effective. However, navigating these changes can be challenging.
Core Java for the Impatient, Fourth Edition, is a complete yet concise guide that reflects all changes through Java SE 21, Oracle's latest Long-Term Support (LTS) release. Written by Cay S. Horstmann--author of the classic two-volume Core Java--this indispensable tutorial offers a faster, easier pathway for learning modern Java.
Horstmann covers everything working developers need to know, including the powerful concepts of lambda expressions and streams, modern constructs such as records and sealed classes, and sophisticated concurrent programming techniques. Given the size and scope of Java 21, there's plenty to cover, but it's presented in small chunks organized for quick access and easy understanding, with plenty of practical insights and sample code to help you quickly apply all that's new.
This book is fully updated to Java 21. It uses modern features and does not dwell on historical or obsolete constructs. Preview features that may make it to the language in the future are not covered either. A key reason to use Java is to tackle concurrent programming. With parallel algorithms and threadsafe data structures readily available in the Java library, the way application programmers should handle concurrent programming has completely changed. I provide fresh coverage, showing you how to use the powerful library features instead of error-prone low-level constructs.
Traditionally, books on Java have focused on user interface programming, but nowadays, few developers produce user interfaces on desktop computers. You will be able to use this book effectively without being distracted by lengthy GUI code. Finally, this book is written for application programmers, not for a college course and not for systems wizards. The book covers issues that application programmers need to wrestle with, such as logging and working with files, but you won’t learn how to implement a linked list by hand or how to write a web server.