Java 23 for Absolute Beginners: Learn the Fundamentals of Java Programming, Third Edition by Iuliana Cosmina

By

Java 23 for Absolute Beginners: Learn the Fundamentals of Java Programming, Third Edition

Iuliana Cosmina

Java 23 for Absolute Beginners

Table of Contents

About the Author ………. xiii

About the Technical Reviewer ………………………xv

Acknowledgments ……..xvii

Chapter 1: An Introduction to Java and Its History ………………………………………….. 1

How It All Started …………………………… 1

Who This Book Is For ………………………. 2

How This Book Is Structured ……………. 4

Conventions ………………………………….. 5

When Java Was Owned by Sun Microsystems …………………… 6

How Is Java Portable? …………………… 10

Sun Microsystem’s Java Versions …… 12

Oracle Takes Over ………………………… 17

Java 7 ……………………………………. 17

Java 8 ……………………………………. 19

Java 9 ……………………………………. 21

Java 10 ………………………………….. 22

Java 11 ………………………………….. 24

Java 12 ………………………………….. 25

Java 13 ………………………………….. 26

Java 14 ………………………………….. 26

Java 15 ………………………………….. 27

Java 16 ………………………………….. 28

Java 17 ………………………………….. 29

Java 18 ………………………………….. 30

Java 19 ………………………………….. 30

Java 20 ………………………………….. 31

Java 21 ………………………………….. 31

Java 22 ………………………………….. 33

Java 23 ………………………………….. 34

Prerequisites ……………………………….. 35

Summary ……………………………………. 36

Chapter 2: Preparing Your Development Environment ……………………………………. 37

Installing Java ……………………………… 38

The JAVA_HOME Environment Variable …………………………… 43

JAVA_HOME on Windows ………….. 43

JAVA_HOME on macOS …………….. 48

JAVA_HOME on Linux ……………….. 50

Running Java Code ………………………. 51

Using JShell ……………………………. 51

Running Java Source Files Directly Using Java 21 ………. 57

Running Java Source Files Directly Using Java 22 ………. 60

Installing Apache Maven ……………….. 67

Installing Git ………………………………… 68

Install a Java IDE ………………………….. 69

Summary ……………………………………. 73

Chapter 3: Getting Your Feet Wet …………………. 75

Core Syntax Parts …………………………. 75

Java Fundamental Building Blocks: Packages, Modules, and Classes ………………………………… 77

Packages ……………………………….. 78

Access Modifiers …………………….. 84

Modules …………………………………. 93

How to Determine the Structure of a Java Project ………….. 101

The HelloWorld! Project in IntelliJ IDEA ……………………. 102

The HelloWorld! Project Compiled and Executed from the Command Line ……………………. 110

Putting the HelloWorld Class in a Package ……………….. 112

Configuring the com.sandbox Module ……………………… 115

Java Projects Using Build Tools, Mostly Maven ……………… 117

Explaining and Enriching the Hello World! Class …………….. 121

Summary ………………………………….. 129

Chapter 4: Java Syntax …………………………….. 131

Base Rules of Writing Java Code ….. 132

Package Declaration ………………. 133

Import Section ………………………. 134

Java Grammar ………………………. 138

Java Identifiers and Variables ….. 142

Java Comments …………………….. 143

Java Types …………………………………. 144

Classes ………………………………… 144

Lambda Expressions …………………… 199

Exceptions …………………………………. 201

Catch the exception, print an appropriate message, and exit the application ………………… 205

Throw an appropriate exception type ………………………. 207

Perform a dummy initialization ………………………………. 210

try/catch Blocks ……………………. 211

Generics ……………………………………. 212

var and the Diamond Operator …….. 215

Unnamed Patterns and Unnamed Variables …………………… 217

Summary ………………………………….. 218

Java Keywords ……………………… 219

Chapter 5: Data Types ………………………………. 225

Stack and Heap Memory ……………… 225

Introduction to Java Data Types ……. 234

Primitive Data Types ………………. 234

Reference Data Types …………….. 237

Java Primitive Types ………………. 242

Java Reference Types …………….. 253

Date/Time API ……………………….. 292

Collections ……………………………. 299

Concurrency-Specific Types ……. 313

Summary ………………………………….. 337

Chapter 6: Operators … 339

The Assignment Operator …………….. 340

Explicit Type Conversion: (typem) and instanceof …….. 343

Type Patterns ………………………… 346

Record Patterns …………………….. 349

Primitive Patterns ………………….. 353

Numerical Operators …………………… 357

Unary Operators ……………………. 357

Binary Operators ……………………. 360

Relational Operators ………………. 369

Bitwise Operators ………………………. 374

Bitwise (~) NOT …………………….. 375

Bitwise (&) AND …………………….. 377

Bitwise Inclusive (|) OR …………… 378

Bitwise Exclusive (^) OR ………… 380

Logical Operators ………………….. 381

Shift Operators ……………………… 386

The Elvis Operator ………………………. 392

Summary ………………………………….. 393

Chapter 7: Controlling the Flow …………………. 395

if-else Statement ……………………. 397

switch ………………………………………. 403

The Classic switch Statement …. 403

The switch Expression ………….. 408

switch with String Options …. 410

switch with enum Options …….. 412

Pattern Matching for switch ……. 417

Record Patterns for switch ……. 422

Looping Statements ……………………. 423

for Statement ……………………….. 425

while Statement ………………….. 433

do-while Statement …………….. 439

Breaking Loops and Skipping Steps …………………………….. 446

break Statement ………………….. 447

continue Statement …………….. 449

return Statement ………………… 451

Controlling the Flow Using try-catch Constructions ……. 453

Summary ………………………………….. 458

Chapter 8: The Stream API ……………………….. 459

Introduction to Streams ………………. 460

Creating Streams ……………………….. 463

Creating Streams from Collections ………………………….. 464

Creating Streams from Arrays …. 469

Creating Empty Streams …………. 471

Creating Finite Streams ………….. 472

Streams of Primitives and Streams of Strings ………….. 479

Short Introduction to Optional<T> ………………………….. 482

How to Use Streams Like a Pro ………………………………. 488

Debugging Stream Pipelines ………… 520

Summary ………………………………….. 525

Chapter 9: Debugging, Testing, and Documenting ………………………………………… 527

Debugging …………………………………. 528

Logging ………………………………… 528

Debug Using Assertions ………….. 559

Step-By-Step Debugging ………… 564

Inspect a Running Application Using Java Tools ………… 568

Accessing the Java Process API ……………………………… 578

Testing ……………………………………… 586

Test Code Location ………………… 588

Building an Application to Test … 589

Introducing JUnit …………………… 590

Documenting ……………………………… 614

Summary ………………………………….. 628

Chapter 10: Making Your Application Interactive …………………………………………. 631

Reading User Data from the Command Line ………………….. 631

Using System.in ……………………. 632

Using java.util.Scanner ………….. 633

Using java.io.Console …………….. 640

Building Applications Using Swing … 643

Introducing JavaFX …………………….. 656

Internationalization …………………….. 668

Building a Web Application ………….. 679

Simple Application Server ………. 683

Java Web Application with an Embedded Server ………. 694

Java Web Application on a Standalone Server ………….. 702

Summary ………………………………….. 712

Chapter 11: Working with Files …………………. 713

Java IO and NIO APIs …………………… 713

File Handlers ……………………………… 715

isFile ………………………………… 716

getAbsolutePath ……………….. 717

getParent, getName, length, isHidden, canRead, and canWrite ………………………… 718

Handling Directories: list() and listFiles() ……… 719

createNewFile(), exists(), createTempFile(..), and deleteOnExit() …………. 721

renameTo …………………………….. 723

Path Handlers …………………………….. 724

Reading Files …………………………….. 731

Using Scanner to Read Files ….. 731

Using Files Utility Methods to Read Files …………………. 732

Using Readers to Read Files ….. 734

Using InputStream to Read Files ………………………….. 739

Writing Files ………………………………. 742

Writing Files Using Files Utility Methods ………………….. 742

Using Writer to Write Files ……. 745

Using OutputStream to Write Files ………………………… 750

Using Java NIO to Manage Files ……. 753

Serialization and Deserialization …… 759

Byte Serialization …………………… 760

XML Serialization …………………… 763

JSON Serialization …………………. 769

The Media API ……………………………. 774

Using JavaFX Image Classes ……….. 788

Writing and Reading from Databases …………………………… 791

Summary ………………………………….. 797

Chapter 12: The Publish/Subscribe Framework …………………………………………… 799

Reactive Programming and the Reactive Manifesto ……….. 800

Using the JDK Reactive Streams API ……………………………. 806

Reactive Streams Technology Compatibility Kit ……………… 820

Using Project Reactor …………………. 824

Summary ………………………………….. 830

Chapter 13: Garbage Collection …………………. 833

Garbage Collection Basics …………… 834

Oracle HotSpot JVM Architecture ……………………………. 834

How Many Garbage Collectors Are There? ……………….. 839

Working with Garbage Collection from the Code ……………. 851

Using the finalize() Method ………………………………. 851

Heap Memory Statistics …………. 858

Using Cleaner ……………………… 864

Preventing GC from Deleting an Object …………………………. 867

Using Weak References ………………. 871

Garbage Collection Exceptions and Causes …………………… 875

Summary ………………………………….. 877

Appendix A: Java Modules ……………………….. 879

Appendix B: IntelliJ IDEA Modules …………….. 901

Index ………………………. 903

This book is US$10
To get free sample pages OR Buy this book


Share this Book!

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.