Why Generics? 1 (Java Why? Article Series 2)

Hüseyin K.
5 min readNov 8, 2022

Hi Java people. Ready for analyzing why we need Generics?

I am Huseyin. Have been a .Net developer for years and Java backend and Android developer for 6 years as well as a QA Automation Engineer team member for several projects for 3 years.

Have been observing that some of my friends , especially junior developers, are not completely aware of the point of using a structure a tool. As a developer, by experiencing the reusability, abstraction and the maintainability issues for years, I decided to implement the same structure in my life and abstract myself from answering similar questions 🙂. Thus, put my effort into creating this “Why Java? Article Series”. I just wanna to help developers to code consciously.

The target audience of “Why Java? Article Series” is the ones who already know coding with Java, but also want to understand the underlying language and the system.

Main purpose of these articles is to understand why we need some structures or tools in our framework in order to be conscious while using them. Of course there are a lot of sources about Java on the internet. What I experienced is that some different approaches while explaining the same thing helps people to understand the topics. What I am trying to do is actually that. A different kind of explanation from WHY point.

Articles are prepared to achieve the understanding of the topic from a step by step approach.

If these articles help you to understand a concept, I definitely feel very happy. Feel free to share and comment.

Sharing is learning at the same time! Enjoy.

Analyzing type-specific methods

Requirement: Make a function that takes a String array and a String Collection parameter, And copies all array elements to provided collection parameter.

Note that this method is specific to String type

Requirement: Make a function that takes an Integer array and a Integer Collection parameter, And copies all array elements to provided collection parameter.

Note that this method is specific to Integer type

I wrote two different type-specific methods, each of them performing the same process.

Is it possible to write a type-independent method?

REMEMBER: In coding world, everything is designed to write less code with high efficiency.

Requirement: Make a type-independent method, that will accept all Non-primitive types of arrays and all types of collections, and performs the same operation without needing to cast.

Solution attempt with Object

Since Object is the top level parent class for all types, Why do not we use Object type that can accept all kinds of type without complaining. Let’s try!

Method looks good. Let’s call this method;

It seems I achieved my goal. This method can accept any kind of object. What about the Object List that I populated? You know, I passed integer values as object. I wanna get first two items (1 and 2) and sum them up. Let’s try;

OMG! It seems a need casting. Everything was good until I perform a summation operation (not applicable to Object type). Since I have an Object collection, I need to cast every object to Integer before such operations.

Solution attempt with Unknown Type

Since we need something type-independent Let’s try unknown type collection.

As you see, unknown type did not work in our case. Summation operation is applicable to Number derived classes like Integer. If you doesn’t explicitly define the type, like Integer, compiler can not apply this operation.

Generic Method

As we see, the Object-type method and unknown-type did not help us. We need something type-independent, and moreover, we do not need to cast elements.

It means if we pass String-type Collection parameter, the method should process String-type Collection, if we pass Integer-type Collection parameter, the method should process Integer-type Collection. Thus, we do not need to cast elements for type-specific operations such as summation of two Integer elements, or concatenation of two Strings elements.

Solution: Generic Method (kind of type-independent)

Let’s create an Integer list with our brand-new generic method and apply a summation operation;

As you see, I defined the type of the ArrayList as Integer. So the method accepted that specific type. Moreover, I do not need to cast anything else. Compiler did not complain. I am happy :)

Let’s create a String list this time and apply a concatenation operation;

So, it is working with any kind of type. No need to cast. Type is not important any more. I can perform the same operation over different types with the help of Generic method.

I will continue explaining ‘why we need generics’, with Generic Class in the next article.

Thanks for reading. Hope this helps! See you in the next articles.

PS: Really benefited from especially Baeldung and other Java Tutorial Websites, as well as Official Java Documentation.

Dedicated to Dirty Java Rangers

--

--