• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » Java » Classes » Java Generics Tutorial | Java Generics Example
Next →
← Prev

Java Generics Tutorial | Java Generics Example

By Dinesh Thakur

Java Generics are used to enforce type-safety in a program. A type-safe program does not have compilation and run-time errors that can result from using incorrect data types.

We’ll be covering the following topics in this tutorial:

  • What is Generics in Java?
  • What is Use of Generics In Java?

What is Generics in Java?

Generics in Java are similar to templates in C++. Generics is a method that refers to a set of language features linked to the definition and application of Common types and methods. Java Generic methods differ from common data types and methods. Before Generics, we utilized the collection to save any objects i.e., non-generic. Now, Generics induce the Java programmer to store a particular type of object. Generic programming provides the facility to get a set of related methods and announce one method that supports almost any legitimate type.

For example, classes such as HashSet, ArrayList, HashMap, etc. use generics exceptionally well. We can use them for almost any type.

Generics are released in Java 5, and it’s a center for generic programming in the Java programming language. With the support of Generics in Java, you can define a type or method to an object of different, which is utilized for compile-time security checking. Philip Wadler produced Generic Java from the year 1998.

Following is a simple example of Generics in Java:

List < String > list = new ArrayList < String > (); 
list.add("Hello World"); 
Integer in = list.get(0); // (type error) Compile time error 

From the above code, we’ve utilized the Generics while specifying the array list. The type of to be kept in the array listing is merely the String. If we attempt to typecast the data into Integer, it’ll provide a compile-time error. This manner compiler will have the ability to find the mistake in the compile-time on your program and remove the errors associated with invalid types.

Now that you know what Generics in Java, let’s move further and understand why you need Java Generics.

Recall from our earlier discussion that the ArrayList class is used to create an array to store elements. The size of the array changes automatically as elements are added or removed from it. First, we describe a potential problem with using ArrayList without generics. Let us create an array list as shown here:

ArrayList todoList = new ArrayList(); 

Suppose that we would like to use this list to keep track of the weekly chores associated with caring for a pet cat. We can add elements to this list using the add method in ArrayList:

todoList.add("Buy gourmet cat food"); 
todoList.add("See funny cat pictures online"); 
todoList.add("walk the cat"); 

All of the elements added to the list are of type String. If you put the above code in the main method and run it, it would run correctly without any errors. Recall, however, that the add method is defined as shown here:

public boolean add(Object obj) – a method that adds the object obj to the end of the list.

Therefore, you able to add an object of any type (say, Box) to todoList using this method:

todoList.add(new Box()); 

There are two problems with this approach. First, it is odd that a to-do list contains an element that is not of type String. Second-and worse-is that it can cause an error at run time.
Suppose that we would like to display the items in the to-do list. It can do with ArrayList’s get method:

public Object get(int index) – a method that returns the element at the given index.

The element returned is of type Object; thus it must be cast back to its original type. A programmer has written this statement to read the fourth item in todoList, expecting it to be of type String:

String s = (String) todoList.get(3); 
System.out.println(s); 

Running the program now results in the following ClassCastException:

Exception in thread "main" java.lang.ClassCastException: 
genericsandcollections.Box cannot be cast to java.lang.String at genericsandcollections.ListDemo.main(ListDemo.java:14) 

The problem is that the fourth element at index 3 is an instance of Box,and it cannot be cast to type String after it is retrieved using the get method. To prevent this error, we will create a list that can only hold elements of type String. This is done as follows:

ArrayList<String> newList = new ArrayList<String>(); 

Note that we have inserted the type String within angle brackets (<, >). This declaration tells the compiler that newList can only store elements of type String. The advantage of doing this is that if the programmer tried to insert an element of a different type in this list, it would be caught during compilation and thus prevent a run-time error. Generics makes this type of declaration possible. With generics, we can specify the type of elements that will be stored in a particular collection before it is created.

Let us examine the documentation for the ArrayList class with generics. This class is defined to be generic by adding the letter E within angle brackets:

public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess,Cloneable, Serializable { 
     // code .. 
} 

The letter E is a placeholder for the actual element type that will be defined by the programmer. E is known as the type parameter of the ArrayList class. By convention, the type parameter is a single uppercase letter, and is defined to be E for the collection classes. For example, this declares an ArrayList called myPictures that can hold elements of type Picture:

ArrayList<Picture> myPictures; 

The type parameter E is replaced by Picture in ArrayList. Here, Picture is called the type argument to the ArrayList class. When you create myPictures, you must again specify that it store elements of type Picture:

myPictures = new ArrayList<Picture>(); 

The type argument Picture is inserted within angle brackets between ArrayList and the parentheses. Now, myPictures can only hold elements of class Picture (and its subclasses). Note that you cannot create an object of type parameter E because it is not an actual data type.
The type argument can be a class type or interface type, but it cannot be a primitive type. This statement results in a compilation error because it creates an ArrayList with a primitive type argument of int:

ArrayList<int> newList = newArrayList<int>(); // error! 

However, you can use a wrapper class, such as Integer, in place of the primitive type.

Generic methods are declared using type parameters. In ArrayList, methods add and get are defined to be generic using the type parameter E:
public boolean add(E e) – a method that adds the element e to the end of the list.
public E get(int index) – a method that returns the element at the specified index in the list.

The methods add and get have a type parameter E instead of Object. After the ArrayList instance has been created, you can use these methods just as you would use any other method in a class. Note that you do not need to cast the value returned by the get method because it is of the data type represented by E and not Object.
To see how the generic methods are called, let us create todoList using genencs:

ArrayList<String> todoList = newArrayList<String>(); 

This replaces the type parameter E with String in todoList’s methods. Therefore, this call to add works correctly:

todoLisLadd("Buy gounnet cat food"); 

However, the following statement results in a compilation error because add can take only a String argument:

todoList.add(new Box()); // error 

The get method retrieves the objects as type String instead of Object:

String s = todoList.get(2); 

Note that a cast to String is not needed now because the object retrieved is of type String.

What is Use of Generics In Java?

Generics are introduced in Java 5 to Empower types (classes and interfaces) to Function as parameters when defining classes, interfaces, and methods. The Java compiler implements them as a front-end conversion known as the erasure. The uses of generic as follows:

Elimination of Type Casting

Generics offer the type checking at compile time. Finding bugs in compile-time may spare time for debugging java Program since compile-time bugs are a lot simpler to discover and fix. Should you use generics, you don’t need to execute the typecasting explicitly. Java compiler implements significant type checking if you use generics on your code and reveals errors in the event the code violates the type of security. You are thus removing the risk of ClassCastException.

Before use Generics Methods

List list = new ArrayList(); 
list.add("Before use Generics method"); 
String str = (String) list.get(0); 

In the above case we can see typecasting in last line.

After use Generics Methods

List < String> list = new ArrayList <String>(); 
list.add("After use Generics method"); 
String str = list.get(0); 

Stronger type checking at compile-time

They are finding bugs in compile-time May Spare time for debugging java program since compile-time bugs are much easier to Locate And mend. Java compiler applies significant type checking to generic code and problems mistakes when the code violates type security.

Example

List < String> list = new ArrayList <String>(); 
list.add("After use Generics method"); 
String str = list.get(0); 

The compiler is responsible for understanding Generics at compile time. The above code has been checked at compile-time, so it’s ensured that no difficulty will happen at runtime. They are allowing developers to implement generic algorithms.
By using generics, developers may implement generic algorithms that operate on collections of distinct types, may be customized, and are type-safe and easier to read.

Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

Java Tutorials

Java Tutorials

  • Java - Home
  • Java - IDE
  • Java - Features
  • Java - History
  • Java - this Keyword
  • Java - Tokens
  • Java - Jump Statements
  • Java - Control Statements
  • Java - Literals
  • Java - Data Types
  • Java - Type Casting
  • Java - Constant
  • Java - Differences
  • Java - Keyword
  • Java - Static Keyword
  • Java - Variable Scope
  • Java - Identifiers
  • Java - Nested For Loop
  • Java - Vector
  • Java - Type Conversion Vs Casting
  • Java - Access Protection
  • Java - Implicit Type Conversion
  • Java - Type Casting
  • Java - Call by Value Vs Reference
  • Java - Collections
  • Java - Garbage Collection
  • Java - Scanner Class
  • Java - this Keyword
  • Java - Final Keyword
  • Java - Access Modifiers
  • Java - Design Patterns in Java

OOPS Concepts

  • Java - OOPS Concepts
  • Java - Characteristics of OOP
  • Java - OOPS Benefits
  • Java - Procedural Vs OOP's
  • Java - Polymorphism
  • Java - Encapsulation
  • Java - Multithreading
  • Java - Serialization

Java Operator & Types

  • Java - Operator
  • Java - Logical Operators
  • Java - Conditional Operator
  • Java - Assignment Operator
  • Java - Shift Operators
  • Java - Bitwise Complement Operator

Java Constructor & Types

  • Java - Constructor
  • Java - Copy Constructor
  • Java - String Constructors
  • Java - Parameterized Constructor

Java Array

  • Java - Array
  • Java - Accessing Array Elements
  • Java - ArrayList
  • Java - Passing Arrays to Methods
  • Java - Wrapper Class
  • Java - Singleton Class
  • Java - Access Specifiers
  • Java - Substring

Java Inheritance & Interfaces

  • Java - Inheritance
  • Java - Multilevel Inheritance
  • Java - Single Inheritance
  • Java - Abstract Class
  • Java - Abstraction
  • Java - Interfaces
  • Java - Extending Interfaces
  • Java - Method Overriding
  • Java - Method Overloading
  • Java - Super Keyword
  • Java - Multiple Inheritance

Exception Handling Tutorials

  • Java - Exception Handling
  • Java - Exception-Handling Advantages
  • Java - Final, Finally and Finalize

Data Structures

  • Java - Data Structures
  • Java - Bubble Sort

Advance Java

  • Java - Applet Life Cycle
  • Java - Applet Explaination
  • Java - Thread Model
  • Java - RMI Architecture
  • Java - Applet
  • Java - Swing Features
  • Java - Choice and list Control
  • Java - JFrame with Multiple JPanels
  • Java - Java Adapter Classes
  • Java - AWT Vs Swing
  • Java - Checkbox
  • Java - Byte Stream Classes
  • Java - Character Stream Classes
  • Java - Change Color of Applet
  • Java - Passing Parameters
  • Java - Html Applet Tag
  • Java - JComboBox
  • Java - CardLayout
  • Java - Keyboard Events
  • Java - Applet Run From CLI
  • Java - Applet Update Method
  • Java - Applet Display Methods
  • Java - Event Handling
  • Java - Scrollbar
  • Java - JFrame ContentPane Layout
  • Java - Class Rectangle
  • Java - Event Handling Model

Java programs

  • Java - Armstrong Number
  • Java - Program Structure
  • Java - Java Programs Types
  • Java - Font Class
  • Java - repaint()
  • Java - Thread Priority
  • Java - 1D Array
  • Java - 3x3 Matrix
  • Java - drawline()
  • Java - Prime Number Program
  • Java - Copy Data
  • Java - Calculate Area of Rectangle
  • Java - Strong Number Program
  • Java - Swap Elements of an Array
  • Java - Parameterized Constructor
  • Java - ActionListener
  • Java - Print Number
  • Java - Find Average Program
  • Java - Simple and Compound Interest
  • Java - Area of Rectangle
  • Java - Default Constructor Program
  • Java - Single Inheritance Program
  • Java - Array of Objects
  • Java - Passing 2D Array
  • Java - Compute the Bill
  • Java - BufferedReader Example
  • Java - Sum of First N Number
  • Java - Check Number
  • Java - Sum of Two 3x3 Matrices
  • Java - Calculate Circumference
  • Java - Perfect Number Program
  • Java - Factorial Program
  • Java - Reverse a String

Other Links

  • Java - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2023. All Rights Reserved.