List versus ArrayList as reference type?
If you use the first form, you are saying all you are ever going to use is the functionality of the List
interface - nothing else, especially nothing extra added by any implementation of it. This means you can easily change the implementation used (e.g. just substitute LinkedList
for ArrayList
in the instantiation), and not worry about it breaking the rest of the code because you might have used something specific to ArrayList
.
Difference in size of reference variables in Java. (List vs ArrayList)
All reference variables are of the same size, yes. This is addressed somewhat tangentially in the JVM specifiation in a couple of places: §2.2:
Like the Java programming language, the Java Virtual Machine operates on two kinds of types: primitive types and reference types. There are, correspondingly, two kinds of values that can be stored in variables, passed as arguments, returned by methods, and operated upon: primitive values and reference values.
(their emphasis)
and §2.6.1:
A single local variable can hold a value of type
boolean
,byte
,char
,short
,int
,float
,reference
, orreturnAddress
. A pair of local variables can hold a value of typelong
ordouble
.
(my emphasis)
As you can see, although it calls out things that have size differences (int
stored in a single JVM variable vs. long
stored in a pair of JVM variables, for instance), there's only one kind of reference, which fits in a single JVM variable. (A JVM variable is not the same thing as a variable at the Java source code level, though obviously they're closely related.)
In a comment you've said:
I got the general idea because ArrayList's reference would have more functionality than a List's reference...
This is the source of your misunderstanding. The functionality, etc., isn't contained in the reference, it's contained in the implementation (class) associated with what the reference refers to (the object). For example:
List<String> list = new ArrayList<>();
That declares a variable of type List
(hand-waving generics) that refers to an instance of ArrayList
, which is an object with an associated implementation (the ArrayList
class). The reference is just something telling the JVM where the object is in memory, nothing more. You can think of it as a number uniquely identifying the object (or even as a memory address if you like, though it's more complicated than that), though you can never directly interact with the number (address). The implementation (class) isn't duplicated for each object (object-specific state data is, but not the methods it uses for instance), but even if it were, that duplication wouldn't be in the reference to the object, it would be in the object itself. There are no List
objects, because List
is an interface. There are only ArrayList
objects, LinkedList
objects, etc.
Type List vs type ArrayList in Java
Almost always List
is preferred over ArrayList
because, for instance, List
can be translated into a LinkedList
without affecting the rest of the codebase.
If one used ArrayList
instead of List
, it's hard to change the ArrayList
implementation into a LinkedList
one because ArrayList
specific methods have been used in the codebase that would also require restructuring.
You can read about the List
implementations here.
You may start with an ArrayList
, but soon after discover that another implementation is the more appropriate choice.
Difference of List and ArrayList in declaration?
The first declaration lets you program to interface. It ensures that later on you can safely replace ArrayList
with, say, LinkedList
, and the rest of code is going to compile.
The second declaration lets you program to the class, so you could potentially use methods of ArrayList
which do not implement the List
interface. For example, you can call ensureCapacity()
on the list declared as ArrayList
, but not on a list declared as List
. Although generally programming to interface should be preferred, there is nothing wrong with doing it if you must call class-specific methods: for example, ability to call ensureCapacity()
could save some unnecessary reallocations if you know the new target size of your list.
What are the List or ArrayList declaration differences in Java?
The first one is only valid since Java 7, and is the equivalent of
List<String> list = new ArrayList<String>();
It's just less verbose.
Same for the third one, which is equivalent to
ArrayList<String> list = new ArrayList<String>();
and thus strictly equivalent to the second one.
You should prefer the first one, for the reasons mentioned in the answers to the following question: List versus ArrayList as reference type?
ArrayList vs List in C#
Yes, pretty much. List<T>
is a generic class. It supports storing values of a specific type without casting to or from object
(which would have incurred boxing/unboxing overhead when T
is a value type in the ArrayList
case). ArrayList
simply stores object
references. As a generic collection, List<T>
implements the generic IEnumerable<T>
interface and can be used easily in LINQ (without requiring any Cast
or OfType
call).
ArrayList
belongs to the days that C# didn't have generics. It's deprecated in favor of List<T>
. You shouldn't use ArrayList
in new code that targets .NET >= 2.0 unless you have to interface with an old API that uses it.
Related Topics
How to Sort an Array of Ints Using a Custom Comparator
How to Replace Case-Insensitive Literal Substrings in Java
Parsing Iso 8601 Date Format Like 2015-06-27T13:16:37.363Z in Java
Creation Timestamp and Last Update Timestamp with Hibernate and MySQL
Test If a String Contains Any of the Strings from an Array
Setting Default Values for Columns in JPA
Jaxb: How to Ignore Namespace During Unmarshalling Xml Document
How to Sort a List by Different Parameters at Different Timed
Invoking a Static Method Using Reflection
What Happens When a Duplicate Key Is Put into a Hashmap
Log4J Redirect Stdout to Dailyrollingfileappender
How Should Equals and Hashcode Be Implemented When Using JPA and Hibernate
What's the Advantage of a Java Enum Versus a Class with Public Static Final Fields
String to String Array Conversion in Java
Warning - Build Path Specifies Execution Environment J2Se-1.4