Java Tutorials - Herong's Tutorial Examples

https://www.herongyang.com/Java

Copyright © 1997-2024 Herong Yang. All rights reserved.

Java Tutorials This tutorial book is a collection of notes and sample codes written by the author while he was learning Java himself. Topics covered in the book include: Java language basics; execution environment; generic classes and parameterized type; generic methods and type argument inferences; enum types and constants; lambda expressions; annotation types and invocations; Java modules; execution threads and synchronization; memory management and garbage collection. Updated in 2024 (Version v8.22) with JDK 20.

Table of Contents

About This Book

JDK - Java Development Kit

Downloading and Installing JDK on Mac

Downloading and Installing JDK on Windows

Adding JDK "bin" Directory to Path Setting

Downloading and Installing JDK on Linux

Writing My First Java Program

JDK Documentation Installation

Execution Process, Entry Point, Input and Output

Creating, Compiling and Executing Java Programs

main() Method - Java Execution Entry Point

Java Execution Console - "in", "out" and "err" Data Streams

Primitive Data Types and Literals

Data Types Supported in Java

Integer Data Types

Floating-Point Data Types

Logical (Boolean) Data Type

Literals of Primitive Types

Literal Formats for Integers

Literal Formats for Integers - Example

Literal Formats for Floating-Point

Literal Formats for Floating-Point - Example

Literal Formats for Characters

Literal Formats for Character - Example

Control Flow Statements

What Is Control Flow Statement

"if-then" Statements

"if-then-else" Statements

Nested "if-then-else" Statements

"switch" Statements

Fall-Through Behavior of "switch" Statements

Basic "for" Statements

Enhanced "for" Statements

"while" Statements

"do" Statements

"break" Statements

Labeled "break" Statements

"continue" Statements

Labeled "continue" Statements

Bits, Bytes, Bitwise and Shift Operations

What Are Bits and Bytes

"byte" Data Type and Implicit Casting

Operations on "byte" Data Type Values

Bitwise Operations on "byte" Values

Bitwise Operations on "byte" Values - Example Program

Shift Operations - Left, Right or Unsigned Right

Managing Bit Strings in Byte Arrays

setBit() - Storing a Bit into a Byte Array

getBit() - Retrieving a Bit from a Byte Array

rotateLeft() - Left Rotating All Bits in a Byte Array

bitStringTest.java - Testing Program

Reference Data Types and Variables

Reference Types Supported in Java

Creating Class Type Objects

Class Type Variables Storing References

Interface Type Variables

Class and Interface Hierarchy

Supertype and Subtype

Explicit and Implicit Type Casting

Type Casting Example Program

Type Casting Compile and Runtime Error

Enum Types and Enum Constants

What Is an Enum Type

Use 'class' to Define Enumeration

Instance Variables for Enum Constants

java.lang.Enum Super Type

Enum Constant Inherited Methods

Enum Constant Implicit Methods

StringBuffer - The String Buffer Class

StringBuffer Class Properties and Methods

SimpleStringBuffer - My Implementation of String Buffer

Performance Comparison of String and StringBuffer

System Properties and Runtime Object Methods

JVM and OS System Properties

System.setProperty() - Setting Your Own Properties

Runtime.getRuntime() - Getting the Runtime Object

freeMemory() - Getting JVM Free Memory Information

Calculating Memory Usage of an Array

exec() - Executing Operating System Commands

Generic Classes and Parameterized Types

Generic Methods and Type Inference

Lambda Expressions and Method References

Java Modules - Java Package Aggregation

Execution Threads and Multi-Threading Java Programs

What Are Processes and Threads

The "Thread" Class - Creating Thread Objects With Thread Sub Classes

The "Runnable" Interface - Creating Thread Objects with Runnable Objects

CPU Execution Time Shared by Multiple Threads

CPU Execution Time Shared by Multiple Threads - Test Output

Application Data Shared by Multiple Threads

Application Data Shared by Multiple Threads - Test Results

interrupt() - Method to Terminate Thread

ThreadGroup Class and "system" ThreadGroup Tree

"ThreadGroup" Class - Container of Threads and Thread Groups

Displaying the "system" ThreadGroup Tree

Adding Threads and Thread Groups

Synchronization Technique and Synchronized Code Blocks

Deadlock Condition Example Programs

What Is Deadlock

Deadlock Example - 5 Dining Philosophers

Deadlock Example - Transferring Funds

Garbage Collection and the gc() Method

Garbage Collection and Unused Objects

The Automated Garbage Collection Process

gc() - The Garbage Collection Method

Example Program of Using the gc() Method

Assert Statements and -ea" Option

"assert" Statements

JVM "-ea" and "-da" Options

Annotation Statements and Declarations

What Is Annotation

Use "interface" as Annotation

Default Values for Annotation Elements

Single-Element Annotation Invocation

No-Element (Marker) Annotation Invocation

getAnnotations() Method - Annotation APIs

Predefined Annotation Types

Java Related Terminologies

Archived Tutorials

Archived: Downloading and Installing JDK 13 on Mac

Archived: Downloading and Installing JDK 10

Archived: JDK 10 Documentation Installation

Archived: Downloading and Installing JDK 8

Archived: Downloading and Installing JDK 1.7

Archived: Downloading and Installing JDK 1.6

Archived: Downloading and Installing JDK 1.5

Archived: JDK 1.3 Bug - Memory Leak With Unstarted Threads

References

Full Version in PDF/EPUB

Keywords: Java, Language, Tutorial