@Pravanjan_17p
GENERICS
In
Advance Java
Definition of
GENERICS IN JAVA
What is Generics in Java
Generics
Generics in Java are a powerful feature that allows you
to write reusable and type-safe code. They are
particularly useful in the context of collections, but their
application extends far beyond that. Generics were
introduced in Java 5 and are part of the java.util
package.
@pravanjan_17p
GENERICS
KEY CONCEPTS
OF GENERICS
1. TYPE SAFETY
Generics enable you to specify a type for a
collection or method at compile time, preventing
runtime ClassCastException.
2. CODE REUSABILITY
By using a single generic class, interface, or
method, you can work with different types of data,
reducing redundancy.
@PRAVANJAN_17P
HOW GENERICS WORK
1. Generic Classes
A generic class allows you to define classes that
work with any data type.
Pravanjan.txt
class Box<T> {
private T item;
public void setItem(T item) {
this.item = item; }
public T getItem() {
return item; } }
public class Main {
public static void main(String[] args) {
Box<String> stringBox = new Box<>();
stringBox.setItem("Hello");
System.out.println(stringBox.getItem());
Box<Integer> intBox = new Box<>();
intBox.setItem(123);
System.out.println(intBox.getItem());
}
}
@pravanjan_17p
HOW GENERICS WORK
2. Generic Methods
A generic method allows you to declare a
method with type parameters.
Pravanjan.txt
class Util {
public static <T> void printArray(T[] array) {
for (T element : array) {
System.out.println(element);
}
}
}
public class Main {
public static void main(String[] args) {
Integer[] intArray = {1, 2, 3};
String[] strArray = {"A", "B", "C"};
Util.printArray(intArray);
Util.printArray(strArray);
}
}
@pravanjan_17p
HOW GENERICS WORK
3. Bounded Type Parameters
You can restrict a type parameter to a specific
range using extends or super.
Pravanjan.txt
class MathUtils {
public static <T extends Number> double square(T
number) {
return number.doubleValue() *
number.doubleValue();
}
}
public class Main {
public static void main(String[] args) {
System.out.println(MathUtils.square(5)); //
Works with Integer
System.out.println(MathUtils.square(5.5)); //
Works with Double
}
}
@pravanjan_17p
GENERICS AND COLLECTIONS
Example: Using Generics with Lists
The Java Collections Framework heavily uses generics for
type safety.
Pravanjan.txt
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<String> names = new ArrayList<>();
names.add("Alice");
names.add("Bob");
for (String name : names) {
System.out.println(name);
}
}
}
@pravanjan_17p
GENERICS AND COLLECTIONS
Example: Using Generics with Maps
Pravanjan.txt
import java.util.HashMap;
public class Main {
public static void main(String[] args) {
HashMap<Integer, String> map = new HashMap<>();
map.put(1, "One");
map.put(2, "Two");
for (Integer key : map.keySet()) {
System.out.println("Key: " + key + ", Value:
" + map.get(key));
}
}
}
@pravanjan_17p
JAVA
Advantages of Generics
1 - Type Safety:
Prevents runtime errors by catching type
mismatches at compile time
2 - Code Reusability:
Allows writing generic code that works
with any data type.
3 - Performance:
Eliminates the need for typecasting,
reducing runtime overhead.
@pravanjan_17p
JAVA
Limitations of Generics
1 - Type Erasure:
Generics are implemented using type erasure,
so the type parameter is removed at runtime.
2 - Cannot Use Primitive Types:
Generics work only with objects, not primitives
(use wrapper classes like Integer for int).
3 - No Static Members:
Generic classes cannot have static members
with type parameters.
@pravanjan_17p
Did you like it?
follow for more!
Like Comment Share Save
@pravanjan_17p