4

If so, what is the difference between the two? Are there actually two separate class definitions in the java library? One for the old ArrayList and one for the new generic one? This is purely a question from curiosity.

1

6 Answers 6

6

There is just one class ArrayList, but it supports generics, which means that you can annotate it with a type. However, that is optional (mostly for backwards compatibility), so you can also continue to use the raw type (but that is discouraged).

Note that Java generics only happen for the compiler, at runtime the ArrayList instance itself has no idea what generic type you assigned to it. This means that it works exactly the same way internally if you annotate it or not.

Unlike arrays, there are no new separate classes for generic collections. So while there is actually a different class for Integer[] than for String[] (or for Integer[][]), the class for ArrayList<String> is the same as for ArrayList<Integer> (and ArrayList<?> and ArrayList and ArrayList<List<Integer>>).

And also unlike arrays, there is nothing special in generics for use by collections (although that remains their most popular application). The same mechanism can be used for completely different things, for example a Callable.

Sign up to request clarification or add additional context in comments.

Comments

4

No, they're the same class, in that there's only one class file. Existing API classes were "generified" in such a way that old code could continue to be compiled with no change, and interoperate nicely with new code which used the new features.

6 Comments

Does this mean that if you make an ArrayList (without the generic), it's automatically passing in Object as the generic type?
If you use the plain ArrayList you don't have generics at all, it's a wide collection that can store references to whatever object you'd like. With the constraint that you'll have to cast them to the proper type when you retrieve them.
No; ArrayList<Object> and ArrayList are different types with different characteristics. The latter is called the "raw type" and you can make assignments between the raw type and generic type; in fact, you can assign a generic type to the raw type without even a warning. Read Angelika Langer's awesome Java generics FAQ for info on this and other subtle points: angelikalanger.com/GenericsFAQ .
Everybody mentioned that generics are just a "compile time" concern. So at runtime there is no difference between ArrayList<Integer> and ArrayList<String>, but at compile time they differ. Having this in mind ArrayList and ArrayList<Object> differ for the compiler, but ArrayList<?> and ArrayList<Object> are the same.
@AmirPashazadeh: "ArrayList<?> and ArrayList<Object> are the same". No. You cannot add anything to an ArrayList<?> for example. And you can assign an ArrayList<Integer> to an ArrayList<?> variable, but not to a ArrayList<Object> variable.
|
1

They are the same class, actually the difference resides only in the type checker.

ArrayList<T> is just an annotation for the compiler to enforce that just objects of type T can be added to the specific list, and consequently it automatically allows you to retrieve objects from the collection that are already of type T.

This mechanism, that is called parametric polymorphism, has been added to Java since version 5 and it's implemented through type erasure: the compiler will make sure that you use the class just with correct types, as you defined, but at runtime the old ArrayList will be used.

They're exactly the same class, indeed.

Comments

1

They're the same. Generic type restrictions are enforced at compile-time, but no type information is actually present at runtime. This is called type erasure, and it means that at runtime, it's actually impossible to determine what generic type(s) an instance was declared with. See this Wikipedia page for more info.

Comments

1
  • ArrayList: Obsolete (unparametrized). Used only for backward-compatibility. Generates warning.

  • ArrayList<Object>: An array-based list of objects.

  • ArrayList<? extends Object>: An array-based list of some type of Object.
    For example, could be referring to an ArrayList<Integer>, an ArrayList<String>, or just a plain ArrayList<Object>.

Comments

1
ArrayList<Object> a1 = new ArrayList<Object>();
ArrayList a2 = new ArrayList();

boolean same = a1.getClass().equals(a2.getClass());
System.out.println("same="+same);

This prints "true". Both arrays are in fact objects of the same class.

2 Comments

a == would also print true.
Yes, because they compile to the same class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.