“This is the first day of my participation in the Gwen Challenge in November. See details of the event: The last Gwen Challenge in 2021”.

Java generics are a new feature introduced in JDK 5. Generics provide compile-time type-safety checks that allow programmers to detect illegal types at compile time.

The nature of generics is parameterized types

Suppose we had a requirement to write a sort method that could sort arrays of integers, arrays of strings, or any other type of array.

Java generics can be used in this case.

Generic markup in Java:

  • E-element (used in collections, because collections contain elements)
  • T-type (Java class)
  • K-key (Key)
  • V-value (Value)
  • N-number (numeric type)
  • ? – Indicates an indeterminate Java type

Code test:

package csdncom.tt;

/** * Created by Java */
public class GenericMethodTest {
	// The generic method printArray
	public static <E> void printArray(E[] inputArray) {
		// Outputs an array element
		for (E element : inputArray) {
			System.out.printf("%s ", element);
		}
		System.out.println();
	}

	public static void main(String args[]) {
		// Create arrays of different types: Integer, Double, and Character
		Integer[] intArray = { 1.2.3.4.5 };
		Double[] doubleArray = { 1.1.2.2.3.3.4.4 };
		Character[] charArray = { 'j'.'a'.'v'.'a'.'l'.'y'.'y' };

		System.out.println("Integer array elements are :");
		printArray(intArray); // Pass an array of integers

		System.out.println("\n Double array elements are :");
		printArray(doubleArray); // Pass a double array

		System.out.println("\n array elements are :");
		printArray(charArray); // Pass an array of characters}}Copy the code

Compile the code and run as follows:

The integer array elements are: 1, 2, 3, 4, 5

The double array elements are: 1.1, 2.2, 3.3, and 4.4

The character array elements are j a V a L Y y

Bounded type parameters:

Sometimes, you will want to limit the range of type types that are allowed to be passed to a type parameter. For example, a method that operates on numbers might only want to accept an instance of Number or a subclass of Number. This is the purpose of bounded type parameters.

To declare a bounded type parameter, first list the name of the type parameter, followed by the extends keyword, followed by its upper bound.

The instance

The following example demonstrates how “extends” can be used in the general sense of “extends” (class) or “implements” (interface). The generic method in this example returns the maximum of three comparable objects.

package csdncom.tt;
/** * Created by Java */
public class MaximumTest {
	// Compare three values and return the maximum
	public static <T extends Comparable<T>> T maximum(T x, T y, T z) {
		T max = x; // Suppose x is the initial maximum
		if (y.compareTo(max) > 0) {
			max = y; / / y is greater
		}
		if (z.compareTo(max) > 0) {
			max = z; // Now z is bigger
		}
		return max; // Return the largest object
	}

	public static void main(String args[]) {
		System.out.printf("The largest number in %d, %d, and %d is %d\n\n".3.4.5, maximum(3.4.5));

		System.out.printf("%.1f, %.1f and %.1f the largest number is %.1f\n\n".6.6.8.8.7.7, maximum(6.6.8.8.7.7));

		System.out.printf("The largest number in %s, %s and %s is %s\n"."pear"."apple"."orange", maximum("pear"."apple"."orange")); }}Copy the code

Compile the code and run as follows:

The largest number of 3, 4 and 5 is 5

The largest number in 6.6, 8.8, and 7.7 is 8.8

Pear, Apple, and orange the largest number is pear

Liked, liked, followed, commented,

Clocked articles updated 92/100 days