Today, I'll be discussing java generics. Hope after reading this article you able to learn the basic concept of java generics.

You are watching: One of the advantages of using generics is


1. What are generics in java?

Generics are one of the most significant improvements for the Java programming language, alongside Lambda expressions. It added to the Java programming language in 2004 within version J2SE 5.0. Generics means parameterized types. The idea is to allow type (Integer, String, … etc, and user-defined types) to be a parameter to methods, classes, and interfaces. So by using this we can do generic programming. It also provides us to create classes that work with different data types. Another important thing is it allows the programmers to specify, with a single method declaration, a set of related methods, or with a single class declaration, a set of related types, respectively.

2. When should we use generics?

Before generics, we can store any type of object in the collection, i.e., non-generic. Now generics force the java programmer to store a specific type of object. Generics should be used instead of raw types (Collection instead of Collection, Callable instead of Callable, …) or Object to guarantee type safety, define clear type constraints on the contracts and algorithms, and significantly ease the code maintenance and refactoring.

3. How can we use generics?

Before using the generics everyone should be aware of the type parameters naming conventions are important to learn generics thoroughly. The common type parameters are as follows

T -> Type

public interface Iterable { /** * Returns an iterator over the elements of this object. */ public operator fun iterator(): Iterator}E -> Elementpublic static void printArray(E<> elements) { for ( E element : elements) { System.out.println(element); }}K -> Keypublic interface Map {/** * Returns the value corresponding to the given , or `null` if such a key is not present in the map. */ public operator fun get(key: K): V?}N -> Number// creae a generics method public void genericsMethod(N data) { System.out.println("Generics Method:"); System.out.println("Data Passed: " + data); }V -> Valuepublic interface Map {/** * Represents a key/value pair held by a . */ public interface Entry { /** * Returns the key of this key/value pair. */ public val key: K/** * Returns the value of this key/value pair. */ public val value: V }}

4. Why should we use or advantage of generics?

To achieve compile-time checking.To avoid ClassCastException error.To achieve code reusability. We can write a method/class/interface once and use it for any type we want.An object is the superclass of all other classes and Object reference can refer to any type of object. These features lack type safety. Generics adds that type of safety feature.

5. Disadvantage of generics?

According to oracle documentation, the following points are the disadvantage of generics:

Cannot instantiate Generic types with primitive types.Cannot create instances of type parameters.Cannot declare static fields whose types are type parameters.Cannot use casts or instanceof with parameterized types.Cannot create arrays of parameterized types.Cannot create, catch, or throw Objects of parameterized types.

See more: Dance Moms Season 7 Episode 21 "Ashlee'S Big Decision Part 1"

That's all for today. If you learn more about java generics please go to oracle java generics documentation. If you enjoy the article don't forget to hit the clap button.