SlideShare a Scribd company logo
PRESENTED BY:
ANSHU SHARMA
Object Oriented Using Java -
Generic 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
HashSet, ArrayList, HashMap, etc use
generics very well. We can use them for any
type.
GENERIC CLASS
 we use <> to specify parameter types in generic class
creation. 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'.
EXAMPLES: GENERIC CLASS
/ A Simple Java program to show working of user defined Generic classes
// We use < > to specify Parameter type
class Test<T>
{
// An object of type T is declared
T obj;
Test(T obj)
{
this.obj = obj;
} // constructor
public T getObject()
{
return this.obj;
}
}
EXAMPLE CONTD.
class Main
{
public static void main (String[] args)
{
// instance of Integer type
Test <Integer> iObj = new Test<Integer>(15);
System.out.println(iObj.getObject());
// instance of String type
Test <String> sObj =
new Test<String>("GeeksForGeeks");
System.out.println(sObj.getObject());
}
}
MULTIPLE TYPE PARAMETERS IN GENERIC
CLASSES
We can also pass multiple Type parameters in Generic classes.
// A Simple Java program to show multiple
// type parameters in Java Generics
// We use < > to specify Parameter type
class Test<T, U>
{
T obj1; // An object of type T
U obj2; // An object of type U
// constructor
Test(T obj1, U obj2)
{
this.obj1 = obj1;
this.obj2 = obj2;
}
EXAMPLE CONTD.
// To print objects of T and U
public void print()
{
System.out.println(obj1);
System.out.println(obj2);
}
}
// Driver class to test above
class Main
{
public static void main (String[] args)
{
Test <String, Integer> obj =
new Test<String, Integer>("GfG", 15);
obj.print();
}
}
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.
EXAMPLE:
// A Simple Java program to show working of user defined
// Generic functions
class Test
{
// A Generic method example
static <T> void genericDisplay (T element)
{
System.out.println(element.getClass().getName() +
" = " + element);
}
EXAMPLE CONTD.
// Driver method
public static void main(String[] args)
{
// Calling generic method with Integer argument
genericDisplay(11);
// Calling generic method with String argument
genericDisplay("GeeksForGeeks");
// Calling generic method with double argument
genericDisplay(1.0);
}
}
ADVANTAGES OF JAVA GENERICS
1. Code Reusability
 Generics allow us to write code that will
work with different types of data. For
example,
public <T> void genericsMethod(T data) {...}
Here, we have created a generics method.
This method can be used to perform
operations on integer data, string data and
so on.
ADVANTAGES CONTD.
2. Compile-time Type Checking
 The type parameter of generics provides information
about the type of data used in the generics code.
 Hence, any error can be identified at compile time which
is easier to fix than runtime errors. For example,
 // without using Generics
NormalClass list = new NormalClass(); // calls method of
NormalClass
list.display("String");
In the above code, we have a normal class. We call the
method named display() of this class by passing a string
data.
 Here, the compiler does not know if the value passed in
the argument is correct or not.
ADVANTAGES CONTD.
 However, let's see what will happen if we use the
generics class instead.
 // using Generics
GenericsClass<Integer> list = new
GenericsClass<>(); // calls method of GenericsClass
list2.display("String");
In the above code, we have a generics class. Here,
the type parameter indicates that the class is working
on Integer data. Hence when the string data is
passed in argument, the compiler will generate an
error.

More Related Content

What's hot (20)

Packages in java
Packages in javaPackages in java
Packages in java
 
Java - Generic programming
Java - Generic programmingJava - Generic programming
Java - Generic programming
 
Java package
Java packageJava package
Java package
 
Core java concepts
Core java  conceptsCore java  concepts
Core java concepts
 
Class and Objects in Java
Class and Objects in JavaClass and Objects in Java
Class and Objects in Java
 
Method overloading
Method overloadingMethod overloading
Method overloading
 
Encapsulation C++
Encapsulation C++Encapsulation C++
Encapsulation C++
 
Generics
GenericsGenerics
Generics
 
L21 io streams
L21 io streamsL21 io streams
L21 io streams
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
 
Abstract class in java
Abstract class in javaAbstract class in java
Abstract class in java
 
Wrapper classes
Wrapper classes Wrapper classes
Wrapper classes
 
Wrapper class
Wrapper classWrapper class
Wrapper class
 
Constructor overloading & method overloading
Constructor overloading & method overloadingConstructor overloading & method overloading
Constructor overloading & method overloading
 
Methods in java
Methods in javaMethods in java
Methods in java
 
Java IO
Java IOJava IO
Java IO
 
Final keyword in java
Final keyword in javaFinal keyword in java
Final keyword in java
 
Java 8 Default Methods
Java 8 Default MethodsJava 8 Default Methods
Java 8 Default Methods
 
Packages in java
Packages in javaPackages in java
Packages in java
 

Similar to Generic programming in java

Generic Types in Java (for ArtClub @ArtBrains Software)
Generic Types in Java (for ArtClub @ArtBrains Software)Generic Types in Java (for ArtClub @ArtBrains Software)
Generic Types in Java (for ArtClub @ArtBrains Software)Andrew Petryk
 
Jdk1.5 Features
Jdk1.5 FeaturesJdk1.5 Features
Jdk1.5 Featuresindia_mani
 
Introduction to new features in java 8
Introduction to new features in java 8Introduction to new features in java 8
Introduction to new features in java 8Raffi Khatchadourian
 
Java New Programming Features
Java New Programming FeaturesJava New Programming Features
Java New Programming Featurestarun308
 
Generic Programming in java
Generic Programming in javaGeneric Programming in java
Generic Programming in javaGarik Kalashyan
 
Introduction to Intermediate Java
Introduction to Intermediate JavaIntroduction to Intermediate Java
Introduction to Intermediate JavaPhilip Johnson
 
Working with Methods in Java.pptx
Working with Methods in Java.pptxWorking with Methods in Java.pptx
Working with Methods in Java.pptxmaryansagsgao
 
documents.pub_new-features-in-java-8-it-jpoialjavanaitedwien15java8pdf-java-8...
documents.pub_new-features-in-java-8-it-jpoialjavanaitedwien15java8pdf-java-8...documents.pub_new-features-in-java-8-it-jpoialjavanaitedwien15java8pdf-java-8...
documents.pub_new-features-in-java-8-it-jpoialjavanaitedwien15java8pdf-java-8...Akaks
 
Android Training (Java Review)
Android Training (Java Review)Android Training (Java Review)
Android Training (Java Review)Khaled Anaqwa
 

Similar to Generic programming in java (20)

Generic Types in Java (for ArtClub @ArtBrains Software)
Generic Types in Java (for ArtClub @ArtBrains Software)Generic Types in Java (for ArtClub @ArtBrains Software)
Generic Types in Java (for ArtClub @ArtBrains Software)
 
Generics
GenericsGenerics
Generics
 
Jdk1.5 Features
Jdk1.5 FeaturesJdk1.5 Features
Jdk1.5 Features
 
Introduction to new features in java 8
Introduction to new features in java 8Introduction to new features in java 8
Introduction to new features in java 8
 
Introduction to new features in java 8
Introduction to new features in java 8Introduction to new features in java 8
Introduction to new features in java 8
 
Java New Programming Features
Java New Programming FeaturesJava New Programming Features
Java New Programming Features
 
Java generics final
Java generics finalJava generics final
Java generics final
 
Generic Programming in java
Generic Programming in javaGeneric Programming in java
Generic Programming in java
 
Templates
TemplatesTemplates
Templates
 
Introduction to Intermediate Java
Introduction to Intermediate JavaIntroduction to Intermediate Java
Introduction to Intermediate Java
 
Computer programming 2 Lesson 15
Computer programming 2  Lesson 15Computer programming 2  Lesson 15
Computer programming 2 Lesson 15
 
Working with Methods in Java.pptx
Working with Methods in Java.pptxWorking with Methods in Java.pptx
Working with Methods in Java.pptx
 
documents.pub_new-features-in-java-8-it-jpoialjavanaitedwien15java8pdf-java-8...
documents.pub_new-features-in-java-8-it-jpoialjavanaitedwien15java8pdf-java-8...documents.pub_new-features-in-java-8-it-jpoialjavanaitedwien15java8pdf-java-8...
documents.pub_new-features-in-java-8-it-jpoialjavanaitedwien15java8pdf-java-8...
 
Java Generics
Java GenericsJava Generics
Java Generics
 
Java 5 Features
Java 5 FeaturesJava 5 Features
Java 5 Features
 
Synapseindia dot net development
Synapseindia dot net developmentSynapseindia dot net development
Synapseindia dot net development
 
Ppt of c vs c#
Ppt of c vs c#Ppt of c vs c#
Ppt of c vs c#
 
Generics C#
Generics C#Generics C#
Generics C#
 
Generics_RIO.ppt
Generics_RIO.pptGenerics_RIO.ppt
Generics_RIO.ppt
 
Android Training (Java Review)
Android Training (Java Review)Android Training (Java Review)
Android Training (Java Review)
 

More from anshu_atri

Types of input and output devices
Types of input and output devicesTypes of input and output devices
Types of input and output devicesanshu_atri
 
Types of computers
Types of computersTypes of computers
Types of computersanshu_atri
 
Intro of computers
Intro of computersIntro of computers
Intro of computersanshu_atri
 
Observer pattern
Observer patternObserver pattern
Observer patternanshu_atri
 
Examination process (1)
Examination process (1)Examination process (1)
Examination process (1)anshu_atri
 
Iterator pattern
Iterator patternIterator pattern
Iterator patternanshu_atri
 
Singleton design pattern
Singleton design patternSingleton design pattern
Singleton design patternanshu_atri
 
Oops design pattern intro
Oops design pattern intro Oops design pattern intro
Oops design pattern intro anshu_atri
 
Acem web designing
Acem web designingAcem web designing
Acem web designinganshu_atri
 
Super and final in java
Super and final in javaSuper and final in java
Super and final in javaanshu_atri
 
Aravali college of engg. and management
Aravali college of engg. and managementAravali college of engg. and management
Aravali college of engg. and managementanshu_atri
 

More from anshu_atri (11)

Types of input and output devices
Types of input and output devicesTypes of input and output devices
Types of input and output devices
 
Types of computers
Types of computersTypes of computers
Types of computers
 
Intro of computers
Intro of computersIntro of computers
Intro of computers
 
Observer pattern
Observer patternObserver pattern
Observer pattern
 
Examination process (1)
Examination process (1)Examination process (1)
Examination process (1)
 
Iterator pattern
Iterator patternIterator pattern
Iterator pattern
 
Singleton design pattern
Singleton design patternSingleton design pattern
Singleton design pattern
 
Oops design pattern intro
Oops design pattern intro Oops design pattern intro
Oops design pattern intro
 
Acem web designing
Acem web designingAcem web designing
Acem web designing
 
Super and final in java
Super and final in javaSuper and final in java
Super and final in java
 
Aravali college of engg. and management
Aravali college of engg. and managementAravali college of engg. and management
Aravali college of engg. and management
 

Recently uploaded

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaasiemaillard
 
Benefits and Challenges of Using Open Educational Resources
Benefits and Challenges of Using Open Educational ResourcesBenefits and Challenges of Using Open Educational Resources
Benefits and Challenges of Using Open Educational Resourcesdimpy50
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaasiemaillard
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...Nguyen Thanh Tu Collection
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxShajedul Islam Pavel
 
Gyanartha SciBizTech Quiz slideshare.pptx
Gyanartha SciBizTech Quiz slideshare.pptxGyanartha SciBizTech Quiz slideshare.pptx
Gyanartha SciBizTech Quiz slideshare.pptxShibin Azad
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxRaedMohamed3
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsCol Mukteshwar Prasad
 
Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.pptBasic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.pptSourabh Kumar
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersPedroFerreira53928
 
Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfVivekanand Anglo Vedic Academy
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxJheel Barad
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfTamralipta Mahavidyalaya
 
Application of Matrices in real life. Presentation on application of matrices
Application of Matrices in real life. Presentation on application of matricesApplication of Matrices in real life. Presentation on application of matrices
Application of Matrices in real life. Presentation on application of matricesRased Khan
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chipsGeoBlogs
 
[GDSC YCCE] Build with AI Online Presentation
[GDSC YCCE] Build with AI Online Presentation[GDSC YCCE] Build with AI Online Presentation
[GDSC YCCE] Build with AI Online PresentationGDSCYCCE
 

Recently uploaded (20)

B.ed spl. HI pdusu exam paper-2023-24.pdf
B.ed spl. HI pdusu exam paper-2023-24.pdfB.ed spl. HI pdusu exam paper-2023-24.pdf
B.ed spl. HI pdusu exam paper-2023-24.pdf
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Benefits and Challenges of Using Open Educational Resources
Benefits and Challenges of Using Open Educational ResourcesBenefits and Challenges of Using Open Educational Resources
Benefits and Challenges of Using Open Educational Resources
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
Gyanartha SciBizTech Quiz slideshare.pptx
Gyanartha SciBizTech Quiz slideshare.pptxGyanartha SciBizTech Quiz slideshare.pptx
Gyanartha SciBizTech Quiz slideshare.pptx
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
 
Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.pptBasic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
 
Operations Management - Book1.p - Dr. Abdulfatah A. Salem
Operations Management - Book1.p  - Dr. Abdulfatah A. SalemOperations Management - Book1.p  - Dr. Abdulfatah A. Salem
Operations Management - Book1.p - Dr. Abdulfatah A. Salem
 
Mattingly "AI & Prompt Design: Limitations and Solutions with LLMs"
Mattingly "AI & Prompt Design: Limitations and Solutions with LLMs"Mattingly "AI & Prompt Design: Limitations and Solutions with LLMs"
Mattingly "AI & Prompt Design: Limitations and Solutions with LLMs"
 
Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdf
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
Application of Matrices in real life. Presentation on application of matrices
Application of Matrices in real life. Presentation on application of matricesApplication of Matrices in real life. Presentation on application of matrices
Application of Matrices in real life. Presentation on application of matrices
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
 
[GDSC YCCE] Build with AI Online Presentation
[GDSC YCCE] Build with AI Online Presentation[GDSC YCCE] Build with AI Online Presentation
[GDSC YCCE] Build with AI Online Presentation
 

Generic programming in java

  • 1. PRESENTED BY: ANSHU SHARMA Object Oriented Using Java - Generic in Java
  • 2. 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 HashSet, ArrayList, HashMap, etc use generics very well. We can use them for any type.
  • 3. GENERIC CLASS  we use <> to specify parameter types in generic class creation. 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'.
  • 4. EXAMPLES: GENERIC CLASS / A Simple Java program to show working of user defined Generic classes // We use < > to specify Parameter type class Test<T> { // An object of type T is declared T obj; Test(T obj) { this.obj = obj; } // constructor public T getObject() { return this.obj; } }
  • 5. EXAMPLE CONTD. class Main { public static void main (String[] args) { // instance of Integer type Test <Integer> iObj = new Test<Integer>(15); System.out.println(iObj.getObject()); // instance of String type Test <String> sObj = new Test<String>("GeeksForGeeks"); System.out.println(sObj.getObject()); } }
  • 6. MULTIPLE TYPE PARAMETERS IN GENERIC CLASSES We can also pass multiple Type parameters in Generic classes. // A Simple Java program to show multiple // type parameters in Java Generics // We use < > to specify Parameter type class Test<T, U> { T obj1; // An object of type T U obj2; // An object of type U // constructor Test(T obj1, U obj2) { this.obj1 = obj1; this.obj2 = obj2; }
  • 7. EXAMPLE CONTD. // To print objects of T and U public void print() { System.out.println(obj1); System.out.println(obj2); } } // Driver class to test above class Main { public static void main (String[] args) { Test <String, Integer> obj = new Test<String, Integer>("GfG", 15); obj.print(); } }
  • 8. 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.
  • 9. EXAMPLE: // A Simple Java program to show working of user defined // Generic functions class Test { // A Generic method example static <T> void genericDisplay (T element) { System.out.println(element.getClass().getName() + " = " + element); }
  • 10. EXAMPLE CONTD. // Driver method public static void main(String[] args) { // Calling generic method with Integer argument genericDisplay(11); // Calling generic method with String argument genericDisplay("GeeksForGeeks"); // Calling generic method with double argument genericDisplay(1.0); } }
  • 11. ADVANTAGES OF JAVA GENERICS 1. Code Reusability  Generics allow us to write code that will work with different types of data. For example, public <T> void genericsMethod(T data) {...} Here, we have created a generics method. This method can be used to perform operations on integer data, string data and so on.
  • 12. ADVANTAGES CONTD. 2. Compile-time Type Checking  The type parameter of generics provides information about the type of data used in the generics code.  Hence, any error can be identified at compile time which is easier to fix than runtime errors. For example,  // without using Generics NormalClass list = new NormalClass(); // calls method of NormalClass list.display("String"); In the above code, we have a normal class. We call the method named display() of this class by passing a string data.  Here, the compiler does not know if the value passed in the argument is correct or not.
  • 13. ADVANTAGES CONTD.  However, let's see what will happen if we use the generics class instead.  // using Generics GenericsClass<Integer> list = new GenericsClass<>(); // calls method of GenericsClass list2.display("String"); In the above code, we have a generics class. Here, the type parameter indicates that the class is working on Integer data. Hence when the string data is passed in argument, the compiler will generate an error.