What are Arrays in Java?
Tags: java, arrays
Published: 2022-05-04 02:10:38

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 strings

Class = {"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.

![Array indexing](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zcr4hei7vcrb6pt1af80.png)
 
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;
OR
int 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.

![error](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5cdbmhnrp64qh5k2i777.png)
 
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 array of syntax easy to read and understand and similar to the syntax of declaring a variable, the first syntax — int[] A; is commonly used as the 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 Bytes
short[] A;          // an array of Shorts
int[] A;            // an array on Integers
long[] A;           // an array of Long
float[] A;          // an array of Floats
double[] A;         // an array of Doubles
boolean[] A;        // an array of Boolean
char[] A;            // an array of Characters
String[] A;         // an array of Strings
```
[Continue Reading](https://tekolio.com/what-are-arrays-in-java-and-how-to-use-them/).