Definition:

An array in Java is a collection of elements, all of the same data type, stored in contiguous memory locations. Arrays are a fixed-size, ordered collection of elements that can be primitive data types (like int, char, float) or objects (like String). The size of the array is determined at the time of creation and cannot be changed later.

Example:

public class ArrayExample {
public static void main(String[] args) {
// Declaring and initializing an array of integers
int[] numbers = {1, 2, 3, 4, 5};

// Accessing elements in the array
System.out.println("First element: " + numbers[0]);
// Output: 1

// Modifying an element
numbers[2] = 10;
System.out.println("Updated array: ");
for (int i = 0; i < numbers.length; i++) {
System.out.print(numbers[i] + " ");
}
// Output: 1 2 10 4 5

// Creating an array of strings
String[] fruits = new String[3]; // Array size is 3
fruits[0] = "Apple";
fruits[1] = "Banana";
fruits[2] = "Mango";

// Looping through the string array
System.out.println("\nFruits:");
for (String fruit : fruits) {
System.out.println(fruit);
}
}
}

Over all Output:

First element: 
1
Updated array:
1 2 10 4 5
Fruits:
Apple
Banana
Mango

Features of Arrays in Java:

  • The size of the array is fixed at the time of creation and cannot be changed dynamically. If you need a dynamic array, you can use classes like ArrayList .
  • Arrays use zero-based indexing, meaning the first element is at index 0, the second element is at index 1, and so on.
  • All elements in the array must be of the same data type (either all int, all String, etc.).
  • Arrays allow efficient access to elements using their index, which makes them faster for lookups and traversals.
  • Elements of an array are stored in contiguous memory locations, making it easier to access them via their index and providing better performance.
  • Java supports both one-dimensional and multidimensional arrays (like 2D arrays or arrays of arrays).
  • When an array is created, elements are automatically initialized with default values based on their data type (e.g., 0 for int, null for objects, false for boolean).

Advantages of Arrays in Java:

  • Arrays allow storing multiple values of the same type in a compact format.
  • Arrays provide fast random access to elements using their index. Accessing an element at any position has a time complexity of O(1).
  • Arrays are stored in contiguous memory blocks, making them more efficient in terms of memory usage.
  • Arrays make looping through data easy and efficient, as you can use for loops or enhanced for loops (for-each).
  • Arrays form the basis of more advanced data structures such as lists , stacks, and queues.

Disadvantages:

  • Once an array is created, its size cannot be changed. This can lead to wasted memory if the array size is too large or inefficient memory usage if it’s too small.
  • Arrays lack advanced methods for data manipulation (like adding, removing, or sorting elements) that are available in more flexible collections like ArrayList.

Uses of Arrays in Java:

  • Arrays are useful when you know the exact number of elements that need to be stored.
  • Arrays are used to represent data in a tabular format, such as matrices or tables.
  • Arrays are commonly used as buffers in programs where data needs to be temporarily stored before further processing.
  • Arrays are used to handle a large number of objects, such as storing multiple Student or Employee objects in an application.
  • Arrays are often used in algorithms for searching (e.g., binary search) or sorting (e.g., bubble sort, quicksort) because of their efficient access to elements.

Categorized in: