Table of Contents....5
About the Author....13
About the Technical Reviewer....14
Acknowledgments....15
Chapter 1: An Introduction to Java and Its History....16
How It All Started....16
Who This Book Is For....17
How This Book Is Structured....19
Conventions....20
When Java Was Owned by Sun Microsystems....21
How Is Java Portable?....25
Sun Microsystem’s Java Versions....27
Oracle Takes Over....32
Java 7....32
Java 8....34
Java 9....36
Java 10....37
Java 11....39
Java 12....40
Java 13....41
Java 14....41
Java 15....42
Java 16....43
Java 17....44
Java 18....45
Java 19....45
Java 20....46
Java 21....46
Java 22....48
Java 23....49
Prerequisites....50
Summary....51
Chapter 2: Preparing Your Development Environment....52
Installing Java....53
The JAVA_HOME Environment Variable....58
JAVA_HOME on Windows....58
JAVA_HOME on macOS....63
JAVA_HOME on Linux....65
Running Java Code....66
Using JShell....66
Running Java Source Files Directly Using Java 21....72
Running Java Source Files Directly Using Java 22....75
Installing Apache Maven....82
Installing Git....83
Install a Java IDE....84
Summary....88
Chapter 3: Getting Your Feet Wet....89
Core Syntax Parts....89
Java Fundamental Building Blocks: Packages, Modules, and Classes....91
Packages....92
Access Modifiers....98
Modules....107
Advanced Module Configurations....111
How to Determine the Structure of a Java Project....115
The HelloWorld! Project in IntelliJ IDEA....116
The HelloWorld! Project Compiled and Executed from the Command Line....124
Putting the HelloWorld Class in a Package....126
Configuring the com.sandbox Module....129
Java Projects Using Build Tools, Mostly Maven....131
Explaining and Enriching the Hello World! Class....135
Summary....143
Chapter 4: Java Syntax....144
Base Rules of Writing Java Code....145
Package Declaration....146
Import Section....147
Java Grammar....151
Java Identifiers and Variables....155
Java Comments....156
Java Types....157
Classes....157
Fields....158
Class Variables....160
Encapsulating Data....162
Methods....166
Constructors....169
Abstraction and Inheritance....174
Enums....183
Interfaces....188
Default Methods in Interfaces....195
Static Methods and Constants in Interfaces....199
Private Methods in Interfaces....201
Records....203
Sealed Classes and Interfaces....208
Hidden Classes....211
Annotation Types....212
Lambda Expressions....212
Exceptions....214
Catch the exception, print an appropriate message, and exit the application....218
Throw an appropriate exception type....220
Perform a dummy initialization....223
try/catch Blocks....224
Generics....225
var and the Diamond Operator....228
Unnamed Patterns and Unnamed Variables....230
Summary....231
Java Keywords....232
Chapter 5: Data Types....237
Stack and Heap Memory....237
Introduction to Java Data Types....246
Primitive Data Types....246
Reference Data Types....249
Java Primitive Types....254
The boolean Type....255
The char Type....256
Numeric Primitive Types....258
Java Integer Primitive Types....260
Java Real Primitive Types....261
Java Reference Types....265
Arrays....271
The String Type....282
Useful String Methods Added After Java 8....290
Escaping Characters....292
Text Blocks....294
String Concatenation....299
Wrapper Classes for Primitive Types....301
Date/Time API....304
Collections....311
Concurrency-Specific Types....325
Classic Thread Creation (Before Java 21)....325
Java 21 Threads New Syntax and Virtual Threads....333
Asynchronous Programming Using CompletableFuture....340
Structured Concurrency....343
Summary....349
Chapter 6: Operators....350
The Assignment Operator....351
Explicit Type Conversion: (typem) and instanceof....354
Type Patterns....357
Record Patterns....360
Primitive Patterns....364
Numerical Operators....368
Unary Operators....368
Incrementors and Decrementors....368
Sign Operators....370
Negation Operator....371
Binary Operators....371
The + (Plus/Addition/Concatenation) Operator....371
The - (Minus) Operator....374
The * (Multiply) Operator....375
The / (Divide) Operator....376
The % (Modulus) Operator....377
Relational Operators....380
The == (Equals) Operator....380
The Other Comparison Operators....382
Bitwise Operators....385
Bitwise (~) NOT....386
Bitwise (&) AND....388
Bitwise Inclusive (|) OR....389
Bitwise Exclusive (^) OR....391
Logical Operators....392
Shift Operators....397
The << Shift Left Operator....397
The >> Shift Right Operator....398
The >>> Unsigned Shift Right Operator....401
The Elvis Operator....403
Summary....404
Chapter 7: Controlling the Flow....405
if-else Statement....407
switch....413
The Classic switch Statement....413
The switch Expression....418
switch with String Options....420
switch with enum Options....422
Pattern Matching for switch....427
Record Patterns for switch....432
Looping Statements....433
for Statement....435
while Statement....443
do-while Statement....449
Breaking Loops and Skipping Steps....456
break Statement....457
continue Statement....459
return Statement....461
Controlling the Flow Using try-catch Constructions....463
Summary....468
Chapter 8: The Stream API....469
Introduction to Streams....470
Creating Streams....473
Creating Streams from Collections....474
Creating Streams from Arrays....479
Creating Empty Streams....481
Creating Finite Streams....482
Streams of Primitives and Streams of Strings....489
Short Introduction to Optional....492
How to Use Streams Like a Pro....498
Terminal Functions forEach and forEachOrdered....500
Intermediate Operation filter and Terminal Operation toArray....502
Intermediate Operations map and flatMap and Terminal Operation collect....503
Intermediate Operation sorted and Terminal Operation findFirst....509
Intermediate Operation distinct and Terminal Operation count....510
Intermediate Operation limit and Terminal Operations min and max....511
Terminal Operations sum and reduce....512
Intermediate Operation peek....514
Intermediate Operation skip and Terminal Operations findAny, anyMatch, allMatch, and noneMatch....514
Intermediate Operation gather....518
Creating a Custom Gatherer....520
Built-In Gatherers....527
Debugging Stream Pipelines....530
Summary....535
Chapter 9: Debugging, Testing, and Documenting....537
Debugging....538
Logging....538
Logging with System.out.print....542
Logging with JUL....546
Logging with SLF4J and Logback....560
Debug Using Assertions....569
Step-By-Step Debugging....574
Inspect a Running Application Using Java Tools....578
jps....579
jcmd....580
jconsole....583
Using JDK Mission Control....587
Accessing the Java Process API....588
Testing....596
Test Code Location....598
Building an Application to Test....599
Introducing JUnit....600
Using Fakes....605
Using Stubs....612
Using Mocks....617
Documenting....624
Summary....638
Chapter 10: Making Your Application Interactive....640
Reading User Data from the Command Line....640
Using System.in....641
Using java.util.Scanner....642
Using java.io.Console....649
Building Applications Using Swing....652
Introducing JavaFX....665
Internationalization....677
Building a Web Application....688
Simple Application Server....692
The jwebserver Command-Line Tool....692
Using SimpleFileServer....695
Java Web Application with an Embedded Server....703
Java Web Application on a Standalone Server....711
Summary....721
Chapter 11: Working with Files....722
Java IO and NIO APIs....722
File Handlers....724
isFile....725
getAbsolutePath....726
getParent, getName, length, isHidden, canRead, and canWrite....727
Handling Directories: list() and listFiles()....728
createNewFile(), exists(), createTempFile(..), and deleteOnExit()....730
renameTo....732
Path Handlers....733
Reading Files....740
Using Scanner to Read Files....740
Using Files Utility Methods to Read Files....741
Using Readers to Read Files....743
Using InputStream to Read Files....748
Writing Files....751
Writing Files Using Files Utility Methods....751
Using Writer to Write Files....754
Using OutputStream to Write Files....759
Using Java NIO to Manage Files....762
Serialization and Deserialization....768
Byte Serialization....769
XML Serialization....772
JSON Serialization....778
The Media API....783
Using JavaFX Image Classes....797
Writing and Reading from Databases....800
Summary....806
Chapter 12: The Publish/Subscribe Framework....807
Reactive Programming and the Reactive Manifesto....808
Using the JDK Reactive Streams API....814
Reactive Streams Technology Compatibility Kit....828
Using Project Reactor....832
Summary....838
Chapter 13: Garbage Collection....840
Garbage Collection Basics....841
Oracle HotSpot JVM Architecture....841
How Many Garbage Collectors Are There?....846
Working with Garbage Collection from the Code....858
Using the finalize() Method....858
Heap Memory Statistics....865
Using Cleaner....871
Preventing GC from Deleting an Object....874
Using Weak References....878
Garbage Collection Exceptions and Causes....882
Summary....884
Appendix A: Java Modules....885
Modules....886
Advanced Module Configurations....890
Appendix B: IntelliJ IDEA Modules....907
Index....909
Write your first code in Java 23 using simple, step-by-step examples that model real-word objects and events, making learning easy. With this book you will be able to pick up core programming concepts without fuss and write efficient Java code in no time. Clear code descriptions and layout ensure you get your code running as soon as possible. Author Iuliana Cosmina focuses on practical knowledge and getting you up to speed quickly—all the bits and pieces a novice needs to get started programming in Java.
In this edition, you will discover how Java has changed since version 17, and how to design and write code using the most recently introduced Java features such as new collection methods, virtual threads, pattern and record matching in switch expressions, structured concurrency tasks, unnamed classes and instance methods, and many more.
This book is a complete Java guide, covering the following topics: setting up a development environment, programming concepts and well-known programming principles, writing Java code following industry-specific design patterns and coding conventions, executing it, debugging, testing, documenting it and even using specialized tools such as IntelliJ IDEA for writing Java code, Maven for building, JUnit Jupiter for testing, and in-memory and Docker-hosted databases or data storage. After reading this book, you’ll have all the necessary skills and knowledge to pass an interview for a starting Java development position.
Those who are new to programming and want to learn Java and use it to build efficient solutions