Java Are ArrayList and ArrayList <Object> two different classes?

If so, what is the difference between the two? Does the java library have two separate class definitions? One for the old ArrayList and one for the new generic? This is just a matter of curiosity.

+4
source share
6 answers

There is only one ArrayList class, but it supports generics, which means you can annotate it with a type. However, this is not necessary (mainly for backward compatibility), so you can also continue to use the raw type (but this is not recommended).

Note that Java generators occur only for the compiler, and at run time, the ArrayList instance itself does not know which generic type you assigned to it. This means that it works exactly the same as internally if you annotate it or not.

Unlike arrays, there are no new separate classes for shared collections. Thus, although there is 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 about generics for use by collections (although this remains their most popular application). The same mechanism can be used for completely different things, for example Callable.

+6
source

No, this is the same class in which there is only one class file. Existing API classes were “generalized” in such a way that the old code could compile without changes and interact well with the new code that used the new functions.

+4
source

This is the same class, in fact the difference is only in the type controller.

ArrayList<T> is just an annotation for the compiler to ensure that only objects of type T can be added to a specific list, and therefore it automatically allows you to retrieve objects from a collection that already have type T

This mechanism, called parametric polymorphism , was added in Java since version 5 and implemented by type erasure: the compiler will make sure that you use the class with the correct types only, as you defined, but the old ArrayList will be used at runtime.

They are exactly the same.

+1
source
They are the same. General type constraints apply at compile time, but type information is not actually present at run time. This is called type erasure, which means that at runtime it is actually impossible to determine which type of type was declared by the instance. See this page on Wikipedia for more information.
+1
source
 ArrayList<Object> a1 = new ArrayList<Object>(); ArrayList a2 = new ArrayList(); boolean same = a1.getClass().equals(a2.getClass()); System.out.println("same="+same); 

Prints "true". Both arrays are actually objects of the same class.

+1
source
  • ArrayList : Deprecated (non-parameterized). Used for backward compatibility only. Generates a warning.

  • ArrayList<Object> : A list of objects based on an array.

  • ArrayList<? extends Object> ArrayList<? extends Object> : a list based on an array of some type Object .
    For example, you can refer to ArrayList<Integer> , a ArrayList<String> or simply to a regular ArrayList<Object> .

+1
source

Source: https://habr.com/ru/post/1397363/


All Articles