How To Declare An Array In JAVA

How To Declare An Array In JAVA

Well, in order to use an array for storing data of similar data types, we need to first declare/create an array. This is a pretty much easy task, and you will surely understand it and get used to it in no time. This is similar to how we create normal variables. We just give the data type, some name to that variable, and also assign some value to the variable. You can refer to the previous programs wherever we have created some variables.

So, here is how you create an array –

type[] arrayVariable = new type[size];

Here, the type specifies the data type of the array. This is like if you want to store some integers, you can create an integer array by using the datatype int. the array variable is just the name of the reference variable and size defines the number of elements that you are going to store in the array. Note that the square brackets after the datatype are telling that this is going to be an array. 

Well, just to mention that the arrays are static, which means that you allocate only a specific amount of memory to the array, meaning that if I have created an array to store 10 integers, I have allocated memory only for 10 integers and I cannot store the 11th. This is kind of straightforward and also makes sense.

While creating an array, the memory is allocated in the heap, and the reference variable is sitting in the stack pointing towards the array object created in the heap. You will get this as we move ahead.

Also, one thing to mention before we move further, I have already mentioned before that the array can be used to store data of similar data types. This does mean that if I am creating an integer array, then I am only supposed to store a specified number of integers in it. It is not the case that you will store some integers, and some Strings with it, no… it won’t work that way.

So, below is an example of how are we going to create an array for different datatypes-

int[] myIntArray = new int[5]; // this creates an array for storing the data of int type

byte[] myByteArray = new byte[5]; // this creates an array for storing the data of byte type

short[] myShortArray = new short[5]; // this creates an array for storing the data of short type.

long[] myLongArray = new long[5]; // this creates an array for storing the data of long type.

float[] myFloatArray = new float[5];// this creates an array for storing the data of type float.

double[] myDoubleArray = new double[5];// this creates an array for storing the data of type double

char[] myCharArray = new char[5]; // this creates an array for storing the data of character type.

boolean[] myBooleanArray = new boolean[5]; // this creates an array for storing the data of type boolean.

Whatever type array you create, all the fields of the array would be initialized with the default value of that particular data type, if you have created an integer array using the above-stated instruction, then all the allocated fields have been initialized to the default value for integers, which is 0. For booleans, it is false, and so on.

Well, when we did use the new keyword, we created an array object, with the ability to store integers. To simply say, we have allocated memory of 5 integers in the heap. But here is one question. We have created the array now, but how are we going to access those memory locations? By this, we mean that how are we going to input into the array, and also after we are done with the input, how are we going to access the elements?

Well, earlier, we used the variable names to access the values. But here, we are going to make use of something called an index.

An index is just a number defining the position of some particular element. This is just like we have an index in the front of our books so that we can access some particular page/lesson/concept. Well, this does not relate directly to the index here. Here, the index is the position of that element, and we can access some particular element with the help of its index.

This is pretty much easy and straightforward as we will see and implement ahead.

It has to be noted that the array index starts from zero. This means that the first element in the array is present at position zero. Let’s now see how to access these array elements from the array we just created, because we need to put some elements right?

Have a look at the below program, where we can understand how to access the individual array elements →

public class IntegerArray {
public static void main(String[] args) {
// lets follow the syntax as type[] arrayname = new type[size]; to create an integer array
int[] myIntArray = new int[5];
// We can access the individual array elements with help of array index.
// The index starts from zero.
myIntArray[0] = 12; // at the zeroth position, we are storing the element 12.
myIntArray[1] = 15;
myIntArray[2] = 10;
myIntArray[3] = 7;
myIntArray[4] = 98;
System.out.println(myIntArray[3]);
}
}

Well, we now know that we can access the array of elements with the help of an index. Try running this program, and you can find that we did access the individual array elements and stored some integer values. The last line prints the element present at the 3rd index, which is the 4th element in the array. But how about 4th? Well, if we start counting from the zeroth index element as the first element, the index 3 element comes out to be the 4th one. This might be confusing to understand, but you will soon get used to it so you don’t have to worry.