- 0
Notifications Mark All Read
No New notification
- Login
- Get Prime
Java Generics Tutorial
What is Java Generics?
Java Generics is a feature of the Java programming language that allows you to write more generic and reusable code.
Java Generics enable you to create classes, interfaces, and methods that can operate on different data types, while providing type safety at compile time.
To understand the Java Generics, Read the Complete Article.
Java Generic Class
- In Java, you can create a Generic Class by declaring a type parameter within angle brackets < >. This type parameter is a placeholder for the actual data type that the class will work with.
- We can use any name you like for the type parameter, but by convention, single uppercase letters are commonly used, such as T, E, K, V, etc.
- Generics provide type safety to Java code by enabling compile-time checks for the data types used in the code.
- Generics allow you to write code that can work with different data types.
- Generics make your code more readable by allowing you to write code that explicitly states the data types used.
- Generics can improve performance by eliminating the need for type casting, which can be costly in terms of CPU time and memory usage
Let’s look at the Java Generics to perform certain operations.
Example 1: Java Program to Create a Generics Method
public class Main { // Generic method that returns the maximum element in an array public static > T getMax (T[]array) { T max = array[0]; for (int i = 1; i < array.length; i++) { if (array[i].compareTo (max) > 0) { max = array[i]; } } return max; } public static void main (String[]args) { // Test the generic method with Integer and Double arrays Integer[]intArray = { 4, 2, 9, 7, 5}; Double[]doubleArray = { 3.5, 6.8, 2.0, 9.1, 1.7}; // Find the maximum element in each array using the generic method Integer maxInt = getMax (intArray); Double maxDouble = getMax (doubleArray); System.out.println ("Max integer: " + maxInt); System.out.println ("Max double: " + maxDouble); } }
Output
Max integer: 9 Max double: 9.1
Example 2 : Java Program to Create a Generics Method
//GenericsMethodExample public class Main { public staticvoid printArray (T[]array) { for (T element:array) { System.out.print (element + " "); } System.out.println (); } public static void main (String[]args) { Integer[]intArray = { 1, 2, 3, 4, 5}; Double[]doubleArray = { 1.1, 2.2, 3.3, 4.4}; String[]stringArray = { "hello", "world"}; System.out.print ("Integer Array: "); printArray (intArray); System.out.print ("Double Array: "); printArray (doubleArray); System.out.print ("String Array: "); printArray (stringArray); } }
Output
Integer Array: 1 2 3 4 5 Double Array: 1.1 2.2 3.3 4.4 String Array: hello world
Bounded Types in Java Generics
Bounded Types in Java Generics allow you to restrict the type of parameter that can be used in a Generic Class or method.
- There are two types of bounds in Java Generics:
- Upper Bound: Specifies that the type parameter must be a subtype of a particular class or interface. To define an upper bound, use the extends keyword followed by the name of the class or interface.
- Lower Bound: Specifies that the type parameter must be a supertype of a particular class or interface. To define a lower bound, use the super keyword followed by the name of the class or interface.
Example 3 : Java Program to Bound Type
//BoundedTypesExample public class Main { public static void printNumber (T number) { System.out.println ("Number: " + number); } public static void main (String[]args) { Integer integerNumber = 7979; Double doubleNumber = 79.79; String stringText = "Hello"; printNumber (integerNumber); printNumber (doubleNumber); // The following line won't compile because String is not a subtype of Number // printNumber(stringText); } }
Output
Number: 7979 Number: 79.79
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment