EN
Java - What is difference between List<?> and List<Object>?
1 answers
6 points
As in the question.
I am thinking what is difference between List<?>
and List<Object>
?
Each class
extends Object
, so why it was introduced?
1 answer
6 points
By using List<?>
you are able to assign to variable types of:
List<String>
,List<Integer>
,List<Boolean>
,List<Object>
,- etc.
By using List<Object>
you are not.
Example source code:
xxxxxxxxxx
1
import java.util.List;
2
import java.util.ArrayList;
3
4
public class Program {
5
6
public static void main(String[] args) {
7
8
List<?> a = new ArrayList<String>();
9
List<?> b = new ArrayList<Integer>();
10
List<?> c = new ArrayList<Boolean>();
11
List<?> d = new ArrayList<Object>();
12
13
// List<Object> a = new ArrayList<String>(); // <--- incorrect assignment
14
// List<Object> b = new ArrayList<Integer>(); // <--- incorrect assignment
15
// List<Object> c = new ArrayList<Boolean>(); // <--- incorrect assignment
16
// List<Object> d = new ArrayList<Object>(); // <--- incorrect assignment
17
}
18
}
0 commentsShow commentsAdd comment