6

What is the difference between List and List<Object> in java?

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
Rnet
  • 4,796
  • 9
  • 47
  • 83

6 Answers6

4

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.

rd22
  • 1,032
  • 1
  • 18
  • 34
amukhachov
  • 5,822
  • 1
  • 41
  • 60
3

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

Steven Schlansker
  • 37,580
  • 14
  • 81
  • 100
2

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 Lists of all parameters.

newacct
  • 119,665
  • 29
  • 163
  • 224
1

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. :)

Till Helge
  • 9,253
  • 2
  • 40
  • 56
1

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]

zengr
  • 38,346
  • 37
  • 130
  • 192
  • 3
    How does `List` help to catch errors, though? Isn't `Object` the most ancestral class possible? – Kerrek SB Oct 25 '11 at 07:35
  • I think the OP means any type of object when he says: `List` and his question is more focused on why use generics. – zengr Oct 25 '11 at 07:37
0

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.

irreputable
  • 44,725
  • 9
  • 65
  • 93