Arrays in Java | Declaration, Example

Arrays are one of the most basic data structures in Java. In the beginning, Java was built as an interpreted language, where the language itself was interpreted by the computer on each run (which meant that all file system commands were executed at run-time). Before Java was an object-oriented language, it was explicitly built to support only very simple data types: strings, integers, and so on.

Java has no concept of arrays. The closest it has is a syntax for declaring and initializing arrays. The syntax resembles a declaration, but the variable type is not specified.

An array is a variable that contains other variables. In Java, these other variables can be of type Object (which contains methods and fields) or of type Array (which contains values).. Read more about how to declare array in java and let us know what you think.

In Java, an array is a container object that contains a collection or set of items of the same data type.

In other terms, an array is a data structure with a defined size that may store many values of the same data type.

It’s usually used to keep track of a set of components (or values) or a collection of related data items with the same name.

As a result, we may store a collection of strings, int values, or float values in the array. However, we are unable to save certain int or float values in the array.

In Java, the elements of an array are kept in a single memory region. When memory space for an array is allocated, the array size (or fixed) that indicates the number of items must be provided (or fixed).

Once the size of an array is created, it cannot be changed further. That is, an array cannot expand or shrink if we need. Thus, we can store a fixed set of elements in an array.

Why are Arrays required in Java?


We’ve only dealt with variables that hold one value at a time so far. An integer variable, for example, can only store one number, while a string variable may carry a lengthy string of text.

Let’s say there are 60 pupils in a class, and we want to keep track of their roll numbers. Because the roll numbers will be integers, we’ll create 60 distinct variables to store them as follows:

int rollNo; int rollNo1; int rollNo2;… int rollNo59; int rollNo1; int rollNo2; int rollNo3; int rollNo4; int rollNo5; int rollNo6; int rollNo

To save roll numbers in these variables, we’ll need additional 60 statements. Even if you create 120 statements simply to save student roll numbers, the code will be unmanageable and clunky.

Arrays come to the rescue in this scenario. We may define a single variable that can contain all 60 items using an array. That is, we may store as many items of a type as we wish in an array variable of that type.

The benefit of utilizing an array is that it allows us to simplify programming by replacing many statements with only one or two, making it easier to comprehend.

In Java, there are many different types of arrays.


Arrays are often divided into two types:

  1. Arrays with just one dimension (or 1D array called one-dimensional array)
  2. Arrays with many dimensions (or 2D, 3D arrays)

We will focus on one-dimensional arrays in this lesson, and multi-dimensional arrays will be discussed in the next tutorial.

In Java, a one-dimensional array (sometimes known as a single-dimensional array)


A one-dimensional array is a collection of similar-type variables that may be retrieved using a common name. A variable in an array is referred to as an array element.

An array name is used to represent each element in the array, with the index or subscript enclosed in square brackets [ ]. There can only be one complete row or column of items in a single-dimensional array. To learn more about array, look at the diagram below:

Arrays in Java

The integers contained in square brackets are referred to as index values or array subscripts. x[0], x[1], x[2], x[3],…,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

These integers identify the location of items inside an array, allowing us to retrieve elements directly from the position.

Because an array is an index-based structure, the array’s first element is kept at index 0, the second member at index 1, and so on.

Every array object in Java has memory allocated to it. Elements in array lists, such int, float, and char, take up memory space as memory addresses based on their individual data values.

Consider the illustration above. Each index position generates an own memory location. For example, x[0] has a memory address of 3020, x[1] has a memory address of 3022, and so on.

An attempt to use an index that is less than zero or higher than or equal to the length of the array produces an exception called IndexOutOfBoundException, which is verified at runtime.

Declaration of Array Variables in Java


Instead of defining separate variables for each value, such as num0, num1, num2, and so on, an array holds many values of similar type in a single variable.

To utilize a one-dimensional array in a Java application, we must define a variable to refer to the array and describe the element type of the array. In Java, the typical syntax for defining an array variable is as follows:

elementType arrayname[ ], elementType arrayname[ ], elementType arrayname[ ], elementType arrayname[ ], elementType arrayname[ ], elementType arrayname[ ], elementType arrayname[ ], elementType

elementType denotes the data type, which may be any basic data type such as int, char, float, Double, and so on, as well as a String or an object. An identifier is represented by arrayname. The array’s items will all have the same data type.

The following code, for example, defines multiple array variables with various data types:

month days[ ], int month days[ ], int month days[ ], int month days[ ], int month days[ ], Multiple int values may be stored in month days. String[ ] name; / name may contain many references to String objects. int number[ ]; float average[ ]; int[ ] counter; int[ ] counter; int[ ] counter; int[ ] counter; int[ ] counter; int[ ] counter; int[ ] counter; int emp can contain multiple references to Employee objects. int[ ] empId; Employe[ ] emp; / emp can hold multiple references to Employee objects.

There is no array in the following definition of array variables. In reality, the value of all array variables is null, indicating that the array is empty.


Note:

A reference variable that refers to an array object is known as an array variable. A reference to an array object is stored in the memory allocated to an array variable. It does not, in fact, hold an array.

b) Strictly speaking, an array and an array variable are different, but most of the time the distinction can be neglected. Thus, it is all right to say, for simplicity, that month_days is an array, instead of stating that month_day is a reference variable that contains a reference to an array of multiple int values.

In Java, you may create an array.


The declaration of an array variable, unlike the creation of a basic data type variable, does not allocate memory to the array. It simply allocates memory for the reference to an array.

The value of an array variable is null if it does not have a reference to an array. We can’t add values or data to an array unless it’s been formed first.

Let’s look at how to make an array object of a certain type now.

An array is an object in Java. Because every object in Java is a member of a class, we can use the new keyword to construct an array object and give its reference to the variable.

The following is the typical syntax for creating an array object:

new datatype[arraySize] = arrayname;

arraySize indicates the number of items in the array that we wish to store in it in this syntax. An array variable connected to an array is called arrayname.

The syntax above accomplishes two goals:

(1) A new datatype[arraySize] is used to construct an array.

(2) It assigns the array variable arrayname the reference to the freshly formed array.

Declaring an array variable, generating an array, and assigning an array object reference to the variable may all be done in a single sentence, as seen below:

Or, datatype arrayname[ ] = new datatype[arraySize];

Here’s an example of a statement like this:

1) num = new int[5]; / It can hold up to five integer items in an array. The length or size of the array is 5. 2) double[ ] myList = new double[10]; / It can hold ten double-type items.

One-dimensional arrays are used in both statements (linear). They define two array variables, num and myList, and then construct an array of five and ten int and double types, respectively, and assign their references to num and myList.

The first array has 5 items (integer data), whereas the second array has 10 elements (integer data) (double data). That means, they are 5 and 10 years old. They may, however, dynamically add and remove components.

Because each member of the num array is an int variable that requires 4 bytes in memory, the whole array will need 20 bytes in memory, plus 4 bytes for the num variable to hold the array’s reference in memory.

When an array is built in this way, all of its entries are automatically populated with a default value.

Similarly, each member of the myList array is a double-type variable that requires 8 bytes in memory; the whole array requires 80 bytes in memory, plus 8 bytes for the myList variable to hold the array’s reference in memory.

To further understand, look at the diagram below.

While creating an array, we may additionally declare an expression to specify the length of the array:

/ arr1 contains 10 items. int length = 10; int[ ] arr1 = new int[length]; arr2 = new int[length * 2] int[ ] arr2 = new int[length * 2] int[ ] arr2 = new int[length * 2] int[ ] arr2 = new in

Because all arrays in Java are objects, their references may be given to an Object reference variable. Consider the following scenario:

new int[5]; int sId[ ] = int sId[ ] = int sId[ ] = int sId[ ] = int sId obj = sId[ ]; / this is a legal assignment.

In this case, we have an array reference in an Object reference variable. Before accessing items by index or assigning it to a new array reference variable, we must convert it to an appropriate array type. The following is the general syntax:

int[ ] arr = (int[ ]) obj; / obj is an Object reference variable that contains an int[ ] reference.

The Benefits of Arrays in Java


Arrays provide a number of benefits in Java. The following are the details:

1. The primary benefit of an array-based structure is that it arranges data in a manner that makes manipulation simple.

2. At every point in time, we may access the nth element of an array at random.

3. Because no variable is required to enable navigation, arrays take significantly less space.

4. Arrays optimize the coding, allowing us to access or sort data more quickly.

Arrays in Java have several drawbacks.


The following are some of the drawbacks of arrays in Java:

1. Because arrays have a fixed size, we can only store items of that size in them.

2. An array’s size cannot be altered after it has been formed. That is, an array cannot grow or decrease in size during execution. To address this issue, Java developed the Collections framework, which grows on its own.

3. An array cannot include various types of items. An array of an Employee class, for example, will only include Employee type objects. Only int values will be stored in a basic array of int.


I hope this article has covered all of the key aspects about arrays in Java and how to declare them using examples. I hope you’ve grasped the fundamental principles of arrays. Thank you for taking the time to read!!!

The java.util package contains a lot of different array implementations. As a result, most of the time you will encounter an array in the wild, it will come from the java.util package.. Read more about java array methods and let us know what you think.

Frequently Asked Questions

What is an array in Java with example?

An array is a list of values that are stored in order. For example, the following code creates an array with two elements: int[] myArray = new int[] { 1, 2 };

What is an array with example?

An array is a list of values that are stored in the same variable. It can be used to store multiple items in one place.

What are some examples of arrays?

An array is a list of values that are stored in the same location.

Related Tags

This article broadly covered the following related topics:

  • java array methods
  • java array example
  • array java
  • types of array in java
  • java initialize array with values