Java Arrays
An array in Java is a data structure that allows us to store multiple values of the same type in a single variable. Instead of declaring separate variables for each value, we can use an array to efficiently manage data.
In this blog, we will cover:
- What is an Array?
- How to Declare an Array
- How to Initialize an Array
- Accessing and Modifying Array Elements
- Array Length
- Looping Through an Array
- Example Programs with Explanations
- Limitations of Arrays
What is an Array?#
An array is a collection of elements stored contiguously in memory. All elements in an array must be of the same data type.
For example, if we want to store the marks of 5 students, instead of declaring multiple variables:
We can use an array:
This creates an array that can store 5 integer values.
How to Declare an Array#
In Java, we can declare an array in two ways:
1. Using the new keyword (Preferred)#
What happens when you declare an array with the new keyword:
- You are telling Java to create a new array object of type
intand size5. - Since
intis a primitive type, all elements in the array are automatically initialized to0(the default value forint).
Internally:
- Memory is allocated for 5
ints. - Values are:
[0, 0, 0, 0, 0].
2. Using Direct Initialization#
What’s going on:
- This is a shorthand syntax for declaring and initializing the array in one step.
- Java automatically infers the size of the array based on the number of values provided (
5here).
Internally:
- A new array of size 5 is created and filled immediately with the given values.
How to Initialize an Array#
We can initialize an array in different ways:
1. Default Initialization#
When an array is declared using new, its elements are automatically initialized to:
0for numeric types (int,double,float, etc.).falseforbooleantype.nullfor object types (String,Integer, etc.).
Output:
2. Explicit Initialization#
Accessing and Modifying Array Elements#

Array elements are accessed using index numbers, starting from 0.
Output:
Array Length#
To find the number of elements in an array, we use .length.
Output:
Looping Through an Array#
We can iterate over an array using:
1. for Loop#
Output:
2. Enhanced for Loop (for-each)#
A simpler way to iterate through an array is using the enhanced for loop.
Output:
Example Programs#
Example 1: Find the Largest Element in an Array#
Output:
Example 2: Calculate the Sum of Array Elements#
Output:
Limitations of Arrays#
- Fixed Size: Once declared, an array’s size cannot be changed.
- Same Data Type: Arrays can only store elements of one data type.
- Inefficient Insertion/Deletion: Adding/removing elements in the middle of an array is slow.
Conclusion#
In this blog, we have learned:
- What arrays are and why they are useful.
- How to declare and initialize arrays in Java.
- How to access and modify array elements.
- Finding the length of an array using
.length. - Looping through arrays using
forandfor-eachloops. - Example programs to find the largest number and sum of elements.
- Limitations of arrays in Java.
Arrays provide a simple and efficient way to store multiple values. However, due to their fixed size, developers often use ArrayLists for more flexibility (which we will discuss in future blogs).