Java 5 — Major Language and API Enhancements
| # | Feature | Description | Small Example |
|---|---|---|---|
| 1 | Generics | Provides compile-time type safety for collections and reduces explicit casting. |
List<String> names = new ArrayList<String>();
|
| 2 | Enhanced for-loop | Provides a simple way to iterate over arrays and collections. |
for(String name : names)
|
| 3 | Autoboxing / Unboxing | Automatically converts primitives to wrapper objects and wrapper objects back to primitives. |
Integer x = 10;
|
| 4 | Typesafe Enum | Provides a type-safe way to represent a fixed set of constants. |
enum Day { MON, TUE, WED }
|
| 5 | Varargs | Allows a method to accept a variable number of arguments. |
void sum(int... nums)
|
| 6 | Static Import | Allows static members to be used without specifying the class name. |
import static java.lang.Math.*;
|
| 7 | Annotations | Adds metadata to Java source code. |
@Override
|
| 8 | Covariant Return Types | An overriding method can return a more specific type than the parent method. |
Dog getAnimal()
|
| 9 | Generic Methods | Methods can define their own generic type parameters. |
<T> void print(T value)
|
| 10 | Concurrency Utilities |
Introduced powerful concurrency utilities under
java.util.concurrent.
|
Executors.newFixedThreadPool(2)
|
| 11 | Formatted Output |
Provides formatted output using
printf().
|
System.out.printf(...)
|
| 12 | Scanner | Simplifies reading and parsing input. |
Scanner sc = new Scanner(System.in);
|
Generics provide compile-time type safety and eliminate unnecessary casting.
List<String> names = new ArrayList<String>();
names.add("Java");
names.add("Spring");
String name = names.get(0);
System.out.println(name);
The enhanced for-loop provides a simpler way to iterate through arrays and collections.
List<String> names = new ArrayList<String>();
names.add("Java");
names.add("Spring");
for (String name : names) {
System.out.println(name);
}
Integer number = 10; // Autoboxing int value = number; // Unboxing
Java automatically converts between primitive types and their corresponding wrapper classes.
enum Status {
NEW,
PROCESSING,
COMPLETED
}
Status status = Status.NEW;
static int sum(int... numbers) {
int total = 0;
for (int n : numbers) {
total += n;
}
return total;
}
sum(10, 20);
sum(10, 20, 30, 40);
Internally, varargs are treated as an array.
Without static import:
System.out.println(Math.sqrt(25)); System.out.println(Math.max(10, 20));
With static import:
import static java.lang.Math.*; System.out.println(sqrt(25)); System.out.println(max(10, 20));
@Override
public String toString() {
return "Employee";
}
Common annotations:
@Override
@Deprecated
@SuppressWarnings("unchecked")
class Animal {
Animal getAnimal() {
return new Animal();
}
}
class Dog extends Animal {
@Override
Dog getAnimal() {
return new Dog();
}
}
The overriding method can return a subtype of the parent's return type.
static <T> void print(T value) {
System.out.println(value);
}
print("Java");
print(100);
print(10.5);
Java 5 introduced the
java.util.concurrent package with
powerful concurrency utilities.
ExecutorService executor =
Executors.newFixedThreadPool(2);
executor.submit(new Runnable() {
public void run() {
System.out.println("Task running");
}
});
executor.shutdown();
Important classes:
String name = "John";
int age = 25;
System.out.printf(
"Name: %s, Age: %d%n",
name,
age
);
Scanner sc = new Scanner(System.in);
System.out.print("Enter age: ");
int age = sc.nextInt();
System.out.println("Age = " + age);
Scanner makes it easier to read and parse input from the console and other input sources.
java.util.concurrent.