What is an Array in Java? - Programmics! - Programmics! - We Love to Code.
demo image

What is an Array in Java? - Programmics!

Share This
So, you know how to store different types of data types?
Yes, you already know, by using different variables.

But what if we say we have similar data sort of data to be stored, do we need to declare different variables to every single piece of data.

Well, the answer is a big NO, because there comes an Array to help us out. So let's start with ...

What is an Array?

What is an Array in Java?

An Array is a collection of similar sort of elements stored at an adjoining memory location. Arrays can store a fixed number of values under the same name.

The total number of values an Array can store is called the length of an Array. The length is defined at the time of declaration of that Array.

So now when you what is an Array. Let's know how it works!


How does an Array in Java works?

When a program is compiled, it allocates the required memory for the Array in the computer memory. The amount of memory depends upon the number and the data type of the Array. The compiler reserves a fixed block of memory for every expected data element in the array.

Once an Array is declared, it can be initialized any moment by assigning values to it. Every block of memory within the Array has a unique address which can be used to assign the values to the Array.

The same address can also be used to access the particular block of memory. Now, let's jump on to declaring an Array.

How to declare and allocate memory to an Array in Java?

Declaring an Array is quite similar to declaring a variable. While declaring a variable, we specify the data type followed by its name, just like that, while declaring an Array, we specify its data type, then we specify its name and at last, its size.

Allocating Memory to an Array.

Memory is allocated to an Array by using a keyword, called new. We can create and allocate memory to an Array in a single line. 

Let's understand with the help of an example.

Syntax : 

<Data Type> <Array Name> =new<Data Type>[Size]; 

Let's check up this example -

int num []=new <Data Type>[size];

How to Initialize & Access the Array Elements?

Arrays can be initialized at the time of declaration. While initializing an Array, we pass a list of values separated by commas enclosed in curly braces ({ }).

In this technique, we do not need to specify the size of the Array as the Java compiler automatically allocates the memory depending upon the number of elements given in the curly braces.

Accessing the Array Elements.

The elements in an Array are stored in adjacent memory locations. 

They can be accessed using the corresponding index number.

But, when an Array has a vast amount of elements we need to use for loop for that.

Your Turn!

Now it's your turn, tell us what do you like about this amazing way to store a vast amount of data under a single name.
I am always here to help you out.

Thanks for being here!

No comments:

Post a Comment