Java Fundamentals and Advanced Concepts

Java Features

Basic Syntax

I. Command-Line Tools for Java Programs

Java command-line tools

II. final, finally, finalize

final, finally, finalize

III. Inheritance

Inheritance

class ParentClass {
    // code
}

class ChildClass extends ParentClass {
    // code
}

IV. Vector, ArrayList, LinkedList

V. Primitive Data Types and Wrapper Classes

Primitive Types and Wrappers

VI. Interfaces and Abstract Classes

Interfaces and Abstract Classes

Advanced Java

Java Reference Queue

Reference Queue

Object counter = new Object();
ReferenceQueue<Object> refQueue = new ReferenceQueue<>();
PhantomReference<Object> p = new PhantomReference<>(counter, refQueue);
counter = null;
System.gc();
try {
    // remove() is a blocking method, can specify a timeout or block indefinitely
    Reference<? extends Object> ref = refQueue.remove(1000L);
    if (ref != null) {
        // do something
    }
} catch (InterruptedException e) {
    // Handle it
}

Java I/O

Java I/O

I/O Utility Classes

  • File

    Description placeholder.

  • RandomAccessFile

    Description placeholder.

  • Byte Streams

    InputStream and OutputStream.

  • Character Streams

    Reader and Writer.

  • Additional Knowledge

    Closeable interface: try-with-resources, try-finally Cleaner or finalize mechanism: last line of resource cleanup

I/O details

NIO (New I/O)

1. Main Components

NIO consists of four main parts: Buffer, Channel, Selector, and Charset.

  • Buffer:

    In NIO, all data is processed through Buffers. Except for boolean, all primitive data types have corresponding Buffer implementations (ByteBuffer, CharBuffer, ShortBuffer, IntBuffer, LongBuffer, FloatBuffer, DoubleBuffer).

    Buffer Details:

    Basic properties: capacity, position, limit, mark.

    capacity: The size of the Buffer, i.e., the length of the underlying array.
    position: The starting index for data operations.
    limit: The operational limit (note: its meaning differs between read and write modes).
    mark: Records the previous position (optional, defaults to 0).
    
  • Channel:

    An abstraction in NIO for batch I/O operations, supporting asynchronous I/O. Common Channel classes include FileChannel, SocketChannel, ServerSocketChannel, and DatagramChannel.

  • Selector:

    A Selector can implement the Reactor pattern, used to listen for events on multiple Channels. It can detect which Channels registered with it are ready, enabling efficient single-thread management of multiple Channels and forming the basis for multiplexing in Java NIO.

  • Charset:

    Provides Unicode string definitions.

2. Core of NIO

Interaction between Buffer and Channel: Data can be read from a Channel into a Buffer, or written from a Buffer to a Channel.

3. Advantages of NIO

NIO efficiently identifies ready Channels and allocates tasks, blocking only during the select() call. This avoids frequent thread switching when many clients connect, significantly improving scalability.

NIO flow

Exception Handling Principles

Exception Handling

Supplementary Concepts

Synchronous vs Asynchronous

  • Synchronous: Subsequent tasks wait for the current call to return before proceeding.
  • Asynchronous: Other tasks do not wait for the current call to return; order is managed via events, callbacks, etc.

Blocking vs Non-blocking

  • Blocking: Unable to perform other tasks until the condition is ready.
  • Non-blocking: The call returns immediately regardless of whether the I/O operation is complete; the operation continues in the background.

Serialization

Description placeholder.

Additional Knowledge

  • Synchronous or blocking operations are not inherently inefficient.
  • In network programming, socket communication is a typical I/O operation target.

Supplementary concepts

Further Details

FileSystemProvider

Two Special Buffers: Direct Buffer and MappedByteBuffer

DirectBuffer:

  • A special form of ByteBuffer that stores data in off-heap memory.

Creating DirectBuffer:

  • Use ByteBuffer.allocateDirect() instead of the traditional allocate().

Key Characteristics:

Feature Description
Direct Access Memory is allocated off-heap, enabling direct access via JNI, avoiding data copy between Java heap and native heap.
Off-heap Memory Memory is not in the Java heap but in the OS native memory.
Performance Suitable for frequent large data access or interaction with native code, as it avoids extra memory copies.

Usage Considerations:

  • Creation and destruction are more expensive than heap ByteBuffers due to OS native memory management.
  • For small data operations, performance may be worse than heap operations due to overhead of small OS allocations/deallocations.
  • Special garbage collection behavior applies.

Basic Buffer Operations

Tags: java NIO I/O Basic Syntax Advanced Java

Posted on Fri, 07 Aug 2026 16:30:05 +0000 by sturbitt