A comprehensive guide to all Java 7 concepts with practical examples for interview preparation.
- Try-with-Resources
- Diamond Operator
- Strings in Switch
- Binary Literals and Underscores in Numeric Literals
- Multi-Catch
- Fork/Join Framework
- NIO.2
- Common Interview Questions
Try-with-resources automatically closes resources that implement AutoCloseable.
Try-with-resources eliminates the need for explicit finally blocks to close resources.
// Automatic resource management
try (BufferedReader br = new BufferedReader(new FileReader("file.txt"))) {
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
// Resource automatically closedtry (FileInputStream fis = new FileInputStream("input.txt");
FileOutputStream fos = new FileOutputStream("output.txt")) {
// Use resources
}- Automatic resource cleanup
- Less boilerplate code
- Prevents resource leaks
- Cleaner code
See TryWithResourcesExample.java for complete example.
The diamond operator (<>) simplifies generic type declarations.
The diamond operator allows the compiler to infer generic types from the context.
// Before Java 7
List<String> list = new ArrayList<String>();
// Java 7+
List<String> list = new ArrayList<>(); // Type inferred- Less verbose
- More readable
- Type inference
- Less repetition
See DiamondOperatorExample.java for complete example.
Switch statements can now use String values.
String-based switch statements provide cleaner code than multiple if-else statements.
String day = "MONDAY";
switch (day) {
case "MONDAY":
System.out.println("Start of week");
break;
case "FRIDAY":
System.out.println("End of week");
break;
default:
System.out.println("Other day");
}- Cleaner than if-else chains
- More readable
- Better performance than if-else
- Type-safe
See StringSwitchExample.java for complete example.
Java 7 allows binary literals and underscores in numeric literals for better readability.
Binary literals and underscores make numeric constants more readable.
// Binary literals
int binary = 0b1010; // 10 in decimal
long binaryLong = 0b1010L;
// Underscores in numeric literals
int million = 1_000_000;
long creditCard = 1234_5678_9012_3456L;
double pi = 3.14159_26535;- Better readability
- Binary literal support
- Easier to read large numbers
- No impact on value
See NumericLiteralsExample.java for complete example.
Multi-catch allows catching multiple exception types in a single catch block.
Multi-catch reduces code duplication when handling multiple exceptions the same way.
try {
// Code that may throw multiple exceptions
} catch (IOException | SQLException e) {
// Handle both exceptions
e.printStackTrace();
}- Less code duplication
- Cleaner exception handling
- More readable
- Same handling for multiple exceptions
See MultiCatchExample.java for complete example.
Fork/Join provides a framework for parallel processing of tasks.
Fork/Join is designed for divide-and-conquer algorithms that can be parallelized.
import java.util.concurrent.*;
class SumTask extends RecursiveTask<Long> {
private int[] array;
private int start, end;
SumTask(int[] array, int start, int end) {
this.array = array;
this.start = start;
this.end = end;
}
@Override
protected Long compute() {
if (end - start < 1000) {
// Direct computation
long sum = 0;
for (int i = start; i < end; i++) {
sum += array[i];
}
return sum;
} else {
// Fork
int mid = (start + end) / 2;
SumTask left = new SumTask(array, start, mid);
SumTask right = new SumTask(array, mid, end);
left.fork();
long rightResult = right.compute();
long leftResult = left.join();
return leftResult + rightResult;
}
}
}
// Usage
ForkJoinPool pool = new ForkJoinPool();
SumTask task = new SumTask(array, 0, array.length);
long result = pool.invoke(task);- Divide-and-conquer parallelism
- Work-stealing algorithm
- Efficient for recursive tasks
- Automatic load balancing
See ForkJoinExample.java for complete example.
NIO.2 provides improved file I/O operations and file system access.
NIO.2 (New I/O 2) enhances file operations with better APIs and performance.
import java.nio.file.*;
// Path operations
Path path = Paths.get("file.txt");
Files.exists(path);
Files.createFile(path);
Files.delete(path);
// Reading files
List<String> lines = Files.readAllLines(path);
byte[] bytes = Files.readAllBytes(path);
// Writing files
Files.write(path, "content".getBytes());- Path API
- Files utility class
- Directory watching
- Symbolic links support
- Better file operations
See NIO2Example.java for complete example.
A: Automatic resource management:
- Resources automatically closed
- Implements AutoCloseable
- Prevents resource leaks
- Cleaner than finally blocks
A: Type inference for generics:
new ArrayList<>()instead ofnew ArrayList<String>()- Compiler infers type
- Less verbose code
- Java 7+ feature
A: Yes, since Java 7:
- String-based switch statements
- Cleaner than if-else chains
- Better performance
- Type-safe
A: Catching multiple exceptions:
catch (IOException | SQLException e)- Same handling for multiple exceptions
- Less code duplication
- More readable
A: Parallel processing framework:
- Divide-and-conquer algorithms
- Work-stealing algorithm
- Efficient for recursive tasks
- Automatic load balancing
Last Updated: 2025
Version: 1.0
This guide covers the features of Java 7, released on July 28, 2011.