Tutorial

Java Generics Explained: Benefits, Examples, and Best Practice

Updated on February 10, 2025
Java Generics Explained: Benefits, Examples, and Best Practice

Introduction

Java Genrics is one of the most important features introduced in Java 5. If you have been working on Java Collections and with version 5 or higher, I am sure that you have used it. Generics in Java with collection classes is very easy but provides many more features than just creating the type of collection. We will try to learn the features of generics in this article.

Generics in Java

Generics was added in Java 5 to provide compile-time type checking and removing risk of ClassCastException that was common while working with collection classes. The whole collection framework was re-written to use generics for type-safety. Let’s see how generics help us using collection classes safely.

List list = new ArrayList();
list.add("abc");
list.add(new Integer(5)); //OK

for(Object obj : list){
 //type casting leading to ClassCastException at runtime
    String str=(String) obj; 
}

Above code compiles fine but throws ClassCastException at runtime because we are trying to cast Object in the list to String whereas one of the element is of type Integer. After Java 5, we use collection classes like below.

List<String> list1 = new ArrayList<String>(); // java 7 ? List<String> list1 = new ArrayList<>(); 
list1.add("abc");
//list1.add(new Integer(5)); //compiler error

for(String str : list1){
     //no type casting needed, avoids ClassCastException
}

Notice that at the time of list creation, we have specified that the type of elements in the list will be String. So if we try to add any other type of object in the list, the program will throw compile-time error. Also notice that in for loop, we don’t need typecasting of the element in the list, hence removing the ClassCastException at runtime.

Java Generic Class

We can define our own classes with generics type. A generic type is a class or interface that is parameterized over types. We use angle brackets (<>) to specify the type parameter. To understand the benefit, let’s say we have a simple class as:

package com.journaldev.generics;

public class GenericsTypeOld {

 private Object t;

 public Object get() {
  return t;
 }

 public void set(Object t) {
  this.t = t;
 }

        public static void main(String args[]){
  GenericsTypeOld type = new GenericsTypeOld();
  type.set("Pankaj"); 
  String str = (String) type.get(); //type casting, error prone and can cause ClassCastException
 }
}

Notice that while using this class, we have to use type casting and it can produce ClassCastException at runtime. Now we will use java generic class to rewrite the same class as shown below.

package com.journaldev.generics;

public class GenericsType<T> {

 private T t;
 
 public T get(){
  return this.t;
 }
 
 public void set(T t1){
  this.t=t1;
 }
 
 public static void main(String args[]){
  GenericsType<String> type = new GenericsType<>();
  type.set("Pankaj"); //valid
  
  GenericsType type1 = new GenericsType(); //raw type
  type1.set("Pankaj"); //valid
  type1.set(10); //valid and autoboxing support
 }
}

Notice the use of GenericsType class in the main method. We don’t need to do type-casting, and we can remove ClassCastException error at runtime. If we don’t provide the type at the creation time, the compiler will warn that “GenericsType is a raw type. References to generic type GenericsType<T> should be parameterized”. When we don’t provide the type, the type becomes Object, and hence, it allows both String and Integer objects. But, we should always try to avoid this because we will have to use type casting while working on the raw type, which can produce runtime errors.

Tip: We can use @SuppressWarnings("rawtypes") annotation to suppress the compiler warning, check out java annotations tutorial.

Also, please note that it supports java autoboxing.

Java Generic Interface

Comparable interface is a great example of Generics in interfaces and it’s written as:

package java.lang;
import java.util.*;

public interface Comparable<T> {
    public int compareTo(T o);
}

In similar way, we can create generic interfaces in java. We can also have multiple type parameters as in Map interface. Again we can provide parameterized value to a parameterized type also, for example new HashMap<String, List<String>>(); is valid.

Java Generic Type

Java Generic Type Naming convention helps us understanding code easily and having a naming convention is one of the best practices of Java programming language. So generics also comes with its own naming conventions. Usually, type parameter names are single, uppercase letters to make it easily distinguishable from java variables. The most commonly used type parameter names are:

  • E - Element (used extensively by the Java Collections Framework, for example ArrayList, Set etc.)
  • K - Key (Used in Map)
  • N - Number
  • T - Type
  • V - Value (Used in Map)
  • S,U,V etc. - 2nd, 3rd, 4th types

Java Generic Method

Sometimes we don’t want the whole class to be parameterized, in that case, we can create java generics method. Since the constructor is a special kind of method, we can use generics type in constructors too. Here is a class showing an example of a java generic method.

package com.journaldev.generics;

public class GenericsMethods {

 //Java Generic Method
 public static <T> boolean isEqual(GenericsType<T> g1, GenericsType<T> g2){
  return g1.get().equals(g2.get());
 }
 
 public static void main(String args[]){
  GenericsType<String> g1 = new GenericsType<>();
  g1.set("Pankaj");
  
  GenericsType<String> g2 = new GenericsType<>();
  g2.set("Pankaj");
  
  boolean isEqual = GenericsMethods.<String>isEqual(g1, g2);
  //above statement can be written simply as
  isEqual = GenericsMethods.isEqual(g1, g2);
  //This feature, known as type inference, allows you to invoke a generic method as an ordinary method, without specifying a type between angle brackets.
  //Compiler will infer the type that is needed
 }
}

Notice the isEqual method signature showing syntax to use generics type in methods. Also, notice how to use these methods in our java program. We can specify type while calling these methods or we can invoke them like a normal method. Java compiler is smart enough to determine the type of variable to be used, this facility is called type inference.

Java Generics Bounded Type Parameters

Suppose we want to restrict the type of objects that can be used in the parameterized type, for example in a method that compares two objects and we want to make sure that the accepted objects are Comparables. To declare a bounded type parameter, list the type parameter’s name, followed by the extends keyword, followed by its upper bound, similar like below method.

public static <T extends Comparable<T>> int compare(T t1, T t2){
  return t1.compareTo(t2);
 }

The invocation of these methods is similar to unbounded method except that if we will try to use any class that is not Comparable, it will throw compile-time error. Bounded type parameters can be used with methods as well as classes and interfaces. Java Generics supports multiple bounds also, i.e <T extends A & B & C>. In this case, A can be an interface or class. If A is class then B and C should be an interface. We can’t have more than one class in multiple bounds.

Java Generics and Inheritance

We know that Java inheritance allows us to assign a variable A to another variable B if A is subclass of B. So we might think that any generic type of A can be assigned to generic type of B, but it’s not the case. Let’s see this with a simple program.

package com.journaldev.generics;

public class GenericsInheritance {

 public static void main(String[] args) {
  String str = "abc";
  Object obj = new Object();
  obj=str; // works because String is-a Object, inheritance in java
  
  MyClass<String> myClass1 = new MyClass<String>();
  MyClass<Object> myClass2 = new MyClass<Object>();
  //myClass2=myClass1; // compilation error since MyClass<String> is not a MyClass<Object>
  obj = myClass1; // MyClass<T> parent is Object
 }
 
 public static class MyClass<T>{}

}

We are not allowed to assign MyClass<String> variable to MyClass<Object> variable because they are not related, in fact MyClass<T> parent is Object.

Java Generic Classes and Subtyping

We can subtype a generic class or interface by extending or implementing it. The relationship between the type parameters of one class or interface and the type parameters of another are determined by the extends and implements clauses. For example, ArrayList<E> implements List<E> that extends Collection<E>, so ArrayList<String> is a subtype of List<String> and List<String> is subtype of Collection<String>. The subtyping relationship is preserved as long as we don’t change the type argument, below shows an example of multiple type parameters.

interface MyList<E,T> extends List<E>{
}

The subtypes of List<String> can be MyList<String,Object>,MyList<String,Integer> and so on.

Java Generics Wildcards

Question mark (?) is the wildcard in generics and represent an unknown type. The wildcard can be used as the type of a parameter, field, or local variable and sometimes as a return type. We can’t use wildcards while invoking a generic method or instantiating a generic class. In the following sections, we will learn about upper bounded wildcards, lower bounded wildcards, and wildcard capture.

Java Generics Upper Bounded Wildcard

Upper bounded wildcards are used to relax the restriction on the type of variable in a method. Suppose we want to write a method that will return the sum of numbers in the list, so our implementation will be something like this.

public static double sum(List<Number> list){
  double sum = 0;
  for(Number n : list){
   sum += n.doubleValue();
  }
  return sum;
 }

Now the problem with above implementation is that it won’t work with List of Integers or Doubles because we know that List<Integer> and List<Double> are not related, this is when an upper bounded wildcard is helpful. We use generics wildcard with extends keyword and the upper bound class or interface that will allow us to pass argument of upper bound or it’s subclasses types. The above implementation can be modified like the below program.

package com.journaldev.generics;

import java.util.ArrayList;
import java.util.List;

public class GenericsWildcards {

 public static void main(String[] args) {
  List<Integer> ints = new ArrayList<>();
  ints.add(3); ints.add(5); ints.add(10);
  double sum = sum(ints);
  System.out.println("Sum of ints="+sum);
 }

 public static double sum(List<? extends Number> list){
  double sum = 0;
  for(Number n : list){
   sum += n.doubleValue();
  }
  return sum;
 }
}

It’s similar like writing our code in terms of interface, in the above method we can use all the methods of upper bound class Number. Note that with upper bounded list, we are not allowed to add any object to the list except null. If we will try to add an element to the list inside the sum method, the program won’t compile.

Java Generics Unbounded Wildcard

Sometimes we have a situation where we want our generic method to be working with all types, in this case, an unbounded wildcard can be used. Its same as using <? extends Object>.

public static void printData(List<?> list){
  for(Object obj : list){
   System.out.print(obj + "::");
  }
 }

We can provide List<String> or List<Integer> or any other type of Object list argument to the printData method. Similar to upper bound list, we are not allowed to add anything to the list.

Java Generics Lower bounded Wildcard

Suppose we want to add Integers to a list of integers in a method, we can keep the argument type as List<Integer> but it will be tied up with Integers whereas List<Number> and List<Object> can also hold integers, so we can use a lower bound wildcard to achieve this. We use generics wildcard (?) with super keyword and lower bound class to achieve this. We can pass lower bound or any supertype of lower bound as an argument, in this case, java compiler allows to add lower bound object types to the list.

public static void addIntegers(List<? super Integer> list){
  list.add(new Integer(50));
 }

Subtyping using Generics Wildcard

List<? extends Integer> intList = new ArrayList<>();
List<? extends Number>  numList = intList;  // OK. List<? extends Integer> is a subtype of List<? extends Number>

Java Generics Type Erasure

Generics in Java was added to provide type-checking at compile time and it has no use at run time, so java compiler uses type erasure feature to remove all the generics type checking code in byte code and insert type-casting if necessary. Type erasure ensures that no new classes are created for parameterized types; consequently, generics incur no runtime overhead. For example, if we have a generic class like below;

public class Test<T extends Comparable<T>> {

    private T data;
    private Test<T> next;

    public Test(T d, Test<T> n) {
        this.data = d;
        this.next = n;
    }

    public T getData() { return this.data; }
}

The Java compiler replaces the bounded type parameter T with the first bound interface, Comparable, as below code:

public class Test {

    private Comparable data;
    private Test next;

    public Node(Comparable d, Test n) {
        this.data = d;
        this.next = n;
    }

    public Comparable getData() { return data; }
}

Common Mistakes with Generics

Even experienced developers make mistakes while using generics. Here are some common pitfalls:

1. Using Raw Types

Using raw types defeats the purpose of generics.

List list = new ArrayList(); // Avoid this
list.add("Hello");
list.add(123); // No type safety

Instead,use parameterized types instead:

List<String> list = new ArrayList<>();

2. Mixing Generics with Legacy Code

Avoid mixing generic and non-generic code as it can lead to unexpected issues.

3. Incorrect Use of Wildcards

Example:

public void addItem(List<? extends Number> list) {
    // list.add(10); // Compilation error
}

Instead, use ? super T if modification is required:

public void addItem(List<? super Integer> list) {
    list.add(10);
}

4. Overusing Wildcards

While wildcards improve flexibility, using them unnecessarily can make the code harder to understand.

FAQs

1. What is Generics in Java?

Generics allow you to define a class or method with a type parameter. For example:

class Box<T> {
    private T value;

    public void setValue(T value) {
        this.value = value;
    }

    public T getValue() {
        return value;
    }
}

Here, T is a type parameter that can be replaced with any concrete type at runtime.

2. Why Use Generics in Java?

  • Type Safety: Prevents ClassCastException at runtime.

  • Code Reusability: Enables writing a single class or method that works with different types.

  • Improved Readability: Avoids excessive casting and makes code cleaner.

For a deeper understanding of Java fundamentals, check out the Java EE tutorial.

3. Can you give an example of a generic method?

A generic method can take a type parameter:

public static <T> void printArray(T[] elements) {
    for (T element : elements) {
        System.out.println(element);
    }
}

This method prints elements of an array of any type.

4. What Does List<?> in Java Mean?

The ? is a wildcard in Java generics. It represents an unknown type. For example:

List<?> list = new ArrayList<String>();

This means the list can hold any type, but its exact type is unknown at compile time. You can learn more about comparators in java in this tutorial on Comparable and Comparator in Java Example.

5. Understanding Wildcards in Java Generics

There are two main types of wildcards:

  1. ? extends T: Represents an unknown type that is a subclass of T.

       public void processElements(List<? extends Number> numbers) {
     for (Number num : numbers) {
         System.out.println(num);
       }
    }
    
  2. ? super T: Represents an unknown type that is a superclass of T.

    public void addNumbers(List<? super Integer> numbers) {
     numbers.add(42);
    }
    

Wildcards provide flexibility while maintaining type safety.

6. Can I Create an Array of Generics in Java?

No, Java does not allow generic array creation due to type erasure. This would lead to ClassCastException error. Instead, you can use a List<T>:

List<String> list = new ArrayList<>();

For more details on Java type annotations, refer to our tutorial on Java Annotations.

7. Why Are Generics Safe in Java?

Generics enforce type checking at compile time, reducing the likelihood of runtime errors. They eliminate the need for explicit casting and make code more readable and maintainable.

8. What Are the Rules for Generics in Java?

  1. Cannot create instances of a generic type.

    class Box<T> {
     // T obj = new T(); // Illegal
    }
    
  2. No static members of generic type.

    class Box<T> {
       // static T instance; // Illegal
    }
    
  3. Cannot use primitives as type parameters (use wrapper classes instead).

    // List<int> list = new ArrayList<>(); // Illegal
    List<Integer> list = new ArrayList<>();
    

Conclusion

Java Generics offers a robust mechanism to ensure type safety and promote code reusability. By grasping the nuances of wildcards, avoiding common pitfalls, and adhering to best practices, you can craft more maintainable, efficient, and error-free Java applications.

For more Java-related topics, you can refer to our tutorials on Java EE Tutorials and Comparable and Comparator examples.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the author(s)

Category:
Tutorial
Tags:

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
July 27, 2013

Hi Thanks for the very informative article… However could you please explain the difference between: public static void printData(List list){ for(Object obj : list){ System.out.print(obj + “::”); } } And public static void printData(List list){ for(Object obj : list){ System.out.print(obj + “::”); } }

- Avinash

JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
July 28, 2013

I think both the methods are same, guessing some typo.

- Pankaj

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    August 3, 2013

    hi! i want to devlop my future in java ,i want to learn it i am beginner no from whrere do i start please help me!!!

    - Sarah

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    August 3, 2013

    Start with core java and then move on to J2EE technologies.

    - Pankaj

      JournalDev
      DigitalOcean Employee
      DigitalOcean Employee badge
      October 26, 2013

      hi, I want to define my arraylist which allows to insert only class A,B,C .How to achieve in genrics

      - sriram

      JournalDev
      DigitalOcean Employee
      DigitalOcean Employee badge
      October 26, 2013

      The classes should have something in common, if nothing specified then Object is the common parent class that we can use to define the type of list.

      - Pankaj

        JournalDev
        DigitalOcean Employee
        DigitalOcean Employee badge
        November 12, 2013

        List str=null; str.add(“varun”); str.add(new Integer(10), str); im getting compile time error…plz give me solution .how to add string and integer both in arrylist ???

        - varun

        JournalDev
        DigitalOcean Employee
        DigitalOcean Employee badge
        November 15, 2013

        Create list of Object and then you can add any type of Object. In above code snippet str is not initialized.

        - Pankaj

          JournalDev
          DigitalOcean Employee
          DigitalOcean Employee badge
          November 26, 2013

          i want usecase digram for generics vs collection, its for my job,please give quickly

          - RANJITH KUMAR

            JournalDev
            DigitalOcean Employee
            DigitalOcean Employee badge
            January 11, 2014

            I want to create xlsx file from a ArrayList. TableBean means a class with some fields and its getter and setter. I have three (3) list 1. ArrayList CompanyBeanList (CompanyBean has 4 fileds with getter - setter) 2.ArrayList EmployeeBeanList (EmployeeBean has 10 fileds with getter - setter) 3.ArrayList ClientBeanList (ClientBeanBean has 8 fileds with getter - setter) I want to call exportToExcel method as exportToExcel(“D:\Back Up\PROJECT\”,CompanyBeanList ,“CompanyBean”) and exportToExcel(“D:\Back Up\PROJECT\”,EmployeeBeanList ,“EmployeeBean”) and exportToExcel(“D:\Back Up\PROJECT\”,ClientBeanList ,“ClientBean”) exportToExcel is a method which create an .xlsx file in the specified path with data from the beanList (2 nd parameter) But problem is i can not pass these 3 different type of list in exportToExcel. Error says The method exportToExcel(String, ArrayList, String) in the type Utility is not applicable for the arguments (String, List, String) (same for other two class) I do not want to write BeanClass name in parameter. it should be parametrized. public static void exportToExcel(String outputPath , ArrayList beanClassName,String sheetName) { try{ Map data = new TreeMap(); XSSFWorkbook workbook = new XSSFWorkbook(); XSSFSheet sheet = workbook.createSheet(sheetName); Object[] colName =new Object[20]; if(beanClassName.size()>0) { Field[] field=beanClassName.get(0).getClass().getDeclaredFields(); for(int i=0;i<field.length;i++) { colName[i]=field[i].getName().toUpperCase(); } data.put(“1”, colName); for(int i=0;i<beanClassName.size();i++) { Object[] colValue =new Object[20]; Field[] field1=beanClassName.get(0).getClass().getDeclaredFields(); for(int f=0;i<field1.length;i++) { Method method=null; try { method=beanClassName.get(i).getClass().getMethod(“get”+field1[f].getName().toUpperCase(), null); }catch (IllegalArgumentException e) { log.error("User: “+getUserName()+” : "+“exportToExcel…1…”+e.getMessage()); e.printStackTrace(); } catch (Exception e) { log.error(“User: “+getUserName()+” : “+“exportToExcel…2…”+e.getMessage()); e.printStackTrace(); } colValue[f]=method.invoke(beanClassName.get(i), null); } data.put((i+2)+””, colValue); } Set keyset = data.keySet(); int rownum = 0; for (String key : keyset) { Row row = sheet.createRow(rownum++); Object [] objArr = data.get(key); int cellnum = 0; for (Object obj : objArr) { Cell cell = row.createCell(cellnum++); if(obj instanceof String) cell.setCellValue((String)obj); else if(obj instanceof Integer) cell.setCellValue((Integer)obj); else if(obj instanceof Double) cell.setCellValue((Double)obj); else if(obj instanceof Float) cell.setCellValue((Float)obj); else if(obj instanceof Number) cell.setCellValue((Double)obj); } } try { //Write the workbook in file system FileOutputStream out = new FileOutputStream(new File(outputPath+File.separator+“OrderCounts.xlsx”)); workbook.write(out); out.close(); } catch (Exception e) { e.printStackTrace(); } } else { return; } }catch (Exception e) { log.error("User: “+getUserName()+” : "+“exportToExcel…”+e.getMessage()); e.printStackTrace(); } } I only want to call exportToExcel(“D:\Back Up\PROJECT\”,CompanyBeanList ,“CompanyBean”) and exportToExcel(“D:\Back Up\PROJECT\”,EmployeeBeanList ,“EmployeeBean”) and exportToExcel(“D:\Back Up\PROJECT\”,ClientBeanList ,“ClientBean”) and it will create .xlsx file in the path. That is my main aim. Is it possible.?? Which modification is necessary in exportToExcel method. ??? If solution is known to you please write / modify exportToExcel method as you wish and reply me over mail or post in this site. It is very urgent to me. I am waiting for your reply. I hope i’ll get a suitable solution from you.I believe on you.

            - Dev

            JournalDev
            DigitalOcean Employee
            DigitalOcean Employee badge
            January 12, 2014

            its simple, the error is coming because your method argument is expecting ArrayList but you might be creating at client side as List myList = new ArrayList();. All you need to do is change the method argument type to List and you should be good to go.

            - Pankaj

              JournalDev
              DigitalOcean Employee
              DigitalOcean Employee badge
              January 12, 2014

              Thanks for this great article … it was very helpful for me

              - Mohsen

                JournalDev
                DigitalOcean Employee
                DigitalOcean Employee badge
                January 13, 2014

                Thanks a lot. I made this change in my code and this is working correctly.

                - Dev

                  JournalDev
                  DigitalOcean Employee
                  DigitalOcean Employee badge
                  February 4, 2014

                  What are the drawbacks of Generics?

                  - Siddu

                    JournalDev
                    DigitalOcean Employee
                    DigitalOcean Employee badge
                    February 4, 2014

                    Hi Respected Sir My expectation is growing high after getting answer from you and solving my previous problem. I face another problem while using java generic in my program. The problem is i have to GROUP BY a ganeric list based on one or two field. Suppose i have a POJO class named StationInformation with CODE,STATION,NAME,ADDRESS i have created a list ArrayList StationInformationList= new ArrayList and populate this list with station data. After some operation with this list object i have to GROUP BY this list based on CODE field and after that CODE and STATION field. This GROUP BY should be same as GROUP BY in database. There is no way to go database from here because data is coming from api. This is very essential to me and i have to resolve it as soon as possible. I hope i’ll get another feedback from you with solution my problem. This GROUP BY is vary urgent and please give me a solution. I am waiting for your answer/reply. Please please sir give me a solution. If possible please give me code example.

                    - Dev

                      JournalDev
                      DigitalOcean Employee
                      DigitalOcean Employee badge
                      April 18, 2014

                      Dear sir i have a query regarding generics plz solve this… I have a generic super class Employee and this super class have two sub class Admin and Finance…so tell me can i use Admin data in Finance sub class and Finance data in Admin sub class.

                      - Ashish

                      JournalDev
                      DigitalOcean Employee
                      DigitalOcean Employee badge
                      April 18, 2014

                      This is not generics, its inheritance. Think of it like Integer and String class, both have super class as Object. Can you assign Integer to String and vice versa, NO.

                      - Pankaj

                      JournalDev
                      DigitalOcean Employee
                      DigitalOcean Employee badge
                      January 13, 2020

                      @Pankaj mejor explicado no podia ser, con esa frase entendi la diferencia para entender super class de generic class, en mi caso trabaje con clases como GenericDAO que principalmente contenia metodos CRUD save(T), update(T), get(T), delete(T) o algo asi, quiero decir que los metodos genericos eran llamados en clases super DAO como PersonDAO, ProductDAO que no implementaban esos metodos sino solo call generic method (void save(T object), ese es mi concepto, pero que mas podrias tu decir sobre esto.

                      - jesus segovia

                        JournalDev
                        DigitalOcean Employee
                        DigitalOcean Employee badge
                        April 22, 2014

                        Tnx man. really great explanation for beginners. Much appreciated.

                        - smokes

                          JournalDev
                          DigitalOcean Employee
                          DigitalOcean Employee badge
                          April 25, 2014

                          How can I use binary operators in generics ???

                          - Mayur Bote

                            JournalDev
                            DigitalOcean Employee
                            DigitalOcean Employee badge
                            April 25, 2014

                            Awesome work! Really informative

                            - Anup

                            JournalDev
                            DigitalOcean Employee
                            DigitalOcean Employee badge
                            April 26, 2014

                            Thank you very much, really well explained.

                            - Pablo

                              JournalDev
                              DigitalOcean Employee
                              DigitalOcean Employee badge
                              May 27, 2014

                              Awesome man :)

                              - laukendra

                                JournalDev
                                DigitalOcean Employee
                                DigitalOcean Employee badge
                                May 28, 2014

                                Nice explanation, easy to understand Thanks for the post.

                                - viswam

                                  JournalDev
                                  DigitalOcean Employee
                                  DigitalOcean Employee badge
                                  June 10, 2014

                                  Could you please explain what is that which can be done in a generic class but not in a normal java class without generics?

                                  - Amishi Shah

                                  JournalDev
                                  DigitalOcean Employee
                                  DigitalOcean Employee badge
                                  June 14, 2014

                                  Firstly u need to understand that GENERIC in java represents a General Model( be it a method or a class). Secondly, by creating generic class u r creating a General Class which can operate on any type of Object. Thirdly, without using generics u need to create separate class for supporting various types of objects. thus use of generic class helps u in reusability of code i.e less LOC & Compile time Type Checking. thanks Finishing School

                                  - Finishing School

                                  JournalDev
                                  DigitalOcean Employee
                                  DigitalOcean Employee badge
                                  June 18, 2014

                                  Thanks a lot.

                                  - Amishi Shah

                                    JournalDev
                                    DigitalOcean Employee
                                    DigitalOcean Employee badge
                                    July 21, 2014

                                    Hi Pankaj, Your explanations and sample programs are very easy to understand. I have a query regarding generics. A sample progam: package main.test; import java.util.ArrayList; import java.util.List; public class MainClass { /** * @param args */ public static void main(String[] args) { List list = new ArrayList(); list.add(8); list.add(7); printList1(list); printList2(list); printList3(list); printList4(list); } /** * Using Generics Bounded Type Parameters * @param list */ public static void printList1(List list){ System.out.println(list); } /** * Using Generics Upper Bounded Wildcard * @param list */ public static void printList2(List list){ System.out.println(list); } /** * Using Simple Generics Method * @param list */ public static void printList3(List list){ System.out.println(list); } /** * Using Generics Unbounded Wildcard * @param list */ public static void printList4(List list){ System.out.println(list); } } There are 4 methods printing a list: printList1: Using Generics Bounded Type Parameters printList2: Using Generics Upper Bounded Wildcard printList3: Using Simple Generics Method printList4: Using Generics Unbounded Wildcard I have two questions: 1) When to use Generics Upper Bounded Wildcard? Because the same thing can be done using Generics Bounded Type Parameters?(methods: printList1 and printList2) I mean how to decide which one to use between these two? 2) Similar Question regarding method: printList3 and printList4.When to use the Generics Unbounded Wildcard?Because the same thing can be done using Using Simple Generics Method.

                                    - SUDIP GHOSH

                                    JournalDev
                                    DigitalOcean Employee
                                    DigitalOcean Employee badge
                                    July 21, 2014

                                    The generics syntax(angel bracket) are not coming while I’m pasting it. :(

                                    - SUDIP GHOSH

                                      JournalDev
                                      DigitalOcean Employee
                                      DigitalOcean Employee badge
                                      July 21, 2014

                                      Generic Upper/Lower Bounded Wildcard and Bounded Type parameters are similar, however bounded type parameters help us in writing generic algorithms, for example in above code, we are using bounded type parameters with Comparable.

                                      - Pankaj

                                        JournalDev
                                        DigitalOcean Employee
                                        DigitalOcean Employee badge
                                        August 1, 2014

                                        Excellent article. I am wondering for simple cases, is there a reason to use Wildcard instead of generics? e.g. The printData method in the “Generics Unbounded Wildcard” could have been written like that? public static void printData(List list) { for(T obj : list) { System.out.print(obj + “::”); } } And we don’t have the limitations of wildcard either (add method not allowed). So, is there any benefit of using wildcard in such cases?

                                        - Manish Verma

                                          JournalDev
                                          DigitalOcean Employee
                                          DigitalOcean Employee badge
                                          August 1, 2014

                                          I was looking for this: Thanks :)

                                          - Abbas

                                            JournalDev
                                            DigitalOcean Employee
                                            DigitalOcean Employee badge
                                            September 4, 2014

                                            great explanation!!!

                                            - sony

                                              JournalDev
                                              DigitalOcean Employee
                                              DigitalOcean Employee badge
                                              September 13, 2014

                                              Very good explanation… Thanks

                                              - Pramod Bablad

                                                JournalDev
                                                DigitalOcean Employee
                                                DigitalOcean Employee badge
                                                September 30, 2014

                                                good explanation

                                                - anonymous

                                                  JournalDev
                                                  DigitalOcean Employee
                                                  DigitalOcean Employee badge
                                                  October 2, 2014

                                                  How converting int to String is autoboxing in the Class generic example given above. type1.set(10); //valid and autoboxing support

                                                  - Satpal Singh

                                                    JournalDev
                                                    DigitalOcean Employee
                                                    DigitalOcean Employee badge
                                                    April 16, 2015

                                                    Sir as I was going through your tutorial. I got stuck at a point public Object get() { return t; } public void set(Object t) { this.t = t; As I have seen in getter(), setter() methord, we write in such a way… public Object getT() { return t; } public void setT(Object t) { this.t = t; What you have done is OK for 1 variable???

                                                    - Abhinav Joshi

                                                    JournalDev
                                                    DigitalOcean Employee
                                                    DigitalOcean Employee badge
                                                    May 27, 2015

                                                    public Object getT() { return t; } public void setT(Object t) { this.t = t; Should be … public T getT() { return t; } public void setT(T t) { this.t = t; This allows any type. If you do it how you had it originally, you’re just making 2 Object type getters/setters with different names.

                                                    - Corey

                                                      JournalDev
                                                      DigitalOcean Employee
                                                      DigitalOcean Employee badge
                                                      March 15, 2016

                                                      really helpfull thanks

                                                      - sairam

                                                        JournalDev
                                                        DigitalOcean Employee
                                                        DigitalOcean Employee badge
                                                        April 14, 2016

                                                        Hi, I have a question what it exactly means?

                                                        - Rashmi

                                                          JournalDev
                                                          DigitalOcean Employee
                                                          DigitalOcean Employee badge
                                                          April 26, 2016

                                                          Hi… Have a doubt. Could you please help? Three classes are there. A,B & C. Both B & C extends A. So in generics how can I make the class C not to be included. It should allow only B Thanks, Shashi

                                                          - Shashikumar

                                                            JournalDev
                                                            DigitalOcean Employee
                                                            DigitalOcean Employee badge
                                                            May 5, 2016

                                                            Thanks for the nice article. I couldnt understand Raw & Unbounded generics. What is the difference in below? Set setOfRawType = new HashSet(); setOfRawType = new HashSet(); Set setOfUnknownType = new HashSet(); setOfUnknownType = new HashSet(); Please explain its very urgent. TIA!

                                                            - Shafali

                                                            JournalDev
                                                            DigitalOcean Employee
                                                            DigitalOcean Employee badge
                                                            July 9, 2016

                                                            1. a raw type (Set) treats the type as if it had no generic type information at all. Note the subtle effect that not only will the type argument T be ignored, but also all other type arguments that methods of that type might have. You can add any value to it and it will always return Object. 2. Set is a Set that accepts all Object objects (i.e. all objects) and will return objects of type Object. 3. Set is a Set that accepts all objects of some specific, but unknown type and will return objects of that type. Since nothing is known about this type, you can’t add anything to that set (except for null) and the only thing that you know about the values it returns is that they are some sub-type of Object. https://stackoverflow.com/a/7360664

                                                            - GOPINATH M B

                                                              JournalDev
                                                              DigitalOcean Employee
                                                              DigitalOcean Employee badge
                                                              December 16, 2016

                                                              public class GenTest { T t; public void setType(T t) { this.t=t; } public T getType() { return t; } public static void main(String[] arg) { GenTest gt=new GenTest(); Kamala k=new Kamala(); gt.setType(k); //Object o=gt.getType(); System.out.println(gt.getType().aboutu()); } } //kamala class public class Kamala { public void aboutu() { System.out.println(“I am kamala ! i am helping class to check generic concept”); } } Sir above program i am getting error: ’ void’ type not allowed here

                                                              - swathi

                                                              JournalDev
                                                              DigitalOcean Employee
                                                              DigitalOcean Employee badge
                                                              December 18, 2016

                                                              1. First of all while using T t in GenTest you need to declare T as type parameter as class GenTest. 2. While instantiating the generic class you must have used GenTest gt = new GenTest(); 3. calling aboutu in sysout - no need to use System.out.println(gt.getType().aboutu()); use gt.getType().aboutu() as method return type is void. Hope this solves your problem and I recommend you to study the generics post thoroughly.

                                                              - shubham

                                                                JournalDev
                                                                DigitalOcean Employee
                                                                DigitalOcean Employee badge
                                                                February 15, 2017

                                                                An example for Java Generic Classes and Subtyping . interface Parent { } interface Child extends Parent { } public class Subtyping implements Child { T tobj; U uobj; public Subtyping(T t, U u) { tobj = t; uobj = u; } public static void main(String[] args) { Parent obj = new Subtyping(4, “raj”); Parent obj1 = new Subtyping(4, 40.0); /* * The subtypes of Parent can be * Subtyping , Subtyping and so on. * but not Subtyping * * this statement will give error * Parent obj2 = new Subtyping( “raj”,4); */ System.out.println(obj); System.out.println(obj1); } @Override public String toString() { return " t= " + tobj + " " + uobj; } }

                                                                - Raj Gopal Bhallamudi

                                                                  JournalDev
                                                                  DigitalOcean Employee
                                                                  DigitalOcean Employee badge
                                                                  February 15, 2017

                                                                  interface Parent { } interface Child extends Parent { } public class Subtyping implements Child { T tobj; U uobj; public Subtyping(T t, U u) { tobj = t; uobj = u; } public static void main(String[] args) { Parent obj = new Subtyping(4, “raj”); Parent obj1 = new Subtyping(4, 40.0); /* * The subtypes of Parent can be * Subtyping , Subtyping and so on. * but not Subtyping * * this statement will give error * Parent obj2 = new Subtyping( “raj”,4); */ System.out.println(obj); System.out.println(obj1); } @Override public String toString() { return " t= " + tobj + " " + uobj; } }

                                                                  - Raj Gopal Bhallamudi

                                                                    JournalDev
                                                                    DigitalOcean Employee
                                                                    DigitalOcean Employee badge
                                                                    April 17, 2017

                                                                    sir plz muje bato ki generics methode me hum 2 object ko jo generics h ko “+” operant se add kyo nahi kr pa rahe.

                                                                    - Yash Rathore

                                                                      JournalDev
                                                                      DigitalOcean Employee
                                                                      DigitalOcean Employee badge
                                                                      September 28, 2017

                                                                      Generics forces the java programmer to store specific type of objects. The language is very easy to understand. Thanks for sharing.

                                                                      - priya

                                                                        JournalDev
                                                                        DigitalOcean Employee
                                                                        DigitalOcean Employee badge
                                                                        March 31, 2018

                                                                        Hi Pankaj, quite amid follower of your tutorials. Short but well explained topics. However, felt some topics are missing in this article. Like PECS of generics, how shall we use them. or maybe it’s me only who is finding it difficult.

                                                                        - Gaurav

                                                                          JournalDev
                                                                          DigitalOcean Employee
                                                                          DigitalOcean Employee badge
                                                                          May 11, 2018

                                                                          public class GenericsTypeOld { private Object t; public Object get() { return t; } public void set(Object t) { this.t = t; } public static void main(String args[]){ GenericsTypeOld type = new GenericsTypeOld(); type.set(“Pankaj”); 112 //String str = (String) type.get(); //type casting, error prone and can cause ClassCastException } I think line number 112 is not giving erroe

                                                                          - ranjit kumar yadav

                                                                            JournalDev
                                                                            DigitalOcean Employee
                                                                            DigitalOcean Employee badge
                                                                            March 15, 2019

                                                                            Lost from “Java Generic Method”…

                                                                            - Roberto

                                                                              JournalDev
                                                                              DigitalOcean Employee
                                                                              DigitalOcean Employee badge
                                                                              December 16, 2019

                                                                              What is Generic class???

                                                                              - Shokhruhhan

                                                                                JournalDev
                                                                                DigitalOcean Employee
                                                                                DigitalOcean Employee badge
                                                                                February 20, 2020

                                                                                Can someone help me understand this code. It is from the 2014 AP exam and I can’t get it to run right, or make sense to me! How can you both “add” and “remove” from a list in the same statement? The code is below: import java.util.ArrayList; import java.util.Arrays; import java.util.List; class Main { public static void main(String[] args) { List animals = Arrays.asList(“bear”, “zebra”, “bass”, “cat”, “koala”, “baboon”); for (int k = animals.size()-1; k > 0; k–) { if (animals.get(k).substring(0, 1).equals(“b”)) { animals.add(animals.size() - k, animals.remove(k)); } } System.out.println("list: " + animals); } }

                                                                                - Wil Johnson

                                                                                  JournalDev
                                                                                  DigitalOcean Employee
                                                                                  DigitalOcean Employee badge
                                                                                  May 16, 2020

                                                                                  Hi Pankaj, I need some help regarding creating a List of generic type which can accept objects of different classes. for eg: List genericList = new ArrayList(); Card card = new Card(); Card extraCard = new ExtraCard(); Card imageCard = new ImageCard(); genericList.add(card); genericList.add(extraCard); genericList.add(imageCard); How can i achieve this. Please help

                                                                                  - Nitin Goyal

                                                                                    Try DigitalOcean for free

                                                                                    Click below to sign up and get $200 of credit to try our products over 60 days!

                                                                                    Sign up

                                                                                    Join the Tech Talk
                                                                                    Success! Thank you! Please check your email for further details.

                                                                                    Please complete your information!

                                                                                    Become a contributor for community

                                                                                    Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

                                                                                    DigitalOcean Documentation

                                                                                    Full documentation for every DigitalOcean product.

                                                                                    Resources for startups and SMBs

                                                                                    The Wave has everything you need to know about building a business, from raising funding to marketing your product.

                                                                                    Get our newsletter

                                                                                    Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

                                                                                    New accounts only. By submitting your email you agree to our Privacy Policy

                                                                                    The developer cloud

                                                                                    Scale up as you grow — whether you're running one virtual machine or ten thousand.

                                                                                    Get started for free

                                                                                    Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

                                                                                    *This promotional offer applies to new accounts only.