JDK 1.5 Features

Java 5 — Major Language and API Enhancements

JDK 1.5, also known as Java 5, was released in 2004 and introduced several important language features that significantly improved Java development.

JDK 1.5 Features at a Glance

# 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);

Detailed Examples

1. Generics

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);
Benefit: Type safety at compile time and no explicit casting.

2. Enhanced for-loop

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);
}

3. Autoboxing and Unboxing

Integer number = 10;  // Autoboxing

int value = number;   // Unboxing

Java automatically converts between primitive types and their corresponding wrapper classes.

4. Typesafe Enum

enum Status {
    NEW,
    PROCESSING,
    COMPLETED
}

Status status = Status.NEW;
Use: Best suited for a fixed set of constants.

5. Varargs

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.

6. Static Import

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));

7. Annotations

@Override
public String toString() {
    return "Employee";
}

Common annotations:

@Override
@Deprecated
@SuppressWarnings("unchecked")
Purpose: Annotations provide metadata to the compiler, runtime, or frameworks.

8. Covariant Return Type

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.

9. Generic Method

static <T> void print(T value) {
    System.out.println(value);
}

print("Java");
print(100);
print(10.5);

10. Concurrency Utilities

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:

  • ExecutorService
  • Future
  • Callable
  • ConcurrentHashMap
  • BlockingQueue
  • CountDownLatch
  • Semaphore
  • ReentrantLock

11. Formatted Output - printf()

String name = "John";
int age = 25;

System.out.printf(
    "Name: %s, Age: %d%n",
    name,
    age
);
Output:
Name: John, Age: 25

12. Scanner

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.

⭐ Interview Short Answer

Question: What are the major features introduced in JDK 1.5?

Answer: JDK 1.5, also called Java 5, introduced Generics, Enhanced for-loop, Autoboxing/Unboxing, Enums, Varargs, Static Imports, Annotations, Covariant Return Types, and major concurrency utilities through java.util.concurrent.