Arrays In Java Tutorials
Programming
Arrays in Java
Introduction
An array is one of the most commonly used data structures in Java. It allows us to store multiple values of the same data type in a single variable.
Instead of creating many variables, we can store all related values in one array.
Arrays make our code simpler, cleaner, and easier to manage.
What is an Array?
An array is a collection of elements of the same data type stored in contiguous memory locations.
Each element in an array is identified by an index.
The index of an array always starts from 0.
Simple Definition
An array is a fixed-size collection of similar data stored under a single variable name.
Why Do We Need Arrays?
Imagine you want to store the marks of five students.
Without an array, you have to create five variables.
plaintext
int marks1 = 85;
int marks2 = 90;
int marks3 = 78;
int marks4 = 88;
int marks5 = 95;This approach becomes difficult when the number of students increases.
Using an array, we can store all marks in a single variable.
plaintext
int[] marks = {85, 90, 78, 88, 95};This is easier to read and manage.
Real-Life Example
Imagine a classroom with 30 students.
Without an array, you create 30 separate variables.
plaintext
student1
student2
student3
...
student30Using an array, all students are stored in one variable.
plaintext
students[]
↓
Rahul
Amit
Riya
Priya
NehaThis saves time and reduces code.
Features of Arrays
Stores multiple values in one variable.
Stores elements of the same data type
Uses index numbers to access elements.
Fast data access.
Fixed size after creation.
Advantages of Arrays
Easy to store multiple values.
Less code.
Easy to access data using indexes.
Better performance than multiple variables.
Easy to use with loops.
Limitations of Arrays
Fixed size.
Can store only one data type.
Inserting or deleting elements is difficult.
Array Declaration
Array declaration tells Java that we are creating an array variable.
Syntax
plaintext
dataType[] arrayName;Example
plaintext
int[] numbers;Another valid syntax
plaintext
int numbers[];Both are correct.
However, the first syntax is recommended.
Array Creation
Creating an array means allocating memory for the elements.
Syntax
plaintext
arrayName = new dataType[size];Example
plaintext
int[] numbers = new int[5];This creates an array that can store five integers.
Array Initialization
Initialization means assigning values to the array.
Example
plaintext
numbers[0] = 10;
numbers[1] = 20;
numbers[2] = 30;
numbers[3] = 40;
numbers[4] = 50;Declaration, Creation and Initialization Together
plaintext
int[] numbers = {10, 20, 30, 40, 50};This is the most common way to create an array.
Accessing Array Elements
Array elements are accessed using their index.
Example
plaintext
public class Main {
public static void main(String[] args) {
int[] numbers = {10, 20, 30, 40, 50};
System.out.println(numbers[0]);
System.out.println(numbers[2]);
System.out.println(numbers[4]);
}
}Output
plaintext
10
30
50Updating Array Elements
Array values can be changed.
Example
plaintext
public class Main {
public static void main(String[] args) {
int[] numbers = {10, 20, 30};
numbers[1] = 100;
System.out.println(numbers[1]);
}
}Output
plaintext
100Array Length
The length property returns the number of elements in an array.
Example
plaintext
public class Main {
public static void main(String[] args) {
int[] numbers = {10, 20, 30, 40, 50};
System.out.println(numbers.length);
}
}Output
plaintext
5Default Values in Arrays
When an array is created using the new keyword, Java automatically assigns default values.
Example
plaintext
int[] numbers = new int[5];The values are
plaintext
0
0
0
0
0For boolean arrays
plaintext
boolean[] status = new boolean[3];Default values
plaintext
false
false
falseFor String arrays
plaintext
String[] names = new String[3];Default values
plaintext
null
null
nullMemory Representation
Suppose we create the following array.
plaintext
int[] numbers = {10, 20, 30, 40, 50};Memory representation
plaintext
Index
0 1 2 3 4
+----+----+----+----+----+
|10 |20 |30 |40 |50 |
+----+----+----+----+----+Each element has a unique index.
Traversing an Array
Traversing means visiting every element of the array.
Using a for loop
plaintext
public class Main {
public static void main(String[] args) {
int[] numbers = {10, 20, 30, 40, 50};
for (int i = 0; i < numbers.length; i++) {
System.out.println(numbers[i]);
}
}
}Output
plaintext
10
20
30
40
50Using Enhanced for Loop
Java provides a simpler way to traverse arrays.
Example
plaintext
public class Main {
public static void main(String[] args) {
int[] numbers = {10, 20, 30, 40, 50};
for (int number : numbers) {
System.out.println(number);
}
}
}Output
plaintext
10
20
30
40
50Common Errors
ArrayIndexOutOfBoundsException
This exception occurs when we access an invalid index.
Example
plaintext
int[] numbers = {10,20,30};
System.out.println(numbers[5]);Output
plaintext
Exception in thread "main"
ArrayIndexOutOfBoundsExceptionNullPointerException
Example
plaintext
int[] numbers = null;
System.out.println(numbers.length);Output
plaintext
NullPointerExceptionBest Practices
Use meaningful array names.
Always check the array length before accessing elements.
Use enhanced for loops when you only need to read values.
Avoid accessing invalid indexes.
Use arrays only when the size is fixed.
Interview Questions
What is an array?
An array is a fixed-size collection of elements of the same data type stored under a single variable.
What is the first index of an array?
The first index is 0.
Can an array store different data types?
No. An array can store only one type of data.
Can the size of an array be changed after creation?
No. The size of an array is fixed after it is created.
What is the difference between length and length()?
lengthis a property used with arrays.length()is a method used withString.
Example
plaintext
int[] arr = {1, 2, 3};
System.out.println(arr.length);plaintext
String name = "Java";
System.out.println(name.length());Summary
In this chapter, you learned:
What an array is
Why arrays are used
Features of arrays
Advantages and limitations
Array declaration
Array creation
Array initialization
Accessing elements
Updating elements
Array length
Default values
Traversing arrays
Common exceptions
Best practices