What is Generics in Java?
Generics in Java is an advanced feature that enables code reusability and type safety. The code reusability functionality is achieved by defining Generic classes, interfaces, constructors, and methods. Generics use data type declaration to ensure type safety, thereby eliminating the chances of runtime errors. The angular bracket ‘<>’ symbol is used to implement Generics, and type parameters are defined within the brackets. These type parameters include ‘T’ for type, ‘E’ for element, ‘N’ for number, ‘K’ for key, and ‘V’ for value. An example of a Generic class with a Type T parameter is ‘public class DemoGenericClass
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
How Generics Implemented in Java?
Generics are implemented using angular brackets “<>” and the brackets enclose the type parameter “T” within them. For instance, in
A Generic class can be defined as:
Code:
public class MyGenericClass<T> {…}
The following are the standard type parameters:
- T: Type
- E: Element
- N: Number
- K: Key
- V: Value
In the case of multi-parameters, S, U, V, and so on are used to define the second, third, and fourth parameters, respectively.
Understanding Generics in Java
What is a type safety and how does it work? How are Generic classes, interfaces, constructors, and methods different from our regular classes and methods that make them reusable?
Java, which is a statically-typed language, requires declaring the data type of a variable before using it.
Example:
Code:
String myString ="eduCBA";
In the above code, “String” is the data type, and “myString” is the variable that will hold a value whose type is String.
Now, if one tries to pass a Boolean value in place of a string, like below:
Code:
String myBooleanStr = true;
It will immediately result in a compile-time error, stating “Type mismatch: cannot convert from boolean to String”
Output:
How to Achieve Code Reusability with Generics?
Now, let’s define a regular method:
Code:
public static void welcome(String name){ System.out.println("welcome to " + name); }
This method can be invoked only by passing a string parameter.
Code:
welcome("eduCBA");
Its output would be “welcome to eduCBA”
However, only a String can invoke this method. Attempting to pass any other data type, such as an integer or boolean, will result in a compile-time error, stating “The method welcome(String) in the type Runner is not applicable for the arguments (boolean)”
Output:
If one wants to invoke a similar method for a different data type, one can create a new method that accepts the required data type as a parameter. This technique of rewriting methods with parameters of different data types is called “method overloading.” However, one disadvantage of this approach is that it can lead to a larger code size.
One can also use Generics to rewrite the above method and use it for any data type we require.
Defining a Generic method:
Code:
public static <T> void welcome(T t){ System.out.println("it is " + t); }
Note: Here, “t” is an object of type T. The actual data type used to invoke the method will be assigned to the type parameter “T”.
This allows the method to be reused with different data types as needed, including strings, booleans, integers, and others.
Code:
welcome("educba"); Integer myint = 1; welcome(myint); welcome(true);
The above statements will provide the below output:
Output:
it is educba it is 1 it is true
Therefore, using Generics here, we can reuse our method for different data types.
How to Achieve Type Safety using Generics?
One of the primary differences between Arrays and Collections is that arrays can only store homogeneous data, whereas collections can store heterogeneous data. In other words, collections can store any type of object, including user-defined data types.
Note: Collections can only hold objects, including user-defined data types, and not primitive data types. To work with primitive data types, collections make use of wrapper classes.
Now, let’s consider an ArrayList.
Code:
ArrayList myList = new ArrayList();
One can add elements of various data types, such as strings, integers, and doubles, to an ArrayList object.
Code:
myList.add("eduCBA"); myList.add(1); myList.add(5.2);
On printing the ArrayList object, one can see that it contains the following values: [eduCBA, 1, 5.2].
Output:
To retrieve these values into variables, one needs to typecast them.
Code:
String someStr = (String)myList.get(0); Integer someInt = (Integer)myList.get(1); Double someFlt = (Double)myList.get(2);
If one does not typecast, it will prompt a compile-time error stating, “Type mismatch: cannot convert from Object to String”
Output:
Thus, one must typecast them to their respective types while retrieving the objects from the ArrayList. However, in real-time scenarios, an ArrayList can contain thousands of records, and manually typecasting every object may not be feasible. There is the risk of mistakenly typecasting an object to an incorrect data type. In such cases, a runtime error will occur, stating “Exception in thread “main” java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String at com.serviceClasess.Runner.main(Runner.java:43)”.
As there is no guarantee with regard to the type of data present inside a collection (in this case, ArrayList), they are considered unsafe to use with respect to type. Here, Generics play a role in providing type safety.
Using ArrayList with Generics:
Code:
ArrayList<String> myList = new ArrayList<String>();
The String type is specified inside the angular brackets “>” which means that this particular implementation of ArrayList can only hold String type data. If one tries to add another data type, it will simply throw a compile-time error. Here, the ArrayList has been made type-safe by eliminating its chances of adding a data type other than “String.”
Output:
Now, since one has specified the data type that is allowed to be added to the collection with the help of Generics, there is no need to typecast it while retrieving the data. One can simply retrieve the data by writing.
Code:
String someStr = myList.get(0);
Output:
How do Generics in Java make Work Easier?
- It helps make the collections type safe and ensures the code does not fail at a later point due to any run time exception.
- It saves the coder from having to typecast each object in the collection, which simplifies and speeds up the code development process.
- Generics allow writing code in a way that can work with multiple data types.
What else to do with Generics in Java?
So far, we have seen how we can achieve type safety and code reusability with Generics.
In addition to type safety and code reusability, here are some other features that Generics can provide:
- Bounded & Multiple Bounded Types
- Type Wildcards
1. Bounded Type
In the case of a bounded type, the data type of a parameter is bounded to a particular range. The keyword “extends” helps achieve this.
For example, let’s consider a Generic class with a bounded type parameter that extends the ‘Runnable interface’:
Code:
class myGenericClass<T extends Runnable>{}
Now, while creating its object in another class:
Code:
myGenericClass<Thread> myGen = new myGenericClass<Thread>();
The above statement will execute perfectly without any errors. In the case of the bounded type, one can pass the same class type or its child class type. Also, one can bind the parameter type to an interface and pass its implementations when invoking it, as in the example above.
What happens if one uses any other type of parameter?
Code:
myGenericClass<Integer> myGen = new myGenericClass<Integer >();
In the above case, it will result in a compile-time error, stating “Bound mismatch: The type Integer is not a valid substitute for the typecast
Output:
- Multiple bounded types: In the case of multiple bounded types, one can bind the parameter data type to more than one type.
Example:
Code:
class myGeneric<T extends Number & Runnable>{}
In this case, one can pass any type that extends the Number class and implements the Runnable interface. However, when using multiple bounded types, a few things should be noted:
- One cannot extend more than one class at a time.
- One can extend any number of interfaces simultaneously, i.e., there is no limit for interfaces.
- The class name should always come first, followed by the interface name. If not, it will result in a compile-time error.
2. Type Wildcards
The “?” (question mark) symbol represents Type Wildcards. It makes use of two main keywords:
- extends (to define upper bound)
- super (to define lower bounds).
Example:
Code:
ArrayList<? extends T> al
The ArrayList object “al” will hold any data of type T and all its subclasses.
Code:
ArrayList<? super T> al
The ArrayList object “al” will hold any data of type T and all its superclasses.
Advantages of Generics in Java
- Flexibility: Generics allow the code to accommodate different data types with the help of Generic classes and methods.
- Code Maintenance and Reusability: Due to Generic classes and methods, one need not rewrite the code in case of a change in requirements later, making the code easier to maintain and reuse.
- Type Safety: Provides type safety to the collection framework by defining the data type the collection can hold beforehand; and eliminating any chances of failure at run time due to ClassCastException.
- Eliminating the Need to Typecast: Since the data types being held by the collections are already determined, one need not typecast it at the time of retrieval. This reduces the code’s length and a coder’s effort.
Generics in Java Skills
- To work with Generics, one should be proficient in the basics of Java.
- One should understand how type checking and typecasting work. Thorough knowledge of other concepts such as method overloading, the relationship between parent and child classes, interfaces, and their implementations is necessary.
- Also, it is crucial to understand the difference between primitive data types (system-defined data type) and objects (user-defined data type) when working with the collection framework.
Why use Generics in Java?
- Using Generics makes the code more maintainable as it reduces the need to rewrite data type-specific code every time there is a change in requirements.
- By using Generics bounded type, one can restrict the data type and, at the same time, provide flexibility to the code by defining its range.
- Generics provide type safety, making the code less error-prone and less likely to fail at a later point.
Scope for Generics in Java
Generics scope is limited to compile time, i.e., the Generics concept is applicable only at compile time but not at run time.
Example:
Code:
ArrayList myList = new ArrayList<Integer>(); ArrayList myList = new ArrayList<Float>(); ArrayList myList = new ArrayList<Double>(); ArrayList myList = new ArrayList<Boolean>();
Here all the above four statements are the same. They will allow adding any type of data to the list object.
Conclusion
Generics renders coding easy for a coder. It diminishes the chances of encountering ClassCastException at run time by providing strong type-checking. Also, it eliminates the need for typecasting, which means less code needs to be written. It allows the development of Generic algorithms independent of the data type they are working with.
The above is the detailed content of What is Generics in Java?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

Java is a popular programming language that can be learned by both beginners and experienced developers. This tutorial starts with basic concepts and progresses through advanced topics. After installing the Java Development Kit, you can practice programming by creating a simple "Hello, World!" program. After you understand the code, use the command prompt to compile and run the program, and "Hello, World!" will be output on the console. Learning Java starts your programming journey, and as your mastery deepens, you can create more complex applications.
