Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference in size of reference variables in Java. (List vs ArrayList)

I got into an argument with a fellow worker that apart from the advantages List interface has over ArrayList<> for reference variables, we also use List<> interface for an object reference because it is lightweight and would take less memory compared to ArrayList<>.

Is my assumption wrong? Do both List and ArrayList occupy the same amount of space?

I tried searching for reference variables size, but nothing whetted my appetite. I'm not looking for the size of the reference variable in different VMs, but is there a difference in size between different reference variables in the same VM.

Please comment on the question if it is a duplicate, so I can remove. I have searched and read from multiple resources and couldn't come to a conclusion

like image 442
Vineeth Chitteti Avatar asked Jan 01 '23 12:01

Vineeth Chitteti


1 Answers

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, or returnAddress. A pair of local variables can hold a value of type long or double.

(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.

like image 60
T.J. Crowder Avatar answered Jan 17 '23 22:01

T.J. Crowder