Arrays in Java
Arrays in Java
Part-1
Introduction
In Java, an array is a linear data structure that stores multiple values of the same type in a single
variable. Arrays are objects, and they implicitly inherit from java.lang.Object, allowing use of methods
like toString(), equals(), and hashCode().
Key Features
• Store Primitives & Objects: Arrays can store primitive types (int, char, etc.) and object references
(String, Student, etc.).
CoderBaba Page | 1
Basic Operations
1. Declaring Arrays
int[] arr1; // or
int arr2[];
2. Initializing Arrays
4. Changing an Element
CoderBaba Page | 2
Example: Integer Array
Arrays of Objects
CoderBaba Page | 3
Array Index Out of Bounds
ArrayIndexOutOfBoundsException
Advantages
Disadvantages
CoderBaba Page | 4
Basics Operation on Arrays in Java
General Syntax:
// Method 1:
int arr[];
// Method 2 (preferred):
int[] arr;
Both declarations are valid and functionally equivalent. The second form (int[] arr) is preferred for better
readability, especially when declaring multiple variables.
Element Type
The element type (e.g., int, char, String) determines what type of values the array will store.
Important Note:
At this stage, arr does not point to any actual array. You must use new to allocate memory before using
it.
CoderBaba Page | 5
2. Initialization of an Array in Java
• When you declare an array in Java, only a reference is created — no actual memory for elements
is allocated at that point.
Example:
Key Points
• Memory is always dynamically allocated (on the heap), unlike in C/C++ where static and
dynamic allocation are both possible.
Example:
• Note: Arrays in Java are not dynamically resizable — size must be fixed at creation. For resizable
structures, use ArrayList.
CoderBaba Page | 6
Array Literals in Java
Definition:
An array literal in Java is a shorthand way to declare and initialize an array when both the size and the
values are known at the time of declaration.
Syntax:
Both forms are valid. The second form is shorter and more commonly used.
Key Points:
• The size of the array is automatically determined by the number of elements in the literal.
Invalid Example:
int[] arr;
CoderBaba Page | 7
3. Changing an Array Element in Java
How It Works:
In Java, you can change the value of an array element by assigning a new value to a specific index.
Syntax:
arrayName[index] = newValue;
Example:
arr[0] = 90;
Key Points:
CoderBaba Page | 8
4. Array Length in Java
Description:
In Java, every array has a built-in length property (not a method) that returns the number of elements in
the array.
Syntax:
int n = arr.length;
Example:
int n = arr.length;
Key Point:
• The length value is fixed and equals the number of elements in the array.
Description:
You can access and update all elements of an array using a for loop by iterating through their indices.
Accessing Elements:
CoderBaba Page | 9
Updating Elements:
System.out.println(value);
Note: The enhanced for loop (for-each) is used only for reading values, not updating them directly.
Key Points:
• Use a standard for loop for both reading and modifying elements.
Description:
In Java, you can create arrays of objects just like arrays of primitive data types. Each element in the array
holds a reference to an object, not the object itself.
CoderBaba Page | 10
Example: Array of Student Objects
class Student {
public int roll_no;
public String name;
Key Points:
• Object arrays store references to objects.
• Memory for the array is allocated using:
Student[] arr = new Student[5];
• Each element must be individually initialized using the constructor.
• You can then access object members using the dot (.) operator:
arr[i].roll_no, arr[i].name
CoderBaba Page | 11
Accessing Elements Outside Array Bounds in Java
What Happens?
If you try to access an element outside the valid index range of an array in Java, the JVM throws an
exception:
ArrayIndexOutOfBoundsException
Example:
arr[0] = 10;
arr[1] = 20;
arr[2] = 30;
arr[3] = 40;
Output:
CoderBaba Page | 12
Key Points:
• Always use arr.length in loops and access logic to avoid this error.
Description:
Just like variables, arrays can be passed to methods in Java. When an array is passed to a method, its
reference is passed, meaning any changes made to the array inside the method affect the original array.
Output
Key Points:
• You can pass both primitive type arrays and object arrays to methods.
CoderBaba Page | 13
Passing Arrays to Methods & Returning Arrays from Methods in Java
You can pass arrays to methods just like regular variables. The method receives a reference to the
original array.
Example:
int sum = 0;
sum += arr[i];
Explanation:
In Java, methods can return arrays. This is useful when you want to generate and send back a full array
from a method.
CoderBaba Page | 14
Example:
class Geeks {
Output:
123
• Type Homogeneity: Can only store elements of the same data type.
• Costly Insertions/Deletions: Modifying elements in the middle requires shifting, which can be
inefficient.
CoderBaba Page | 15