This post explores the creation and utilization of arrays in the Java programming language. An array in Java is a data structure that stores elements of a similar datatype in contiguous memory locations. It provides a systematic way to store and access elements of the same type. Java arrays have a fixed size, and each element is assigned an index, starting with 0 for the first element. This section will delve into the process of creating and working with arrays in Java.
Java Array Declaration
In Java, declaring an array object mirrors the process of declaring a Java variable. The array’s element type and variable name are specified, with square brackets [] denoting it as an array. An array declaration comprises two components: the type and the name. The type specifies the element type of the array, determining the data type for each array element.
There are three approaches to declaring an array in Java, and each method involves a distinct placement of square brackets. The ensuing example illustrates the three types of Java array declarations.
package com.test; public class JavaArray { int[] age; int []yearofbirth; int yearofjoin[]; public static void main(String[] args) { } }
Multi Dimensional Array Declaration
The declaration of a multidimensional array in Java varies based on the placement of square brackets. Multiple square brackets indicate a multidimensional array, with each bracket representing a single dimension. The following example illustrates the declaration of a multidimensional array in Java.
package com.test; public class JavaArray { int[][] marks; int[] studentmarks[]; int[] []studentsubjectmarks[]; double salary[][]; double [][]employeesalary; double []empsalary[]; double[] []esalary; public static void main(String[] args) { } }
Java Array Initialization
The array can be initialized by using the new
keyword, specifying the data type, and enclosing the size in square brackets. It is essential that the initialization data type matches the array data type declared. Memory is allocated for the declared number of elements based on the declared array’s size. Since the size is not mentioned in the declaration, the size specified in the square brackets during array initialization will be used to create the Java array memory. The following example demonstrates how to initialize a Java array.
package com.test; public class JavaArray { public static void main(String[] args) { int[] age; age= new int[10]; age = new int[] {1,2,3}; } }
Assigning Value in Array
Values in an array can be assigned similarly to any Java variable. To assign values to a Java array, the index is specified within square brackets. The array can also be assigned to another array variable. There are various ways to assign values to an array, and the following example illustrates different approaches to assigning values to an array.
package com.test; public class JavaArray { public static void main(String[] args) { int[] age; // method 1 age= new int[10]; age[0] = 1; age[1] = 2; //method 2 age= new int[] {1,2,3}; //method 3 int[] number = {1,2,3}; } }
Accessing value in Java Array
A Java array can be used similarly to any other Java variable. The array’s index is used to access a specific element within the array. The index, specified within square brackets, is followed by the name of the array variable. The entire array, along with all its elements, can be accessed using the array variable. The following example demonstrates how to access a Java array and its elements.
package com.test; public class JavaArray { public static void main(String[] args) { int[] age = new int[] {1,2,3}; System.out.println(age[0]); System.out.println(age[1]); System.out.println(age); } }