Introduction to Generics Tutorial

Plain and simple - there is no easy way to explain what a generic is to someone who is learning the language. The topic of generics is quite extensive and mastering them will allow you to code some pretty cool stuff. Anyone who has been following my tutorials up until this point is more that prepared to easily learn how they work. Let's get started with the very first piece of generics syntax by creating a new class. In the class declaration directly after the class name you will put a pair of angle brackets < >, sometimes referred to as chevrons. To begin with, we will place the letter T inside of the chevrons. There is no significance to the letter T – it can be any valid identifier (variable name) – T, V, and E have become somewhat standard. The letter T is a variable that will represent a reference data type such as Integer, String, Long, Double, etc. The variable or variables contained inside of the chevrons cannot ever represent primitive data types. If you have been following my tutorials then you should understand that the primitive wrapper classes will come in super handy with generics – I'll explain in the video.
The following is an example of a bare bones generic class declaration:
class BasicGeneric<T> { }

My new class BasicGeneric serves no purpose at the moment, but it will compile just fine. One thing to note is that T is technically called a type parameter. The next thing that we will do is to create an instance variable of type T:
class BasicGeneric<T> {
      T refVariable;
}


Great ... now we need either a method or a constructor to instantiate an object for our refVariable to refer to. I'll create a constructor. The constructor will contain a single parameter of type T:
class BasicGeneric<T> {
      T refVariable;
      BasicGeneric(T param) {
            refVariable = param;
      }
}


At this point you may be confused as to the purpose of T, but I promise you that in a minute you will understand completely. The last thing that I am going to add is a simple method to display the value of the object that refVariable refers to.
class BasicGeneric<T> {
      T refVariable;
      BasicGeneric(T param) {
            refVariable = param;
      }
      void displayReference() {
            System.out.println(refVariable);
      }
}




Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md GenericIntro
C:\Java>cd GenericIntro
C:\Java\GenericIntro>Notepad GenericIntro.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


class GenericIntro {
    public static void main(String args[]) {
        BasicGeneric<String> str1 = new BasicGeneric<String>(new String("Hello"));
        str1.displayReference();

        BasicGeneric<String> str2 = new BasicGeneric<String>("World");
        str2.displayReference();

        BasicGeneric<Integer> i1 = new BasicGeneric<Integer>(new Integer(41));
        i1.displayReference();

        BasicGeneric<Integer> i2 = new BasicGeneric<Integer>(1024);
        i2.displayReference();

        BasicGeneric<Boolean> b1 = new BasicGeneric<Boolean>(new Boolean("true"));
        b1.displayReference();

        BasicGeneric<Object> o1 = new BasicGeneric<Object>(new Object());
        o1.displayReference();
    }
}

class BasicGeneric<T> {
    T refVariable;
    BasicGeneric(T param) {
        refVariable = param;
    }
    void displayReference() {
        System.out.println(refVariable);
    }
}  

Now switch back to the command prompt (CMD) and type in javac GenericIntro.java and press Enter.
Now type in java GenericIntro and press Enter.


C:\Java\GenericIntro>javac GenericIntro.java
C:\Java\GenericIntro>java GenericIntro
Hello
World
41
1024
true
java.lang.Object@2a139a55


Final thoughts

The generic class in this tutorial doesn't really do much of anything. However, you should now be able to appreciate how a generic class is structured. This class is able to take in any sort of reference data type and create an internal object that we can manipulate and display values. The term generic is the perfect way to describe a class whose state can contain almost anything. In future tutorials I will be expanding on these concepts to demonstrate how to create generic classes that actually serve a purpose.


Tutorials