Arrays in Java

Arrays in Java

May 11th, 2026
5144
5:00 Minutes

Arrays are fundamental data structures in Java programming language. Java Arrays allow you to store multiple values of the same type in a single variable. It is important to understand them for efficient coding whether you are building a simple or complex application. I have created this guide to help you explore the Java Arrays basics. This includes how to declare, initialize and access them. Let’s get into it.

What are Arrays in Java?

A data structure which has homogeneous elements is called an array. In other words, all the elements in the array are of the same data type. Here is an example -


In the given image, array contains seven elements all of which are integers and of the same type. The light grey box beneath the array represents the indices which always start at 0 and go up to n-1. The indices range from 0 to 6 for this array with seven elements.

Master Java Programming with Java Training

Boost your coding skills and gain hands-on knowledge in Java.

Explore Now

Key Features of Arrays in Java

Here are the key features of Java Arrays -

  • Fixed Length - It is not possible to change the size of an array once it is created.
  • Continuous Memory Allocation - The actual values of arrays are stored in consecutive memory locations when they hold primitive types. Only the object references are stored in consecutive locations, while the actual objects reside elsewhere in the memory.
  • Zero-Based Indexing - Numbering starts at 0 in an array so the first element is stored at index 0.
  • Store Primitives and Objects - Java arrays are capable of holding both primitive data types (char, boolean, int) and objects (String, Integer, etc).
  • Single Identifier for Multiple Events - All elements within the array are accessed with the help of a single identifier. This helps in simplifying code and improving readability.

Also Explore: How to Install Java

Basic operations on Arrays in Java

I have compiled some important techniques for declaring, initializing, and manipulating arrays in Java in this section.

1. Declare an Array in Java

Here is a standard syntax for declaring an array -

int[] numbers;

The statement int[] numbers declares an array called numbers. It can store multiple integer values. At this stage, the array exists as a variable, but no memory has been allocated yet for the actual elements. The array must later be initialized with a size or assigned values to use it.

2. Initialize an Array in Java

Here is a standard syntax for initializing an array -

int[] numbers = {10, 20, 30, 40, 50};

The above declares and initializes an array in a single step. The array numbers are created with five elements, and each position is naturally filled with the given values. The size of the array will depend on the number of elements available inside the curly braces, which is 5 in the above example.

3. Change an Array in Java

Give a new value to a specific index to change an element. The index starts with 0 and ends at -1 (total array size).


// Changing the first element to 90
arr[0] = 90;

4. Array Length

Here is a standard syntax for array length -

int[] numbers = {5, 10, 15, 20, 25};
System.out.println("Length of the array: " + numbers.length);

The array numbers contain five elements in this example. The property. length is used to find the total number of elements in the array. It prints “Length of the array:5” to the console when the code runs.

5. Updating and Accessing Array Elements in Java

All elements of an array can be traversed using a for loop in Java. Each value is accessed by referring to its specific index within the array. Here is a Java program that tells you how to create an integer array, assign values to it and display each element on the console.


class igmGuru {
public static void main(String[] args)
{
// declares an Array of integers.
int[] arr;
// allocating memory for 5 integers.
arr = new int[5];
// initialize the elements of the array, first to last(fifth) element
arr[0] = 2;
arr[1] = 4;
arr[2] = 8;
arr[3] = 12;
arr[4] = 16;
// accessing the elements of the specified array
for (int i = 0; i < arr.length; i++)
System.out.println("Element at index " + i + " : " + arr[i]);
}
}

Output:

Element at index 0 : 2
Element at index 1 : 4
Element at index 2 : 8
Element at index 3 : 12
Element at index 4 : 16

Arrays of Objects in Java

An array of objects is a structure that holds references to many objects of the same class in Java. Object arrays are different from primitive types that store actual values. Object arrays keep memory addresses that point to the real objects stored in the heap.

Example:

Here we are taking a Book class and creating an array of Books with three Book objects in the array. The Book objects have to be instantiated using the constructor of the Book class and their references should be assigned to the array elements.

// Book class
class Book {
String title;
String author;
// Constructor
Book(String title, String author) {
this.title = title;
this.author = author;
}
// Method to display book details
void display() {
System.out.println("Title: " + title + ", Author: " + author);
}
}
// Main class
public class BookArrayExample {
public static void main(String[] args) {
// Creating an array of Book objects
Book[] library = new Book[3];
// Initializing each Book object
library[0] = new Book("Java Basics", "James Gosling");
library[1] = new Book("Effective Java", "Joshua Bloch");
library[2] = new Book("Clean Code", "Robert C. Martin");
// Printing details of each book
for (int i = 0; i < library.length; i++) {
library[i].display();
}
}
}

Output:

Title: Java Basics, Author: James Gosling
Title: Effective Java, Author: Joshua Bloch
Title: Clean Code, Author: Robert C. Martin
Also Explore: How to Learn Java From Scrach

Advantages of Java Arrays

Let’s see what all there is there you can benefit from Java Arrays.

  • Memory Efficiency - Arrays keep items close together in memory. This makes it easy to get aside space for them all at once and helps avoid memory from getting chopped up into tiny pieces.
  • Adaptable - Arrays are useful as they can store different kinds of data including numbers, letters and complicated data like objects with ease.
  • Hardware Compatibility - Arrays usually work with most computer hardware which makes them helpful for coding.
  • Quick Access - It allows you to grab any item as they are stored right next to each other in memory. It means fast and direct access.
Related Article: Java Interview Questions

Disadvantage of Arrays in Java

Here are some drawbacks of Java Arrays to keep in mind.

  • Lack of data type support - Arrays can only hold items of the same ty[e which can be a pain while working with complicated datasets.
  • Inserting and Removing - Adding and deleting items can result in slow down as you have to move the other items around.
  • Not flexible - Arrays are not very flexible because they have a fixed size and do not support many data types. It makes them less adaptable than things like trees or linked lists.

Wrapping Up

It is safe to conclude that Arrays in Java are important building blocks to store and manage multiple values. Understanding how to declare, initialize and manipulate them allows you to handle data in your applications with ease. Mastering arrays is a must for Java developers for working with simple or complicated data structures.

FAQs

Q1. Can arrays in Java be resized?

No, as arrays in Java have a fixed size once they are formed. You can use classes from the Java collections if you need a resizable array.

Q2. What is the time complexity for accessing an element in an array?

Accessing an element by its index in an array takes constant time. This is because arrays in Java are contiguous blocks of memory. This allows direct computation of the memory address of any element using its index.

Q3. How does an array differ from an ArrayList in Java?

An array has a fixed size so length cannot be changed after creating. An ArrayList can grow or shrink in size as needed and it also has easy built-in methods to add or remove elements.

Q4. How to find the length of an array in Java?

You can find the length of an array by using the length property. For eg. array.length shows how many elements are in the array.

About the Author
Author Nehal Sharma
About the Author

Nehal Sharma is a skilled Data Analyst with expertise in Java, mobile development, and data analytics. She transforms complex data into actionable insights and has experience in business intelligence, data science, and Salesforce. She also simplifies technical concepts into clear, engaging content for learners and professionals.

Drop Us a Query
Fields marked * are mandatory
×

Your Shopping Cart


Your shopping cart is empty.