Generics: An introduction to generics in java

The following two tabs change content below.
Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

Latest posts by Prasad Kharkar (see all)

You may have heard all the fuss about the generics in java. Collections use them. But what are they exactly? We are going to get introduced to generics in this article.

Consider you want a data structure which stores some strings in an ordered manner. What comes to the mind? arrays and ArrayList . Let’s declare them

If we add anything other than String into stringsArray, then compiler will stop you, complaining that type cannot be added to an array of String.

This is good for us, we don’t want anything other than String in stringsArray . But we would like to use ArrayList instead of arrays as it provides advantages over it. But what happens we add something to stringsList instead of only String

This is because the statement List list = new ArrayList(); means that you can add ANYTHING into the collection. Any object can be added to it. Here comes the benefit of generics into picture.

If you want only a list of String then you can define it as.


After declaring like this, you will get type safety at compile time. So if you add anything other than String into stringsList now, it will not compile.

This will stop you from adding anything other than String saying The method add(String) in the type List<String> is not applicable for the arguments (Integer) .

This is the advantage of using generics for compile time safety.


Share Button

7 comments for “Generics: An introduction to generics in java

  1. Sudhir Kumar
    November 26, 2013 at 12:58 am

    Here comes the benefit of generics into picture.

    If you want only a list of String then you can define it as.

    1. List stringsList = new ArrayList();

    instead of using ArrayList , we can use Array of String or Array of Integer. why Generics?

    • Prasad Kharkar
      November 26, 2013 at 10:16 am

      The declaration List stringsList = new ArrayList(); will cause any object to be added into the list and you cannot control that. stringsList can include anything from string, Integer, Cat or even Object.

      When you do List stringsList = new ArrayList(); then the type of elements to be added is checked at compile time only. It will only add String type elements.

      • Sudhir Kumar
        November 27, 2013 at 1:28 am

        i was asking about List of Generic type .

  2. Pranaya
    June 27, 2016 at 2:43 am

    List stringsList = new ArrayList(); — is better practice in Java7

    • Pranaya
      June 27, 2016 at 2:45 am

      List<> stringsList = new ArrayList<>(); — is better practice

      • Pranaya
        June 27, 2016 at 2:48 am

        issue with reply box.

        List <String> stringsList = new ArrayList <>(); — is better practice

        • June 27, 2016 at 7:32 am

          Hi Pranaya, thank you for your comment. new ArrayList <>(); is syntactical sugar. Above code was written keeping java 1.6 users in mind. Thank you for your inputs.

Leave a Reply

Your email address will not be published. Required fields are marked *