Java Tutorials - Herong's Tutorial Examples

http://www.herongyang.com/Java

Copyright © 2015 by Dr. Herong Yang. All rights reserved.

Java Tutorials This free Java book is a collection of notes and sample codes written by the author while he was learning Java himself. It covers topics related Java language core features and classes provided in the java.lang.* package: array, bit, byte, deadlock, expression, generic class, generic method, literal, main method, parameterized type, primary type, synchronization, string, thread.

Table of Contents

About This Book

Installing JDK 1.8 on Windows

Downloading and Installing JDK 8

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

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 Shift

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

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

What Is a Generic Class?

Using a Generic Class

Using a Generic Class - Example

Creating a Generic Class

Creating a Generic Class - Example

Bounded Type Parameters

Raw Type, Generic Type and Parameterized Type

Parameterized Type and Subtyping

Wildcard Parameterized Types

Wildcard Parameterized Type Test

Wildcard Parameterized Subtyping

Wildcard Parameterized Subtyping Example

Generic Methods and Type Inference

What Is a Generic Method?

Comparing Generic Method with Non-Generic Method

Non-Generic Method Example - maxNonGeneric()

Generic Method Example - maxGeneric()

Generic Methods in java.util.Collections Class

Testing Generic Methods in Collections Class

What Is Type Argument Inference?

Type Argument Inference by Parameter List

Type Argument Inference by Return Value

Generic Methods using Parameterized Types

Parameterized Type as Generic Method Return Type

Lambda Expressions and Method References

What Is Lambda Expression?

LambdaCalculator.java - Lambda Expression Example

Lambda Expression Syntax Options

Lambda Expression as Method Reference

Method Reference Example - LambdaMethodRefernce.java

Lambda Expression Stream Pipeline Operations

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

Why Synchronization Is Needed in Multi-Threading Applications?

Synchronization Technique - Lock and Synchronized Code

"synchronized" - How Java Supports Synchronization

BankingThread.java - Synchronization Sample Program

BankingThread.java - Synchronization Sample Program Output

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

Outdated Tutorials

Outdated: Downloading and Installing JDK 1.7

Outdated: Downloading and Installing JDK 1.6

Outdated: Downloading and Installing JDK 1.5

Outdated: JDK 1.3 Bug - Memory Leak With Unstarted Threads

References

PDF Printing Version

Keywords: Java, Tutorial, Example, Sample, Book, JDK, JVM