Introduction
Generics in Java allow you to write a class,
interface, or method that works with any type of
data, while providing compile-time type safety.
Instead of specifying the exact type of an object,
you can use a placeholder (a generic type) that will
be replaced by the actual type when the code is
compiled.
Here, the class Box can store any type of object, and the
type T is determined when an object of the class is created
(e.g., Box<Integer> or Box<String>).
Why do we need Generics?
Type Safety: Generics enforce compile-time type checking,
ensuring that the correct type is used and reducing runtime errors.
Code Reusabiity: With generics, you can write more generalized
code. Instead of writing multiple versions of the same method or
class for different types, you can use a single generic method or
class.
Eliminates Explicit Type Casting: When using generics, you don't
need to cast objects to their expected types. The compiler handles
it for you, making the code cleaner and less error-prone.
Enhanced Maintainability: Since the type of data is known at
compile-time, it makes code easier to understand and maintain. It
also makes refactoring safer.
When to Use Generics?
Data Structures:
When creating custom data structures (e.g., linked lists,
stacks, queues) that should work with any type of
object.
Utility Classes:
When writing utility methods that operate on
collections or other types, generics help provide type
safety and reduce code duplication.
APIs:
When you design reusable APIs or libraries that must
work with a variety of types without knowing them in
advance.
Wildcards in Generics
Unbounded Wildcard (<?>)
<?> means any type can be used, and no restrictions are applied.
but it doesn't allow you to modify the elements of the collection.
You can read the elements, but you can’t insert new elements
because you don’t know the exact type.
Upper Bounded Wildcard (<? extends T>)
The upper-bounded wildcard restricts the type to be either the
specified type or a subtype of it.
Lower Bounded Wildcard (<? super T>)
The lower-bounded wildcard allows you to pass a type that is
either the specified type or a supertype of it.
When to Not Use Generics?
When You Need to Work with Primitive Types
Generics cannot work directly with primitive types like int,
char, double, etc. You need to use their wrapper classes (e.g.,
Integer, Character, Double) instead.
When You Don’t Need Type Safety
When the Code is Not Intended for Reusability
Thank you for reading
Please share this with your friends
Manjul Tamang
Software Developer
manjultamang.com.np