0% found this document useful (0 votes)
3 views

Generics in Java

Java Generics, introduced in Java 5, enhances code reusability and type safety by allowing types to be passed as parameters to classes, methods, or interfaces. It includes generic methods and classes, with basic syntax involving type parameters like T, K, and V. Generics also integrate with Java Collections, providing type-safe operations and supporting wildcards for flexible type handling.

Uploaded by

Rohan Choudhary
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Generics in Java

Java Generics, introduced in Java 5, enhances code reusability and type safety by allowing types to be passed as parameters to classes, methods, or interfaces. It includes generic methods and classes, with basic syntax involving type parameters like T, K, and V. Generics also integrate with Java Collections, providing type-safe operations and supporting wildcards for flexible type handling.

Uploaded by

Rohan Choudhary
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Understanding Generics

in JAVA
Improving Code Reusability and Type Safety
What Are Generics?
• Java Generics is a set of related methods or a set of
similar types.
• Generics allow types Integer, String, or even user-
defined types to be passed as a parameter to classes,
methods, or interfaces.
List<String> list = new ArrayList<>();
• Introduced in Java5
list.add("Hello");
• Ensures type safety String s = list.get(0); // No casting needed
• Code reusability
• Reduces casting
Types of Generics
• Generic Method: Generic Java method takes a parameter and
returns some value after performing a task. It is exactly like a
normal function, however, a generic method has type parameters
that are cited by actual type. This allows the generic method to be
used in a more general way. The compiler takes care of the type of
safety which enables programmers to code easily since they do not
have to perform long, individual type castings.
• Generic Classes: A generic class is implemented exactly like a
non-generic class. The only difference is that it contains a type
parameter section. There can be more than one type of parameter,
separated by a comma. The classes, which accept one or more
parameters, ?are known as parameterized classes or parameterized
types.
// To create an instance of generic class
BaseType <Type> obj = new BaseType <Type>()

Basic Syntax of Generics


class Box<T> {
private T t;
public void set(T t) { this.t = t; }
public T get() { return t; }
}
// To create an instance of generic class
Box <String> obj = new Box <String>() ;
Type Parameters in Generics
• T – Type public class Pair<K, V> {
private K key;
• E – Element private V value;
• K – Key public Pair(K key, V value) { this.key = key; this.value = value; }
public K getKey() { return key; }
• V – Value public V getValue() { return value; }
}
Generics with Java Collections
• Generics in core collections: List<T>, Set<T>, Map<K, V>.
List<Integer> numbers = new ArrayList<>();
numbers.add(10);
numbers.add(20);
• Provides Type-safe operations.
Wildcards in Generics
Unbounded wildcard: List<?>
These are useful in the following cases –
• When writing a method that can be employed using functionality provided in Object
class.
• When the code is using methods in the generic class that doesn’t depend on the type
parameter

Upper bounded wildcard: List<? extends Number>


These wildcards can be used when you want to relax the restrictions on a variable. For example,
say you want to write a method that works on List < Integer >, List < Double >, and List <
Number >, you can do this using an upper bounded wildcard.

Lower bounded wildcard: List<? super Integer>


It is expressed using the wildcard character (‘?’), followed by the super keyword, followed by its
lower bound: <? super A>.
Unbounded wildcards
Upper bounded Wildcards
Lower Bounded Wildcards
Defining Generic Methods
public static <T> T getFirstElement(T[] array) {
return array[0];
}

You might also like