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

Final Generics

Generics in Java allow type parameters like Integer and String to be passed to methods, classes, and interfaces. This allows code reuse and prevents errors that could occur without type safety like adding an integer to an ArrayList meant for strings. Generic methods and classes improve type safety and avoid unnecessary casting.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Final Generics

Generics in Java allow type parameters like Integer and String to be passed to methods, classes, and interfaces. This allows code reuse and prevents errors that could occur without type safety like adding an integer to an ArrayList meant for strings. Generic methods and classes improve type safety and avoid unnecessary casting.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Generics in Java

Generics in Java

• Generics in Java is similar to templates in C++. The idea is to allow


type (Integer, String, … etc and user defined types) to be a parameter
to methods, classes and interfaces. For example, classes like ArrayList
use generics very well. We can use them for any type.
To create objects of generic class, we use following syntax.

• / To create an instance of generic class


• BaseType <Type> obj = new BaseType <Type>()

• Note: In Parameter type we can not use primitives like


• 'int','char' or 'double'.
Z
Output
• 15
• GeeksForGeeks
• We can also pass multiple Type parameters
in Generic classes.
Output
• GfG
• 15
Generic Functions:

• We can also write generic functions that can be called with different
types of arguments based on the type of arguments passed to generic
method, the compiler handles each method.
Output
• java.lang.Integer = 11
• java.lang.String = GeeksForGeeks
• java.lang.Double = 1.0
Advantages of Generics:
• Programs that uses Generics has got many benefits over non-generic
code.
1. Code Reuse: We can write a method/class/interface once and use for
any type we want.
2. Type Safety : Generics make errors to appear compile time than at run
time (It’s always better to know problems in your code at compile
time rather than making your code fail at run time). Suppose you
want to create an ArrayList that store name of students and if by
mistake programmer adds an integer object instead of string,
compiler allows it. But, when we retrieve this data from ArrayList, it
causes problems at runtime.
Output
• Exception in thread "main" java.lang.ClassCastException:
• java.lang.Integer cannot be cast to java.lang.String
• at Test.main(Test.java:19)
How generics solve this problem?
• At the time of defining ArrayList, we can specify that this list can take
only String objects.
Output
• 15: error: no suitable method found for add(int)
• al.add(10);
• ^
Advantages of Generics:
3. Individual Type Casting is not needed: If we do not use generics,
then, in the above example every-time we retrieve data from ArrayList,
we have to typecast it. Typecasting at every retrieval operation is a big
headache. If we already know that our list only holds string data then
we need not to typecast it every time.
Advantages of Generics:
• Implementing generic algorithms: By using generics, we can
implement algorithms that work on different types of objects and at
the same they are type safe too.

You might also like