What is the difference between List
and List<Object>
in java?
6 Answers
Suppose you've got two lists:
List<Object> mainList = new ArrayList<Object>();
and one with a raw type:
List rawList = new ArrayList();
Now let's do something like this:
List<Integer> intList = new ArrayList<Integer>();
intList.add(42);
mainList.addAll(intList);
rawList.addAll(intList);
But there is somewhere one more list which contains String objects:
List<String> strList = new ArrayList<String>()
.
When we try to call
strList.addAll(mainList)
we get a Compilation Error :
The method addAll(Collection<? extends String>) in the type List<String> is not applicable for the arguments (List<Object>)
.
But if we try to call
strList.addAll(rawList)
we get just a warning.
So if you are using only lists of objects in your application it's really no difference between List and List. But since Java 1.5 it's a good idea to use generics for providing compile-time type safety for your application.

- 1,032
- 1
- 18
- 34

- 5,822
- 1
- 41
- 60
One particular danger of raw types (e.g. List
without the <?>
) is that raw types disable checking even outside of their own declarations
Ref: a Java Puzzler that shows the danger of raw types. (starts at page 13) Short version: because a raw type was used in a statement, a generic type elsewhere was not evaluated by the compiler, and thus bizarre runtime exceptions happened.
Short answer: it's a warning to use a raw type, for good reason. Use an unbounded wildcard List<?>
in strong preference to a raw type List

- 37,580
- 14
- 81
- 100
The difference is this:
List<String> a = new ArrayList<String>();
List<Object> b = a; // compile error
List c = a; // fine
List<Object>
is not compatible with a List
of any other parameter. However, a raw List
is compatible with List
s of all parameters.

- 119,665
- 29
- 163
- 224
The difference is quite subtle. The more obivous of the two is List<Object>
which ist a list of arbitrary objects (as every class has Object as super class). The difference to List
now is that List
is untyped and therefore no type checks at all are performed, which ultimately leads to certain warnings and can lead to weird runtime behavior. While Java knows that List<Object>
is a list that might contain anything, it doesn't know that about a List
. Because if you consider "old style" Java, what you know would call List<String>
used to be called only List
. And that's the difference between those two...List<Object>
is defined to contain a wide spectrum of objects on purpose, while List
isn't.
I hope that sort of clarifies the matter. If not, just tell us what's still unclear. :)

- 9,253
- 2
- 40
- 56
-
4The technical term for using a generified type without a type parameter is using a "raw type". – Joachim Sauer Oct 25 '11 at 07:31
Generics in Java is important for better error checking before hand (at compile time) due to which, less time is spent during runtime checking and debugging.
So, there is no direct performance gains, but you are quickly able to catch errors.
Bottom line: It helps in speed up development.
I strongly recommend you to go through this: Generics in the Java Programming Language [pdf]

- 38,346
- 37
- 130
- 192
There are quiet some special treatments about raw types; there's no incentive to enumerate them all, since raw types are discouraged anyway.
Let's talk about a narrower problem, which is probably most pertinent: given an object, we mostly care about what methods we can invoke on it; for that purpose, is there any difference if its type is List
or List<Object>
?
The methods of List<Object>
are methods of List<E>
with E substituted with Object; The methods of raw type List
is the erasure of methods of List<E>
. So we have
List List<Object>
boolean add(Object) boolean add(Object)
Object get(int) Object get(int)
int size() int size()
boolean addAll(Collection) boolean addAll(Collection<? extends Object>)
... ...
most of their methods are the same! In that regard, the two types are hard to distinguish.

- 44,725
- 9
- 65
- 93