Array in Java

In this section, we will concentrate on the core part of Java which is Array in Java. Array in Java is the basic foundation block which help programmer to write complex application easily.

Objectives:

  • What is Array?
  • Store and Retrieve from an Array
  • Create an Array

Array Introduction:

Array is the foundation concept of java on which the entire java programming language is developed.  So every programmer should understand this concept very carefully.

Array is nothing but a data structure to store an element. If you want to deal with a group of objects, Array will help you to solve the problem. Array has different component while implementing. When Array is created, JVM (Java Virtual Machine) uses different memory storage to store different component of the array. We will discuss more in this section on this.

Now we have a high level visual for an Array. Now we can discuss on array in detail.

Array is nothing but a data structure to store an element. It is a linear homogeneous data structure. In other word, we can say that this is a collection of similar type of element. We can access each element of an array through an index. Index in array starts with 0 & continues to 1, 2, 3, 4 and so on.

To create an we need to use “new” keyword.

Syntax:

Data_type    variable_name[] = new Data_Type[Size]
Data_type []   variable_name = new Data_Type[Size]

Example

Int arr[] = new int[2]
Int[] arr = new int[2]

Int = Data Type of an Array

arr = Name of the Array variable

[ ] = Indicates that this is an array

new = Keyword

2(Size) = This specifies the number of elements in array.

You can see we have created an array in 2 formats. One is Int arr[] = new int[2] and other is

Int[] arr = new int[2]. Both are correct. But we need to mention [] symbol either with the variable name or with the data type to specify that this is an array. As we have used only one [] symbol while creating an array, this is called one dimensional array.

If you want to create an array of 10 element then you need to give the size as 10.

Let’s have a look on the array creation example.

Example:

class Try{
	public static void main(String args[]){
		int[] arrDemo = new int[3];
		System.out.println(arr[0]);
		System.out.println(arr[1]);
		System.out.println(arr[2]);
	}	
}

Output:

0
0
0

Explanation:

In the above example, we have seen that we have created array variable of name arrDemo with size 3. When we print the first element with 0th  index, it will print as 0. Same as first index, for 2nd and 3rd index this is showing 0 also. As we have not assigned any values to array, it will store the default value to the every element of an array which is 0. That’s why we are getting 3 zero in output.

Now we have seen the simple way to create an array. Let’s understand on array creation in granular level.

In general, we can create the array in 3 steps.

  • Declaration
  • Construction
  • Initialization
Arrays in Java

Declaration:

Declaration is the initial part of the array creation from which signature of array will be declared. It conveys the name of the array and type of the elements which is stored in array. For example:

  • Int[] arr   // Integer array variable
  • float[] arr // Float array variable
  • double[] arr // Double array variable
  • char[] arr // Character array variable

From the above list you can notice that we have declared the type of the array with [] e.g int, float, double, char and the name after the type of the array which is arr.

Construction:

After we declare the type and name of the array, we need to construct the array so that we can allocate the memory for the array to store. We can construct the array with “new” keyword.

Initialization:

This is the final step for creation of an array. During Initialization, Array are initialized with the default values.

Element typeDefault Value / Initial Value
byte0
int0
float0.0f
char‘\u0000’
short0
long0L
double0.0d
booleanFalse
referencenull

The above table signifies the default / Initial value of an array initialization. I we will assign some values to the element of an array with the index then it will overwrite the default value. Let’s understand with a simple example.

Example:

class Try{
	public static void main(String args[]){
		int[] arr = new int[3];
		System.out.println("Values before assignment: "+ arr[0]);
		System.out.println("Values before assignment: "+arr[1]);
		System.out.println("Values before assignment: "+arr[2]);
		
		arr[0] = 75;
		arr[1] = 5;
		arr[2] = 90;
		System.out.println("Values after assignment: "+arr[0]);
		System.out.println("Values after assignment: "+arr[1]);
		System.out.println("Values after assignment: "+arr[2]);
 	}
}

Output:

Values before assignment: 0
Values before assignment: 0
Values before assignment: 0
Values after assignment: 75
Values after assignment: 5
Values after assignment: 90

Explanation:

From the above example you can notice that before assigning any values if we will print the value of an each element then the default value will be printed. But when we assign the values to the array then it will overwrite the default values and print the new assigned value.

In next section we will discuss more on Memory allocation of an array and multi dimensional array.

Summary:

  • Array in Java is nothing but a data structure to store an element.
  • When Array is created, JVM (Java Virtual Machine) uses different memory storage to store different component of the array.
  • We can create the Array in Java in 3 steps. Declaration, Construction & Initialization.
  • Declaration is the initial part of the array creation from which signature of array will be declared. It conveys the name of the array and type of the elements which is stored in array.
  • After we declare the type and name of the array, we need to construct the array so that we can allocate the memory for the array to store. We can construct the array with “new” keyword.
  • This is the final step for creation of an array. During Initialization, Array are initialized with the default values if no value assigned.

Leave a Reply

Your email address will not be published. Required fields are marked *