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

all about generic in java

Generics in Java enable the creation of classes, interfaces, or methods that can operate on any data type while ensuring compile-time type safety. They enhance code reusability, eliminate the need for explicit type casting, and improve maintainability. Generics are particularly useful in data structures, utility classes, and reusable APIs, but should not be used with primitive types or when type safety is not a concern.

Uploaded by

saddubuddu1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

all about generic in java

Generics in Java enable the creation of classes, interfaces, or methods that can operate on any data type while ensuring compile-time type safety. They enhance code reusability, eliminate the need for explicit type casting, and improve maintainability. Generics are particularly useful in data structures, utility classes, and reusable APIs, but should not be used with primitive types or when type safety is not a concern.

Uploaded by

saddubuddu1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy