Last Updated: 19 November, 2023
An array is an object used to store collections of elements of the same type. The length of an array is established when the array is created. Once an array is created, its size cannot be changed. An array allocates contiguous memory locations for each element in Java.
An array in Java is index-based; each element is accessed by its unique numerical index value, which starts at 0, which means the first element of the array will be stored at the 0th index position, the second element will be stored at the 1st index position, and so on. Using the index number, we can directly access any random value from the array.
Declare an array in Java
In Java, here is how we can declare an array.
Java Array Declaration Syntax:
Let's see the array implementation with the help of given example.
Example 1 : Java Array Declaration, Instantiation and Initialization
Output
Index: 0, Element: A
Index: 1, Element: B
Index: 2, Element: C
Index: 3, Element: D
Index: 4, Element: E
Index: 5, Element: F
Index: 6, Element: G
Index: 7, Element: H
Index: 8, Element: I
Index: 9, Element: J
As given above example, here we are declaring 10 characters size of Array which means Array length is 10 and storing values like A, B, C, D, E, F, G, H, I, J. Since at the time of array declaration, the character 'A' is declared first and then 'B' character and so on, therefore 'A' will be store at the 0 index position and 'B' will be store at index 1 and so on as we can see in the below image.
Arrays allows to store primitive data types (int, long, char, etc.) and non-primitive data types(Object). In case of primitive data types, the actual values are stored in a memory, whereas in the case of non-primitive, the actual objects are stored in a heap memory.
Java supports mainly two types of Array:
An array with one dimension is called a one-dimensional array or single-dimensional array in Java. A Single Dimensional Array stores the same data-types values in a sequential fashion. Its length is fixed once created and cannot be changed.
A single-dimensional Array allocates contiguous memory locations for each element in Java.
Read more...A multidimensional array is an array of arrays. It has more than one row and it represents the data in a tabular form (Rows and columns).
To access data or elements in the two-dimensional array we use row index and column index.
Read more...That's all guys, hope this Java article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com
Why Array is required?
Suppose if we need to store multiple values of same type such as 10 students name then instead of creating 10 String variable, we will create array of string type whose lengh will be 10.Array makes easy to store and manipulate large data sets. As array is index based, any element can be accessed randomly by its index value.
Difference between Array and ArrayList in Java
Ans. There are following differences between Array and ArrayList:
Array | ArrayList |
---|---|
Array size is fixed. Once declared, its size can’t be changed. | ArrayList size is dynamic and can be increased or decreased in size when required. |
Array can be single-dimensional or multidimensional. | ArrayList can be single-dimensional only. |
An array can hold primitives and reference types (objects) both in Java. | ArrayList can only hold reference types (objects), not primitives. |
Array does not support generics. | ArrayList supports generics. |
Array provides better performance and uses less memory. | When compared to Array, ArrayList performs worse and consumes more memory. |
We can iterate through an array using loops only. | ArralyList provides various ways for iteration, like loops, Iterator and ListIterator class. |
Array elements are stored at contiguous memory locations. | ArrayList elements are not stored at contiguous memory locations. |
Using the assignment operator, we can add elements to the array. | We can add elements to the ArrayList using the add () method. |
Can we set the negative number as an Array size in Java?
Ans. No, the array size can not be negative in Java. If we set the array size as negative, there will be no issue at the compile time. But at runtime, the JVM will throw a NegativeArraySizeException. Let's see the example below.
public class NegativeArraySize { public static void main(String[] args) { int[] num = new int[-5]; System.out.println("Array Length: " + num.length); } }
Output:
Exception in thread "main" java.lang.NegativeArraySizeException: -5 at com.javabytechie.array.NegativeArraySize.main(NegativeArraySize.java:6)