I feel like some of the people above have a problem with their understanding. First of all, List<?> represents an unbounded wildcard. It does not represent any type, but represents a specific type, but what this specific type is does not matter when we program. Don't care; List represents a native list, a list without generic parameters. There is nothing to say about this and its use is not recommended; List<Object> means that this list stores Object, as long as it is an Object, it can be put in; Finally List< ;? extends Object> represents a collection. This collection stores one of the specific subclasses of Object, rather than just a subclass of object.
The last thing I want to say is that <?> is actually a bit similar to <? extends Object>. This similarity is reflected in that they can match each other without generating any warnings. For example:
public class Base
{
public static void unbounded(List<?> list){}
public static void upBounded(List<? extends Object> list){}
public static void main(String args[])
{
List<?> list1 = new ArrayList<Object>() ;
List<? extends Object> list2 = new ArrayList<Object>() ;
unbounded(list2) ;
upBounded(list1) ;
}
}
However, these differences are only generated by the compiler at compile time. At runtime, due to erasure, they are all List<Object> My personal understanding
List<?> and List: It means that any type of objects can be placed in the collection. If List is used alone, in Eclipse There will be a warning; List<?> 与 List:说的是集合里可以放任意类型的对象,单独使用List的话,在Eclipse中会有警告;
List<Object>: It is about storing objects of type Object in a collection. Understanding the inheritance and upward transformation mechanism in Java, it is not difficult to find that this collection can actually store any type of object;
List<? extends Object>: It means that the objects saved in the collection must be subclass objects of the Object class; 🎜
🎜For details, please refer to the content about generics in "Java Core Technology (Volume 1)"...🎜
List is a type of collection. < >Represents a generic type List<?>Represents any type List<Object>Indicates that objects of object type can be placed in this collection List<? extends Object> means that any type of object that inherits from the object class can be placed in this collection
If you want to know more about the small gestures of the collection framework, you can read a column I wrote. There is a chapter about the collection framework
**List<?>, List, List<Object>, List<? extends Object> The differences are as follows: 1. Understand the generics in JDK. Generics are for type checking, and you want to put things in Collection , by limiting the types in <> during compilation, you can try to detect errors during compilation instead of throwing exceptions when running online. 2. If List is written like this, there will be a yellow line below it in eclipse, and there will be a warning. And it is also written in an irregular way, while the rest are written in a standardized way. 3.List<?> can hold any type; List<Object> can only hold Object type, but since Object is the parent class of all classes, other classes can be forced to type conversion by adding (Object) in front, List< ;? extends Object> Also, you can put any class or the Object class itself. Hope it helps you~~**
I feel like some of the people above have a problem with their understanding. First of all, List<?> represents an unbounded wildcard. It does not represent any type, but represents a specific type, but what this specific type is does not matter when we program. Don't care; List represents a native list, a list without generic parameters. There is nothing to say about this and its use is not recommended; List<Object> means that this list stores Object, as long as it is an Object, it can be put in; Finally List< ;? extends Object> represents a collection. This collection stores one of the specific subclasses of Object, rather than just a subclass of object.
The last thing I want to say is that <?> is actually a bit similar to <? extends Object>. This similarity is reflected in that they can match each other without generating any warnings. For example:
However, these differences are only generated by the compiler at compile time. At runtime, due to erasure, they are all List<Object>
My personal understanding
List<?>
andList
: It means that any type of objects can be placed in the collection. IfList
is used alone, in Eclipse There will be a warning;List<?>
与List
:说的是集合里可以放任意类型的对象,单独使用List
的话,在Eclipse中会有警告;List<Object>
:说的是集合里存放Object类型的对象,理解Java中的继承、及向上转型机制,不难发现这个集合其实可以保存任意类型的对象;List<? extends Object
List<Object>
: It is about storing objects of type Object in a collection. Understanding the inheritance and upward transformation mechanism in Java, it is not difficult to find that this collection can actually store any type of object;List<? extends Object
>: It means that the objects saved in the collection must be subclass objects of the Object class; 🎜 🎜For details, please refer to the content about generics in "Java Core Technology (Volume 1)"...🎜List is a type of collection.
< >Represents a generic type
List<?>Represents any type
List<Object>Indicates that objects of object type can be placed in this collection
List<? extends Object> means that any type of object that inherits from the object class can be placed in this collection
If you want to know more about the small gestures of the collection framework, you can read a column I wrote. There is a chapter about the collection framework
**List<?>, List, List<Object>, List<? extends Object> The differences are as follows:
1. Understand the generics in JDK. Generics are for type checking, and you want to put things in Collection , by limiting the types in <> during compilation, you can try to detect errors during compilation instead of throwing exceptions when running online.
2. If List is written like this, there will be a yellow line below it in eclipse, and there will be a warning. And it is also written in an irregular way, while the rest are written in a standardized way.
3.List<?> can hold any type; List<Object> can only hold Object type, but since Object is the parent class of all classes, other classes can be forced to type conversion by adding (Object) in front, List< ;? extends Object> Also, you can put any class or the Object class itself.
Hope it helps you~~**