Loading content...
Loading content...
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.
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.
An array is a fixed-size collection of similar data stored under a single variable name.
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.
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.
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.
Easy to store multiple values.
Less code.
Easy to access data using indexes.
Better performance than multiple variables.
Easy to use with loops.
Fixed size.
Can store only one data type.
Inserting or deleting elements is difficult.
Array declaration tells Java that we are creating an array variable.
plaintext
dataType[] arrayName;Example
plaintext
int[] numbers;Another valid syntax
plaintext
int numbers[];Both are correct.
However, the first syntax is recommended.
Creating an array means allocating memory for the elements.
plaintext
arrayName = new dataType[size];Example
plaintext
int[] numbers = new int[5];This creates an array that can store five integers.
Initialization means assigning values to the array.
Example
plaintext
numbers[0] = 10;
numbers[1] = 20;
numbers[2] = 30;
numbers[3] = 40;
numbers[4] = 50;plaintext
int[] numbers = {10, 20, 30, 40, 50};This is the most common way to create an array.
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
50Array 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
100The 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
5When 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
nullSuppose 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 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
50Java 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
50This 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"
ArrayIndexOutOfBoundsExceptionExample
plaintext
int[] numbers = null;
System.out.println(numbers.length);Output
plaintext
NullPointerExceptionUse 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.
An array is a fixed-size collection of elements of the same data type stored under a single variable.
The first index is 0.
No. An array can store only one type of data.
No. The size of an array is fixed after it is created.
length and length()?length is a property used with arrays.
length() is a method used with String.
Example
plaintext
int[] arr = {1, 2, 3};
System.out.println(arr.length);plaintext
String name = "Java";
System.out.println(name.length());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