Cover....1
Title Page....2
Copyright and Credits....2
Contributors....4
Table of Contents....8
Preface....18
Part 1: Overview of Java Programming....26
Chapter 1: Getting Started with Java 17....28
Technical requirements....29
How to install and run Java....29
What is the JDK and why do we need it?....30
Installing Java SE....32
Commands, tools, and utilities....32
How to install and run an IDE....33
Selecting an IDE....34
Installing and configuring IntelliJ IDEA....35
Creating a project....36
Importing a project....42
Executing examples from the command line....47
Java primitive types and operators....48
Boolean types....48
Numeric types....49
Default values of primitive types....52
Literals of primitive types....53
New compact number format....56
Operators....57
String types and literals....62
String literals....63
String immutability....66
IDs and variables....66
ID....67
Variable declaration (definition) and initialization....67
Java statements....68
Expression statements....70
Control flow statements....71
Summary....84
Quiz....84
Chapter 2: Java Object-Oriented Programming (OOP)....90
Technical requirements....91
OOP concepts....92
Object/class....92
Inheritance....93
Abstraction/interface....94
Encapsulation....95
Polymorphism....96
Class....96
Method....98
Constructor....100
The new operator....103
Class java.lang.Object....105
Instance and static properties and methods....110
Interface....112
Default methods....113
Private methods....115
Static fields and methods....116
Interface versus abstract class....116
Overloading, overriding, and hiding....117
Overloading....117
Overriding....119
Hiding....121
The final variable, method, and classes....125
The final variable....125
Final method....127
Final class....128
The record class....128
Sealed classes and interfaces....130
Polymorphism in action....134
The object factory....134
The instanceof operator....137
Summary....139
Quiz....139
Chapter 3: Java Fundamentals....144
Technical requirements....145
Packages, importing, and access....145
Packages....145
Importing ....146
Access modifiers....148
Java reference types....152
Class and interface....152
Array....153
Enum....155
Default values and literals ....158
A reference type as a method parameter....158
equals() method ....161
Reserved and restricted keywords....164
Reserved keywords....164
Reserved identifiers....165
Reserved words for literal values....165
Restricted keywords....165
Usage of the this and super keywords....166
Usage of the this keyword....166
Usage of the super keyword ....168
Converting between primitive types....169
Widening conversion....169
Narrowing conversion....170
Methods of conversion....171
Converting between primitive and reference types....174
Boxing....174
Unboxing....175
Summary....176
Quiz....177
Part 2: Building Blocks of Java....180
Chapter 4: Exception Handling....182
Technical requirements....183
The Java exceptions framework....183
Checked and unchecked exceptions....185
The try, catch, and finally blocks....187
The throws statement....190
The throw statement....192
The assert statement....193
Best practices of exception handling....193
Summary....194
Quiz....195
Chapter 5: Strings, Input/Output, and Files....198
Technical requirements....199
String processing....199
Methods of the String class ....199
String utilities....206
I/O streams....209
Stream data....210
The InputStream class and its subclasses....211
The OutputStream class and its subclasses....222
The Reader and Writer classes and their subclasses....226
Other classes of the java.io package....228
The java.util.Scanner class....238
File management....241
Creating and deleting files and directories....241
Listing files and directories....244
Apache Commons’ FileUtils and IOUtils utilities ....245
The FileUtils class....246
The IOUtils class....247
Summary....247
Quiz....248
Chapter 6: Data Structures, Generics, and Popular Utilities....252
Technical requirements....253
List, Set, and Map interfaces....253
Generics....254
How to initialize List and Set....255
java.lang.Iterable interface....259
Collection interface....260
List interface....263
Set interface ....266
Map interface....267
Unmodifiable collections....270
Collections utilities....271
java.util.Collections class....272
CollectionUtils class....274
Arrays utilities....276
java.util.Arrays class....276
ArrayUtils class....278
Objects utilities....279
java.util.Objects class....279
ObjectUtils class....284
The java.time package....285
LocalDate class....285
LocalTime class....289
LocalDateTime class....291
Period and Duration classes....292
Period of day....294
Summary....295
Quiz....295
Chapter 7: Java Standard and External Libraries....302
Technical requirements....303
Java Class Library (JCL)....303
java.lang....304
java.util....305
java.time....306
java.io and java.nio....307
java.sql and javax.sql....307
java.net....307
java.lang.math and java.math....308
java.awt, javax.swing, and javafx....308
External libraries....309
org.junit....309
org.mockito....311
org.apache.log4j and org.slf4j....312
org.apache.commons....315
Summary....320
Quiz....320
Chapter 8: Multithreading and Concurrent Processing....324
Technical requirements....325
Thread versus process....325
User thread versus daemon....326
Extending the Thread class....326
Implementing the Runnable interface....328
Extending Thread versus implementing Runnable....330
Using a pool of threads....331
Getting results from a thread....339
Parallel versus concurrent processing....346
Concurrent modification of the same resource....346
Atomic variable....350
Synchronized method....353
Synchronized block....354
Concurrent collections ....355
Addressing memory consistency errors....358
Summary....358
Quiz....359
Chapter 9: JVM Structure and Garbage Collection....362
Technical requirements....363
Java application execution....363
Using an IDE....363
Using the command line with classes....369
Using the command line with JAR files....370
Using the command line with an executable JAR file....371
Java processes ....372
Classloading....375
Class linking....376
Class initialization....377
Class instantiation....377
Method execution....378
Garbage collection....378
Application termination....378
JVM’s structure....380
Runtime data areas....380
Classloaders....381
Execution engine....381
Garbage collection....382
Responsiveness, throughput, and stop-the-world....382
Object age and generation....383
When stop-the-world is unavoidable....383
Summary....384
Quiz....384
Chapter 10: Managing Data in a Database....388
Technical requirements....389
Creating a database....389
Creating a database structure....391
Connecting to a database....394
Releasing the connection....397
CRUD data....398
The INSERT statement....398
The SELECT statement....399
The UPDATE statement....400
The DELETE statement....401
Using statements....401
Using PreparedStatement....408
Using CallableStatement....410
Using a shared library JAR file to access a database....412
Summary....417
Quiz....417
Chapter 11: Network Programming....422
Technical requirements....423
Network protocols....423
UDP-based communication....424
TCP-based communication....429
The java.net.ServerSocket class....430
The java.net.Socket class....433
Running the examples....435
UDP versus TCP protocols....437
URL-based communication....438
The URL syntax....438
The java.net.URL class....439
Using the HTTP 2 Client API....449
Blocking HTTP requests....451
Non-blocking (asynchronous) HTTP requests....453
Server push functionality....459
WebSocket support....461
Summary....463
Quiz....464
Chapter 12: Java GUI Programming....466
Technical requirements....467
Java GUI technologies....467
JavaFX fundamentals....468
HelloWorld with JavaFX....472
Control elements....475
Charts....479
Applying CSS....481
Using FXML....483
Embedding HTML....493
Playing media....503
Adding effects....509
Summary....535
Quiz....535
Part 3: Advanced Java....536
Chapter 13: Functional Programming....538
Technical requirements....539
What is functional programming?....539
What is a functional interface?....542
What is a Lambda expression?....544
Standard functional interfaces....548
Consumer....548
Predicate....550
Supplier....552
Function....553
Other standard functional interfaces....556
Lambda expression limitations....557
Method references....559
Summary....562
Quiz....563
Chapter 14: Java Standard Streams....564
Technical requirements....565
Streams as a source of data and operations....565
Stream initialization....567
Stream interface....567
The Stream.Builder interface....572
Other classes and interfaces....574
Operations (methods)....576
Intermediate operations....578
Terminal operations....582
Numeric stream interfaces....606
Creating a stream....607
Intermediate operations....607
Terminal operations....611
Parallel streams....612
Stateless and stateful operations....612
Sequential or parallel processing?....613
Summary....616
Quiz....617
Chapter 15: Reactive Programming....620
Technical requirements....621
Asynchronous processing....621
Sequential and parallel streams....622
Using the CompletableFuture object....624
Non-blocking APIs....625
The java.io package versus the java.nio package....625
The event/run loop....626
Reactive....627
Responsive....628
Resilient....628
Elastic....629
Message-driven....629
Reactive streams....630
RxJava....631
Observable types....634
Disposable....643
Creating an observable....645
Operators....647
Multithreading (scheduler)....659
Summary....666
Quiz....666
Chapter 16: Java Microbenchmark Harness....668
Technical requirements....669
What is JMH?....669
Creating a JMH benchmark....672
Running the benchmark....674
Using an IDE plugin....674
JMH benchmark parameters....677
Mode....677
Output time unit....678
Iterations....678
Forking....678
JMH usage examples....679
Using the @State annotation....681
Using the Blackhole object....682
Using the @CompilerControl annotation....683
Using the @Param annotation....683
A word of caution....684
Summary....685
Quiz....685
Chapter 17: Best Practices for Writing High-Quality Code....688
Technical requirements....689
Java idioms, their implementation, and their usage....689
The equals() and hashCode() methods....690
The compareTo() method....693
The clone() method....695
The StringBuffer and StringBuilder classes....701
The try, catch, and finally clauses....701
Best design practices....701
Identifying loosely coupled functional areas....702
Breaking the functional area into traditional tiers....702
Coding to an interface....702
Using factories....703
Preferring composition over inheritance....703
Using libraries....703
Code is written for people....704
Use well-established frameworks and libraries....705
Testing is the shortest path to quality code....712
Summary....712
Quiz....713
Assessments....714
Chapter 1 – Getting Started with Java 17....714
Chapter 2 – Java Object-Oriented Programming (OOP)....715
Chapter 3 – Java Fundamentals....715
Chapter 4 – Exception Handling....716
Chapter 5 – Strings, Input/Output, and Files....716
Chapter 6 – Data Structures, Generics, and Popular Utilities....717
Chapter 7 – Java Standard and External Libraries....717
Chapter 8 – Multithreading and Concurrent Processing....718
Chapter 9 – JVM Structure and Garbage Collection....719
Chapter 10 – Managing Data in a Database....719
Chapter 11 – Network Programming....720
Chapter 12 – Java GUI Programming....721
Chapter 13 – Functional Programming....721
Chapter 14 – Java Standard Streams....722
Chapter 15 – Reactive Programming....722
Chapter 16 – Java Microbenchmark Harness....723
Chapter 17 – Best Practices for Writing High-Quality Code....724
Index....726
Other Books You May Enjoy....745
Learn the fundamentals of Java Programming with this updated guide with the latest features
Java is one of the most preferred languages among developers. It is used in everything right from smartphones and game consoles to even supercomputers, and its new features simply add to the richness of the language.