search
HomeJavajavaTutorialGeneric wildcards and bounded type parameters in Java

Type wildcard

The type wildcard is generally used? instead of the specific type parameter (Here is the type parameter, not the type parameter). When you do not need to use the specific functions of the type when operating a type, but only use the functions in the Object class, you can use the ? wildcard character to represent unknown types. For example, List> is logically the parent class of all List such as List, List, etc.

public class GenericTest {    
    public static void main(String[] args) {
        List<String> name = new ArrayList<String>();
        List<Integer> age = new ArrayList<Integer>();
        List<Number> number = new ArrayList<Number>();   
        name.add("zwj");
        age.add(18);
        number.add(120);
   
        getNumberData(age); // 报错
        getNumberData(number); // 120
        getData(name);  // zwj
        getData(age); // 18
        getData(number); // 120
        //getUperNumber(name); // 出现错误,方法中的参数已经限定了参数泛型上限为Number
        getUperNumber(age); //18
        getUperNumber(number); //120       
    }
    /**
     * 在使用List<Number>作为形参的方法中,不能使用List<Ingeter>的实例传入,
     * 也就是说不能把List<Integer>看作为List<Number>的子类;
     */
    public static void getNumberData(List<Number> data) {
        System.out.println("data :" + data.get(0));
    }
    /**
     * 使用类型通配符可以表示同时是List<Integer>和List<Number>的引用类型。
     * 类型通配符一般是使用?代替具体的类型实参,注意此处是类型实参;
     * 和Number、String、Integer一样都是一种实际的类型,可以把?看成所有类型的父类。
     */
    public static void getData(List<?> data) {
        System.out.println("data :" + data.get(0));
    }
    /**
     * 类型通配符上限通过形如List来定义,如此定义就是通配符泛型值接受Number及其下层子类类型。
     */
    public static void getUperNumber(List<? extends Number> data) {
          System.out.println("data :" + data.get(0));
    }
}

Bounded type parameters

Sometimes we want to limit the range of types that are allowed to be passed to a type parameter. For example, a method that operates on numbers may only wish to accept instances of Number or a subclass of Number. At this time, Monkey needs to add an upper bound to the generic, that is, the type argument passed in must be a subtype of the specified type.

To declare a bounded type parameter, first list the name of the type parameter, followed by the extends or super keyword, and finally followed by its upper or lower bound. From this we can know that the addition of the upper and lower bounds of a generic must be together with the declaration of the generic.

extends T> indicates that the type represented by the wildcard is a subclass of the T type. For example, when adding elements to a collection, you can add either an E type object or a subtype object of E. Why? Because when fetching, E type can receive both E class objects and E subtype objects.

super T> means that the type represented by the wildcard is the parent class of the T type. For example, when obtaining elements from a collection for operation, you can use the type of the current element to receive it, or you can use the parent type of the current element to receive it.

public class GenericMethodTest {
    // 比较三个值并返回最大值
    public static <T extends Comparable<T>> T getMaxNuum(T x, T y, T z) {                     
        T max = x; // 假设x是初始最大值
        if ( y.compareTo( max ) > 0 ){
            max = y; //y 更大
        }
        if ( z.compareTo( max ) > 0 ){
            max = z; // 现在 z 更大           
        }
        return max; // 返回最大对象
    }
    public static void main( String args[] ) {
        System.out.println( "结果 " + getMaxNuum(3, 4, 5) );  // 结果 5
        System.out.println( "结果 " + getMaxNuum(1.2, 6.6, 10.10) ); // 结果 10.10
   }
}

We can also change the previous definition of the generic class:

public class GenericClassDemo<T extends Number> { 
    private T t;   
    public GenericClassDemo() { 
    }
    public GenericClassDemo(T t) { 
        this.t = t;
    }
    public void setT(T t) {
        this.t = t;
    }
    public T getT(){ 
        return t;
    }
}

At this time, when instantiating the GenericClassDemo generic class, the parameter type can only be Number and Number subclasses. On this basis, let’s look at an example of a generic method:

/**
 * 在泛型方法中添加上下边界限制的时候, 必须在泛型声明的时候添加;
 * 也就是在权限修饰符与返回值之间的<T>上添加上下边界
 */
public <T extends Number> T getT(GeneriClassDemo<T> demo){
    T t = demo.getT();
    return t;
}

Generic array

You cannot create an array of an exact generic type in java of.

List<String>[] lsa = new ArrayList<String>[10];  // Not really allowed. 
Object o = lsa;    
Object[] oa = (Object[]) o;    
List<Integer> li = new ArrayList<Integer>();    
li.add(new Integer(3));    
oa[1] = li; // Unsound, but passes run time store check    
String s = lsa[1].get(0); // Run-time error: ClassCastException.

In this case, due to the JVM generic erasure mechanism, the JVM does not know the generic information at runtime, so you can assign an ArrayList to oa[1] without exception. However, when retrieving data, a type conversion is required, so a ClassCastException will occur. If a generic array can be declared, the above situation will not cause any warnings or errors during compilation. Only when running That's when things go wrong. To limit the declaration of generic arrays, in such cases, you can be prompted at compile time that the code has type safety issues, which is much better than no prompt at all.

The following wildcard methods are allowed: The type of the array cannot be a type variable, unless the wildcard method is used, because for the wildcard method, the final data needs to be retrieved Explicit type conversion.

List<?>[] lsa = new List<?>[10]; // OK, array of unbounded wildcard type.    
Object o = lsa;    
Object[] oa = (Object[]) o;    
List<Integer> li = new ArrayList<Integer>();    
li.add(new Integer(3));    
oa[1] = li; // Correct.    
Integer i = (Integer) lsa[1].get(0); // OK

Related articles:

The difference between T and question mark (wildcard) in Java generics

Generics in Java Detailed explanation of type

The above is the detailed content of Generic wildcards and bounded type parameters in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?Mar 17, 2025 pm 05:46 PM

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?Mar 17, 2025 pm 05:45 PM

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?Mar 17, 2025 pm 05:44 PM

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?Mar 17, 2025 pm 05:43 PM

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

How does Java's classloading mechanism work, including different classloaders and their delegation models?How does Java's classloading mechanism work, including different classloaders and their delegation models?Mar 17, 2025 pm 05:35 PM

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment