Home > Java > javaTutorial > body text

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

高洛峰
Release: 2016-12-19 15:43:11
Original
2247 people have browsed it

Types originally included: simple types and complex types. After the introduction of generics, complex types are divided into more details;

Now List, List are two different types; and there is no inheritance relationship;

The benefits of generics are:

Start version

 public void write(Integer i, Integer[] ia);  public void write(Double  d, Double[] da);
Copy after login

Generic version

 public <T> void write(T t, T[] ta);
Copy after login

Simplified code

Define generics

1. Define it after the class

Follow the class name

 public class TestClassDefine<T, S extends T>{}
Copy after login

Define generic T, S, and S inherits T

2. It is defined after the method decorator

immediately after the modifier (public)

 public <T, S extends T> T testGenericMethodDefine(T t, S s){}
Copy after login

defines the generic T, S, and S inherits T

instantiates the generic

1. Instantiate a generic defined on a class

when first declaring a class variable or instantiating it. For example

    List<String> list;     list = new ArrayList<String>;
Copy after login

when the second inherited class or implements an interface. For example

public class MyList<E> extends ArrayList<E> implements List<E> {...}
Copy after login

2. Generics on instantiation definition method

When calling a generic method, the compiler automatically assigns a value to the type parameter (generic). When the assignment cannot be successful, a compilation error is reported

Wildcard (?)

There are definitions and assignments of generics above; when assigning values, the above section said that the assigned values ​​are all specific types. When the assigned type is uncertain, we use wildcards (?) instead Now:

Such as

List<?> unknownList;  List<? extends Number> unknownNumberList;  
List<? super Integer> unknownBaseLineIntgerList;
Copy after login

In the Java collection framework, for a container class whose parameter value is of unknown type, you can only read the elements and cannot add elements to it. Because its type is unknown, the compiler cannot recognize the addition. Whether the type of element and the type of container are compatible, the only exception is NULL


For more related articles about the difference between T and question mark (wildcard) in java generics, please pay attention to PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!