WHY DO WE NEED JAVA GENERICS | JAVA GENERICS BENEFITS WITH EXAMPLE | TYPE ERASURE | InterviewDOT
Channel: Interview DOT
Date: 05/06/2021

#JAVATUTORIAL #JAVAGENERICS #JAVAGENERICSEXAMPLE #JAVACODEEXAMPLE #JAVACODEGENERICS

Click here - https://www.youtube.com/channel/UCd0U_xlQxdZynq09knDszXA?sub_confirmation=1 to get notifications.

WHY DO WE NEED JAVA GENERICS | JAVA GENERICS BENEFITS WITH EXANPLE | TYPE ERASURE | InterviewDOT

KINDLY SUBSCRIBE TO INTERVIEWDOT CHANNEL FOR LATEST SOFTWARE UPDATES THANKS

Why Generics?
If you closely look at java collection framework classes then you will observe that most classes take parameter/argument of type Object and return values from methods as Object. Now, in this form, they can take any java type as argument and return the same. They are essentially heterogeneous i.e. not of a particular similar type.

Programmers like us often wanted to specify that a collection contains elements only of a certain type e.g. Integer or String or Employee. In the original collection framework, having homogeneous collections was not possible without adding extra checks before adding some checks in code. Generics were introduced to remove this limitation to be very specific. They add this type checking of parameters in your code at compile-time, automatically. This saves us writing a lot of unnecessary code which actually does not add any value in run-time if written correctly.

How Generics works in Java
In the heart of generics is “type safety“. What exactly is type safety? It’s just a guarantee by compiler that if correct Types are used in correct places then there should not be any ClassCastException in runtime. A usecase can be list of Integer i.e. List Integer. If you declare a list in java like List Integer , then java guarantees that it will detect and report you any attempt to insert any non-integer type into above list.

Another important term in java generics is “type erasure“. It essentially means that all the extra information added using generics into source code will be removed from bytecode generated from it. Inside bytecode, it will be old java syntax which you will get if you don’t use generics at all. This necessarily helps in generating and executing code written prior to java 5 when generics were not added in language.

What is the point of type erasure?
Type erasure ensures that no new classes are created for parameterized types; consequently, generics incur no runtime overhead.


Types of Generics?
Now we have some understanding of what generics are all about. Now start exploring other important concepts revolving around generics. I will start by identifying the various ways, generics can be applied into sourcecode.


Generic Type Class or Interface
A class is generic if it declares one or more type variables. These type variables are known as the type parameters of the class. Let’s understand with an example.

DemoClass is a simple java class, which has one property t (can be more than one also); and type of property is Object.

What is not allowed to do with Generics?
So far we have learned about a number of things which you can do with generics in java to avoid many ClassCastException instances in your application. We also saw the usage of wildcards as well. Now it’s time to identify some tasks which are not allowed to do in java generics.