Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the data array in java.util.ArrayList package-private?

Tags:

In the java.util.ArrayList class, the object array for the list's elements is defined as package-private:

transient Object[] elementData; // non-private to simplify nested class access 

The comment states that the reason why this field is not private is easier access in nested classes. However, nested classes can access private data of the enclosing class just fine. So why is elementData not private? Is there something happening in the background (e.g., at compilation time)?

like image 655
Andreas Schörgenhumer Avatar asked Apr 29 '21 07:04

Andreas Schörgenhumer


People also ask

Can we store multiple data types in ArrayList?

Unlike simple arrays, an ArrayList can hold data of multiple data types. It permits all elements, including null . Elements in the ArrayList are accessed via an integer index. Indexes are zero-based.

What is array and ArrayList in Java?

Array is a fixed length data structure whereas ArrayList is a variable length Collection class. We cannot change length of array once created in Java but ArrayList can be changed. We cannot store primitives in ArrayList, it can only store objects. But array can contain both primitives and objects in Java.

What is an ArrayList in Java?

An ArrayList is a re-sizable array, also called a dynamic array. It grows its size to accommodate new elements and shrinks the size when the elements are removed. ArrayList internally uses an array to store the elements. Just like arrays, It allows you to retrieve the elements by their index.

What is the difference between ArrayList and array in Java?

Java ArrayList The ArrayList class is a resizable array , which can be found in the java.util package. The difference between a built-in array and an ArrayList in Java, is that the size of an array cannot be modified (if you want to add or remove elements to/from an array, you have to create a new one).

Where are ArrayList objects stored in Java?

Since ArrayList can’t be created for primitive data types, members of ArrayList are always references to objects at different memory locations (See this for details). Therefore in ArrayList, the actual objects are never stored at contiguous locations.

Does ArrayList support primitive data types in Java?

However, ArrayList only supports object entries, not the primitive data types. Note: When we do arraylist.add (1) than it converts the primitive int data type into an Integer object which is as illustrated in below example

How to create an ArrayList in Java?

The ArrayList class consists of various constructors which allow the possible creation of the array list. The following are the constructors available in this class: 1. ArrayList (): This constructor is used to build an empty array list. If we wish to create an empty ArrayList with the name arr, then, it can be created as: 2.


2 Answers

When you access a private field from a nested class, the compiler actually generates a synthetic accessor method that is package-visible, and then uses that for the access. It can't access the private member directly, so to avoid that indirection you can make the member package-visible instead.

Here's an answer with more details.

like image 143
Pezo Avatar answered Oct 12 '22 13:10

Pezo


That comment is outdated. With the introduction of this JEP, there will be no syntactic method created by the compiler anymore; and that was introduced in jdk-11.

Before that change, the problem for such a highly used structure like ArrayList, was that another method in the call-stack (for accessing that private field) could potentially have a high cost in critical paths. To get aways from calling one more method, you could declare the field without private.

like image 21
Eugene Avatar answered Oct 12 '22 15:10

Eugene