Join Regular Classroom : Visit ClassroomTech

JAVA – codewindow.in

Related Topics

JAVA Programming

public static int getMax(int[] array) {
    int max = array[0];
    for (int i = 1; i < array.length; i++) {
        if (array[i] > max) {
            max = array[i];
        }
    }
    return max;
}

This method only works with arrays of integers. With generics, you can define a more generic version of the method that works with any type that implements the Comparable interface, as follows:

public static <T extends Comparable<T>> 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;
}

This version of the method uses a type parameter T that is bounded by the Comparable<T> interface. This means that the method works with any type that implements the Comparable interface, and the compiler ensures that only such types are used as arguments to the method.

public class Box<T> {
    private T content;

    public void setContent(T content) {
        this.content = content;
    }

    public T getContent() {
        return content;
    }
}

In this example, Box is a generic class that can hold any type of object. The type parameter T is declared in angle brackets after the class name, and it can be used in the class methods to ensure type safety.

  1. Declaring a generic interface:

public interface List<T> {
    void add(T element);
    T get(int index);
}

In this example, List is a generic interface that specifies two methods for adding and getting elements of any type.

  1. Declaring a generic method:

public static <T> void printArray(T[] array) {
    for (T element : array) {
        System.out.print(element + " ");
    }
    System.out.println();
}

In this example, printArray is a generic method that takes an array of any type T and prints its elements. The type parameter T is declared before the method return type, and it can be used in the method body to ensure type safety.

These examples show how generics can be used to create flexible and type-safe code that can work with a variety of data types.

<? extends type>

For example, let’s say we have a method that takes a list of numbers and returns the sum of those numbers. We can use an upper-bounded wildcard to ensure that the list contains only objects that are subtypes of the Number class:

public static double sum(List<? extends Number> list) {
    double sum = 0;
    for (Number n : list) {
        sum += n.doubleValue();
    }
    return sum;
}

This method can be called with a list of any subclass of Number, such as Integer, Double, or Float.

A lower-bounded wildcard is used to specify that the type parameter must be a supertype of a particular class or interface. The syntax for a lower-bounded wildcard is:

<? super type>

For example, let’s say we have a method that takes a list of objects and adds a new object to the list. We can use a lower-bounded wildcard to ensure that the new object is a subtype of the objects in the list:

public static void addToList(List<? super Integer> list) {
    list.add(new Integer(10));
}

This method can be called with a list of any supertype of Integer, such as Number or Object.

List<String> myList = new ArrayList<>();
myList.add("Hello");
String greeting = myList.get(0);

In this example, the type parameter for the ArrayList constructor is not specified explicitly, but the Java compiler infers that the type argument should be String based on the context in which the ArrayList is assigned to the List variable. When we call the get method on the List object, we can be sure that it returns a String, without having to cast the result.

Type erasure and type inference are important concepts in Java generics, and understanding how they work can help you write more concise and efficient code.

      

Go through our study material. Your Job is awaiting.

Recent Posts
Categories