What are Arrays in Java?

An array is a type of data structure that can store a fixed number of elements of the same data types in our RAM in contiguous order, and Arrays in Java are no exception. It has the same features as any other array except that in Java, an array is considered to be an object that stores elements of the same data type and a fixed size.

Arrays are usually declared so that we can store multiple values in one go and we don’t have to declare and initialize each value separately.

Let’s take an example to understand the above-said line. Let’s say in a class there are 100 students and we want to store their names in our system for the class record.

One way is to use a data type string and declare and initialize 100 different variables to store them. A better approach will be to make an array of size 100 and the same data type- in this case, string and store all the names in it.

This is because the former process will consume a lot of time and is repetitive, while the latter method is much better as no time is wasted and with one declaration all the values have been initiated and declared, and even stored.

String[] Class = new String[100];// declaring and allocating a memory of 100 stringsClass = {"Ateev", "Arun", "Rohit" ..... "Subash"};// initializing elements

In the above example, we have created an array of fixed size (100) which is equal to the amount of data we have to store in it.

Indexing of Arrays in Java

In most programming languages (Java included), the indexing of arrays starts from 0 and goes to 1 less than the size or length of the array.

Each item in an array is called an element, and each element is accessed by its numerical index. As shown in the image, indexing begins at 0 and ends at 8 which is 1 less than its length.

Declaring Arrays in Java

In Java, there are 2 ways in which we can declare an array.

int[] A;ORint A[];

When it comes to declaring an array or a variable in java, the syntax is the same. They both need a data type so that the JVM can tell which type of values will be stored and a name that can be anything we want, provided that it follows the rules of naming a variable in Java.

In the above syntax int is the data type or primitive data type to be more specific and A is the name of the array the empty braces symbolize that the variable holds an array whose size is yet to be determined.

We cannot provide the size of the array in the declaration part as the Java compiler will throw an error saying unexpected token/identifier, etc.

As with variables, the declaration does not create an array; it has just created a reference of that array which tells the compiler that this variable will hold an array of the specified type. To link this Array with the actual physical array of the same type, we must allocate one using the new keyword and assign it to typeArray.

To keep the syntax of Array easy to read and understand and similar to the syntax of declaring a variable, the first syntax — int[] A; is commonly used, and the empty brackets brackets identifies the array type and should appear with the type designation.

Similar to int there is another data type as well which can be used to declare an array.

byte[] A;           // an array of Bytesshort[] A;          // an array of Shortsint[] A;            // an array on Integerslong[] A;           // an array of Longfloat[] A;          // an array of Floatsdouble[] A;         // an array of Doublesboolean[] A;        // an array of Booleanchar[] A;            // an array of CharactersString[] A;         // an array of Strings

Continue Reading.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ateev Duggal

I am a front-end developer from India with over one year of experience in freelancing with skills like Git, HTML5, CSS3, Bootstrap 4&5, and React.