C

Core Java tutorial for beginners

Clean • Professional

Core Java Arrays – Introduction and Basics

3 minute

Arrays in Java

Arrays are one of the most important data structures in Java. They allow you to store multiple values of the same type in a single variable, making your code more organized and efficient.


What is an Array in Java?

  • An array is a container object that holds a fixed number of values of the same type.
  • Each value in an array is called an element, and each element has an index starting from 0.

Example:

int[] numbers = {10, 20, 30, 40, 50};
System.out.println(numbers[2]); // 30

Here, numbers is an array of integers. numbers[2] accesses the third element (index starts from 0).


Advantages of Using Arrays

  1. Organized Storage: Stores multiple values in a single variable.
  2. Efficient Access: Use an index to access or modify elements quickly.
  3. Memory Management: Arrays store homogeneous data efficiently.
  4. Easy Iteration: Works seamlessly with loops (for, while, enhanced for).
  5. Useful for Algorithms: Sorting, searching, and mathematical operations become simpler.

Array Declaration and Initialization

There are two main steps: declaration and initialization.

1. Declaration

int[] arr1;     // Preferred way
int arr2[];     // Also valid

2. Initialization

arr1 = new int[5];  // Creates an array of 5 integers (default 0)

Declaration + Initialization in One Step

int[] scores = {90, 80, 70, 60, 50};

Example: Using Arrays

public class ArrayIntro {
    public static void main(String[] args) {
        // Declaration and Initialization
        int[] numbers = new int[5];

        // Assign values
        numbers[0] = 10;
        numbers[1] = 20;
        numbers[2] = 30;
        numbers[3] = 40;
        numbers[4] = 50;

        // Access and print values
        for(int i = 0; i < numbers.length; i++) {
            System.out.println("Element at index " + i + ": " + numbers[i]);
        }
    }
}

Output:

Element at index 0: 10
Element at index 1: 20
Element at index 2: 30
Element at index 3: 40
Element at index 4: 50

Points to Know :

  1. Arrays store homogeneous data in a single variable.
  2. Array size is fixed once created.
  3. Indexing starts at 0. Accessing an invalid index throws ArrayIndexOutOfBoundsException.
  4. Arrays are useful for loop-based operations and algorithm implementation.
  5. For dynamic data, consider ArrayList or other collections.

🏆 Top 5 Interview Questions – Arrays in Java

1. What is an array in Java?

Answer: An array is a container that stores multiple values of the same data type in contiguous memory locations. Each element can be accessed using an index, starting from 0.

Example:

int[] numbers = {1, 2, 3, 4, 5};
System.out.println(numbers[0]); // Output: 1

2. How is an array different from a variable?

Answer:

FeatureVariableArray
StoresSingle valueMultiple values of same type
AccessDirectBy index
MemorySingle memory locationContiguous memory locations
Exampleint a = 5;int[] arr = {1, 2, 3};

3. What are the advantages of using arrays?

Answer:

  1. Stores multiple values in a single variable.
  2. Efficient memory usage compared to multiple variables.
  3. Allows easy iteration using loops.
  4. Provides random access using indices.
  5. Useful in algorithms and data structures for sequential data.

4. How do you declare and initialize an array?

Answer:

Declaration:

int[] numbers;       // preferred
int numbers2[];      // valid but less preferred

Initialization:

numbers = new int[5];           // creates array of size 5
int[] numbers3 = {1, 2, 3, 4};  // declaration + initialization

Accessing elements:

numbers[0] = 10;
System.out.println(numbers[0]); // Output: 10

5. What happens if you try to access an index outside the array bounds?

Answer:

  • Java throws an ArrayIndexOutOfBoundsException at runtime.

Example:

int[] arr = {1, 2, 3};
System.out.println(arr[5]); // Exception: Index 5 out of bounds for length 3
  • Always ensure the index is between 0 and array.length - 1.
Article 0 of 0