A comprehensive guide to all Java 1.4 concepts with practical examples for interview preparation.
- Assertions
- Regular Expressions
- Exception Chaining
- NIO (New Input/Output)
- Logging API
- Common Interview Questions
Assertions provide a way to test assumptions in code during development.
Assertions help catch programming errors by testing assumptions that should always be true.
// Simple assertion
assert condition;
// Assertion with message
assert condition : "Error message";
// Example
assert x > 0 : "x must be positive";public int divide(int a, int b) {
assert b != 0 : "Division by zero";
return a / b;
}# Enable assertions
java -ea MyClass
# Disable assertions (default)
java -da MyClass- Development-time testing
- Can be enabled/disabled
- Helps catch bugs early
- Not for production error handling
See AssertionsExample.java for complete example.
Java 1.4 introduced the java.util.regex package for pattern matching.
Regular expressions provide powerful pattern matching capabilities for strings.
import java.util.regex.*;
// Pattern matching
String text = "The quick brown fox";
Pattern pattern = Pattern.compile("\\b\\w{4}\\b");
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println("Found: " + matcher.group());
}
// String methods with regex
String result = text.replaceAll("\\s+", " ");
boolean matches = text.matches(".*fox.*");- Pattern compilation
- Matcher for finding patterns
- String methods support
- Group capturing
- Replacement operations
See RegularExpressionsExample.java for complete example.
Exception chaining allows exceptions to wrap other exceptions, preserving the original cause.
Exception chaining helps maintain the full exception history when exceptions are caught and rethrown.
try {
// Some operation
} catch (IOException e) {
throw new MyException("Operation failed", e); // Chain exception
}
// Accessing chained exception
try {
// Code
} catch (MyException e) {
Throwable cause = e.getCause(); // Get original exception
cause.printStackTrace();
}- Preserve exception history
- Better error diagnostics
- Cause tracking
- Improved debugging
See ExceptionChainingExample.java for complete example.
NIO provides non-blocking I/O operations and improved performance.
NIO offers better I/O performance through buffers, channels, and selectors.
import java.nio.*;
import java.nio.channels.*;
import java.nio.file.*;
// FileChannel
FileChannel channel = FileChannel.open(Paths.get("file.txt"));
ByteBuffer buffer = ByteBuffer.allocate(1024);
channel.read(buffer);
// Selector for non-blocking I/O
Selector selector = Selector.open();
ServerSocketChannel serverChannel = ServerSocketChannel.open();
serverChannel.configureBlocking(false);
serverChannel.register(selector, SelectionKey.OP_ACCEPT);- Non-blocking I/O
- Buffers for efficient I/O
- Channels for I/O operations
- Selectors for multiplexing
- Better performance
See NIOExample.java for complete example.
Java 1.4 introduced the java.util.logging package for application logging.
The Logging API provides a standard way to log messages in Java applications.
import java.util.logging.*;
Logger logger = Logger.getLogger("MyLogger");
logger.setLevel(Level.INFO);
logger.info("Information message");
logger.warning("Warning message");
logger.severe("Error message");- Multiple log levels
- Handlers for output
- Formatters for formatting
- Configurable logging
- Standard API
See LoggingExample.java for complete example.
A: Development-time testing mechanism:
- Test assumptions in code
- Can be enabled/disabled with -ea flag
- Not for production error handling
- Helps catch bugs early
A: Pattern matching with java.util.regex:
- Pattern class for compiling regex
- Matcher class for finding matches
- String methods support regex
- Group capturing and replacement
A: Wrapping exceptions:
- Preserve original exception as cause
- Better error diagnostics
- Full exception history
- Access via getCause() method
A: New Input/Output:
- Non-blocking I/O
- Buffers for efficient I/O
- Channels for I/O operations
- Selectors for multiplexing
- Better performance than traditional I/O
A: Standard logging framework:
- Multiple log levels (INFO, WARNING, SEVERE)
- Handlers for output destinations
- Formatters for message formatting
- Configurable logging behavior
Last Updated: 2025
Version: 1.0
This guide covers the features of Java 1.4, released on February 6, 2002.