Hi all, in this blog I’ll be focusing on generics in java language. I’ll try to give some basic understanding of java generics and then would show generics in action. Java generics are more powerful than what we can think of. They are a must to know concept for a good Java developer. I’ll try to introduce them in a simple way. The intent here is to help anyone willing to start with the Java generics programming. So let’s begin with the agenda that i am going to cover in this blog:
- Generics, quick intro for the beginners.
- Why generics?
- When to use generics.
Generics were first introduced by the JDK 5, and since then they have become an integral part of Java. “Many algorithms are logically the same no matter what type of data they are being applied to”. The statement may look easy to understand, but wasn’t very easy to implement prior to the introduction of generics to the Java language. So with generics, we now can create classes, interfaces and methods that would work in a type safe manner with various kinds of data being applied on them. The most significant effect of generics being introduced in the java language was seen on the Collections Framework. I will cover the working of generics with collection framework some other time.
Generics are more commonly referred to as parameterized types. They enable us to create classes, interfaces, and methods in which the type of the data on which they operate is specified as a parameter. That covers the brief introduction of generics.
Why and When Generics
People who are familiar with the Object class(parent class of any other class in java) would ask what is so special about generics? use Object references wherever you want to have a generalized type? Well that question is a genuine one because even before the introduction of generics, java has already provided a way to create generalized classes, interfaces, and methods through the references of the Object class. Since, Object class is the superclass of all other classes in Java, an object reference could refer to any type object. But with this approach of using a reference of Object class there were some limitations+drawbacks:
- Need to explicitly employ casts between Object and the type of data actually being operated upon.
- It lacked type safety.
To showcase those limitations let’s go through the code snippet below:
Now let us see how introduction of generics resolved those two problems:
With the example i have shown above, the difference to using Object references and Generics should be clear. This blog provides a way to get familiar with Generics, in my next blog i would pick the following topics:
- Bounded generics.
- Wildcards with generics.
- Type erasure in generics.
That is all from the perspective of this blog, if you have any doubts or questions please feel free to post them in the comments section below. In case you have some feedback please feel free to share that as well so that i can work on it and get more relevant content for the community.
- The official Java documentation.
- Java: The Complete Reference.(Book)