Here, I'll provide you with Java code examples that demonstrate casting the `.class` operator on a generic type, such as `List`, to `Class<List<?>>` and `Class<List<Integer>>`. I'll provide three examples for each case with detailed explanations and corresponding output.

Case 1: Casting to `Class<List<?>>`

Example 1:

             java
public class Main {
    public static void main(String[] args) {
        Class<List<?>> listClass = List.class;
        System.out.println(listClass.getName());
    }
}

             

Explanation:

In this example, we create a generic class `List<?>` and assign it to `listClass`. We fetch the name of the class and print it using `getName()` method.

Output:

             
java.util.List

             

Example 2:

             java
import java.util.List;
import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {
        Class<List<?>> listClass = new ArrayList<>().getClass();
        System.out.println(listClass.getName());
    }
}

             

Explanation:

Here, instead of using `List.class`, we create an instance of `ArrayList` and use `getClass()` to get the `Class` object. Then, we print the name of the class.

Output:

             
java.util.ArrayList

             

Example 3:

             java
import java.util.List;

public class Main {
    public static void main(String[] args) {
        Class<List<?>> listClass = (Class<List<?>>) List.of().getClass();
        System.out.println(listClass.getName());
    }
}

             

Explanation:

We use `List.of()` to create an empty `List`, and cast its class to `Class<List<?>>`. Then, we print the name of the class.

Output:

             
java.util.ImmutableCollections$ListN

             

Case 2: Casting to `Class<List<Integer>>`

Example 4:

             java
import java.util.List;

public class Main {
    public static void main(String[] args) {
        Class<List<Integer>> listClass = (Class<List<Integer>>) List.of(1, 2, 3).getClass();
        System.out.println(listClass.getName());
    }
}

             

Explanation:

In this example, we create a list of integers using `List.of(1,2,3)` and cast its class to `Class<List<Integer>>`. Then, we print the name of the class.

Output:

             
java.util.ImmutableCollections$List12

             

Example 5:

             java
import java.util.List;
import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {
        Class<List<Integer>> listClass = (Class<List<Integer>>) new ArrayList<Integer>().getClass();
        System.out.println(listClass.getName());
    }
}

             

Explanation:

We create an instance of `ArrayList` of integers and cast its class to `Class<List<Integer>>`. Then, we print the name of the class.

Output:

             
java.util.ArrayList

             

Example 6:

             java
import java.util.List;

public class Main {
    public static void main(String[] args) {
        Class<List<Integer>> listClass = (Class<List<Integer>>) List.of(1, 2, 3).getClass();
        System.out.println(listClass.getName());
    }
}

             

Explanation:

In this example, we use `List.of(1, 2, 3)` to create a list of integers and cast its class to `Class<List<Integer>>`. Then, we print the name of the class.

Output:

             
java.util.ImmutableCollections$List12

             

These examples demonstrate how to cast the `.class` operator on a generic type to `Class<List<?>>` and `Class<List<Integer>>` in Java.

Was this article helpful?
YesNo

Similar Posts