A comprehensive guide to all Java 5 concepts with practical examples for interview preparation.
- Generics
- Enhanced For-Loop (For-Each)
- Autoboxing and Unboxing
- Enumerations (Enum)
- Varargs (Variable-Length Arguments)
- Static Imports
- Annotations
- Common Interview Questions
Generics provide type safety by allowing types to be parameters when defining classes, interfaces, and methods.
Generics enable you to write code that works with different types while maintaining type safety at compile time.
// Generic class
class Box<T> {
private T item;
public void setItem(T item) {
this.item = item;
}
public T getItem() {
return item;
}
}
// Usage
Box<String> stringBox = new Box<>();
stringBox.setItem("Hello");
String value = stringBox.getItem();public static <T> void printArray(T[] array) {
for (T element : array) {
System.out.println(element);
}
}// Upper bounded wildcard
List<? extends Number> numbers;
// Lower bounded wildcard
List<? super Integer> integers;
// Unbounded wildcard
List<?> list;- Type safety
- Eliminates casting
- Better code reusability
- Compile-time type checking
See GenericsExample.java for complete example.
The enhanced for-loop provides a simpler way to iterate over collections and arrays.
The for-each loop eliminates the need for explicit iterator or index management.
// For arrays
int[] numbers = {1, 2, 3, 4, 5};
for (int num : numbers) {
System.out.println(num);
}
// For collections
List<String> list = new ArrayList<>();
for (String item : list) {
System.out.println(item);
}- Simpler syntax
- Less error-prone
- More readable
- Automatic iteration
See EnhancedForLoopExample.java for complete example.
Automatic conversion between primitive types and their wrapper classes.
Autoboxing converts primitives to wrapper objects, and unboxing converts wrapper objects to primitives.
// Autoboxing: int -> Integer
Integer i = 10; // Automatically boxes int to Integer
// Unboxing: Integer -> int
int value = i; // Automatically unboxes Integer to int
// In collections
List<Integer> numbers = new ArrayList<>();
numbers.add(1); // Autoboxing
int num = numbers.get(0); // Unboxing- Convenient
- Works seamlessly with collections
- Less boilerplate code
See AutoboxingExample.java for complete example.
Enums provide a type-safe way to define a fixed set of constants.
Enums are classes that represent a group of constants.
enum Day {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}
// Usage
Day today = Day.MONDAY;enum Planet {
MERCURY(3.303e+23, 2.4397e6),
VENUS(4.869e+24, 6.0518e6);
private final double mass;
private final double radius;
Planet(double mass, double radius) {
this.mass = mass;
this.radius = radius;
}
public double getMass() {
return mass;
}
}- Type safety
- Can have methods and fields
- Switch statement support
- Better than constants
See EnumExample.java for complete example.
Varargs allow methods to accept a variable number of arguments.
Varargs simplify method calls by allowing zero or more arguments of the same type.
public static void printNumbers(int... numbers) {
for (int num : numbers) {
System.out.println(num);
}
}
// Usage
printNumbers(1, 2, 3);
printNumbers(1, 2, 3, 4, 5);
printNumbers(); // Zero arguments- Variable number of arguments
- Treated as array
- Must be last parameter
- Can have other parameters before it
See VarargsExample.java for complete example.
Static imports allow you to import static members of a class.
Static imports eliminate the need to qualify static members with their class name.
import static java.lang.Math.*;
// Now can use without Math prefix
double result = sqrt(16); // Instead of Math.sqrt(16)
double pi = PI; // Instead of Math.PI- Less verbose code
- More readable
- Convenient for constants and utility methods
See StaticImportsExample.java for complete example.
Annotations provide metadata about program elements.
Annotations can be used by tools and frameworks to generate code, perform validation, etc.
@Override
public String toString() {
return "Overridden method";
}
@Deprecated
public void oldMethod() {
// Deprecated method
}
@SuppressWarnings("unchecked")
List list = new ArrayList();@interface MyAnnotation {
String value();
int count() default 1;
}
@MyAnnotation(value = "test", count = 5)
public class MyClass {
}- Metadata for code
- Used by tools and frameworks
- Compile-time and runtime processing
- Can have parameters
See AnnotationsExample.java for complete example.
A: Type parameters for classes and methods:
- Type safety at compile time
- Eliminates casting
- Better code reusability
- Wildcards for flexibility
A:
- ArrayList: Raw type, no type safety, requires casting
- ArrayList: Generic type, type-safe, no casting needed
- Always use generics for type safety
A: Automatic conversion:
- Primitive to wrapper (autoboxing)
- Wrapper to primitive (unboxing)
- Happens automatically
- Used extensively with collections
A: Type-safe constants:
- Fixed set of values
- Can have methods and fields
- Better than public static final constants
- Switch statement support
A: Variable-length arguments:
- Method accepts variable number of arguments
- Treated as array
- Must be last parameter
- Simplifies method calls
Last Updated: 2025
Version: 1.0
This guide covers the features of Java 5, released on September 30, 2004.