DEV Community

Cover image for Java Quickies. Generics
Tristan Elliott
Tristan Elliott

Posted on • Updated on

Java Quickies. Generics


  • This series is going to be dedicated to the basic understanding of Java. When ever I find myself asking, "How does this work ?". I will create a blog post and put it here. This series will not be in order so feel free to read what ever post you find most relevant. Everything I have posted in this post can be found in the official generics documentation HERE

What are Generics?

  • If you have worked with Java at all then I am sure you have seen < > those two little angle brackets and it is because of them that generics in Java is possible. Generics enables types (classes and interfaces) to be parameters when defining classes, interfaces or methods. It is similar to to when we define parameters in a normal method. The difference is that the inputs to methods use values, while the inputs to the <> are types(classes, interfaces)

Why use Generics?

  • Well there are two main reasons:

1) : stronger type checking at compile time. This means that our compiler is able to detect the errors for us, which eliminates some Runtime errors. Runtime errors are the errors that make our app crash.

2) : elimination of casts. If you are unfamiliar with casts, it is the process of making a variable behave as the variable of another type. Example of using casts below :

List list = new ArrayList();
String s = (String) list.get(0);

Enter fullscreen mode Exit fullscreen mode
  • thanks to generics that becomes:
List<String> list = new ArrayList<String>();
String s = list.get(0);   // no cast

Enter fullscreen mode Exit fullscreen mode
  • Notice in the second example that we no longer have the (String) syntax. This change may seem small but it aids in the elimination of errors. Now lets move on to an example of a non-generic class and a generic class.

non-generic class

public class Box {
    private Object object;
    public Box (Object object){
       this.object = object;
    public void set(Object object) { this.object = object; }
    public Object get() { return object; }

Bob box = new Box("It do be like that sometimes")

Enter fullscreen mode Exit fullscreen mode
  • Notice that we used Object instead of a specific type, this is because Object is the super class of all other classes in Java. Using Object allows us to use any type of object we want. The down side to using Object is that there is no way of knowing if we are using different types when we shouldn't be. This is a danger, because we will only know about certain errors at runtime. A Runtime error will crash our app.

generic class

public class Box<T> {
    // T stands for "Type"
    private T t;

    public void set(T t) { this.t = t; }
    public T get() { return t; }

Box<Integer> integerBox = new Box<Integer>(); 
// creating a generic box

Enter fullscreen mode Exit fullscreen mode
  • As you noticed, all we did was replace Object with T and added some angle brackets(more on them later). This will allow the compiler to do type checks prior to running the code and will result in far fewer Runtime errors.

The Diamond <>

  • The two angle brackets that we have seen are informally called the 'diamond operator'. Its main job is to simplify the use of generics in Java. When looking at java code I am sure that you have seen something like this Box <Integer> intBox = new Box<>() , now what does the empty <> mean? Well, long story short the compiler is inferring the type, so we do not have to explicitly state what it is.


-This is just a surface level understanding of generics, please check out the documentation HERE

  • Thank you for taking the time out of your day to read this blog post of mine. If you have any questions or concerns please comment below or reach out to me on Twitter.
  • Also make sure to checkout my YouTube channel for more programming tutorials.

Top comments (0)