Back to Arrays
Q3Question 3 of 4

What does the Arrays utility class provide?

Key Concepts
  • Arrays.sort() sorts in-place using dual-pivot quicksort (primitives) or TimSort (objects)
  • Arrays.binarySearch() finds an element in a sorted array in O(log n)
  • Arrays.equals() compares two arrays element by element
  • Arrays.toString() produces a readable string: [1, 2, 3]
  • Arrays.copyOf() creates a resized copy
  • Arrays.fill() fills all elements with a value
  • Arrays.stream() converts to IntStream/Stream for functional operations

Definition

The Arrays utility class provides sort, search, compare, copy, fill, and stream operations for arrays. All methods are static.

Key methods:

  • sort() – in-place sort. O(n log n).
  • binarySearch() – find element in sorted array. O(log n).
  • equals() – compare two arrays element by element.
  • toString() – readable string representation.
  • copyOf() – create a resized copy.
  • fill() – set all elements to a value.
  • stream() – convert to Stream/IntStream for functional operations.

The Arrays class is a toolbox for array operations. Sort is alphabetizing bank files. BinarySearch is the fast lookup in the sorted index. Equals checks if two file drawers have identical contents. CopyOf makes a backup with optional resizing.

Understanding the Concepts

Common operations

JAVA
import java.util.Arrays;

int[] amounts = {50000, 12000, 8000, 25000};

// Sort
Arrays.sort(amounts);  // [8000, 12000, 25000, 50000]

// Binary search (array must be sorted!)
int index = Arrays.binarySearch(amounts, 25000);  // 2

// ToString
System.out.println(Arrays.toString(amounts));  // [8000, 12000, 25000, 50000]

// Equals
int[] copy = Arrays.copyOf(amounts, amounts.length);
System.out.println(Arrays.equals(amounts, copy));  // true

// Fill
int[] zeros = new int[5];
Arrays.fill(zeros, -1);  // [-1, -1, -1, -1, -1]

// Stream
long sum = Arrays.stream(amounts).sum();  // 95000
int max = Arrays.stream(amounts).max().orElse(0);  // 50000

Sorting objects

JAVA
String[] names = {"Priya", "Rajesh", "Pankaj"};
Arrays.sort(names);  // [Pankaj, Priya, Rajesh] (natural order)

// Custom comparator
Arrays.sort(names, Comparator.reverseOrder());  // [Rajesh, Priya, Pankaj]

What should a senior engineer know?

  • Primitive sort uses dual-pivot quicksort (not stable). Object sort uses TimSort (stable).
  • Arrays.parallelSort() uses fork-join for large arrays. Faster on multi-core for arrays >4096 elements.
  • Arrays.mismatch() (Java 9+) returns the index of the first difference between two arrays, or -1 if equal.

What are common mistakes?

A common mistake is not understanding the concept fully before using it in production banking applications.

Wrong approach:

JAVA
package com.bank.service;

public class TransferService {
    public void transfer(String fromAccount, String toAccount, double amount) {
        // WRONG: using double for money
        double balance = getBalance(fromAccount);
        balance = balance - amount;
    }
    private double getBalance(String id) { return 10000.0; }
}

Correct approach:

JAVA
package com.bank.service;

import java.math.BigDecimal;

public class TransferService {
    public void transfer(String fromAccount, String toAccount, BigDecimal amount) {
        // CORRECT: using BigDecimal for money
        BigDecimal balance = getBalance(fromAccount);
        balance = balance.subtract(amount);
    }
    private BigDecimal getBalance(String id) { return new BigDecimal("10000.00"); }
}

Always use BigDecimal for monetary calculations in banking systems to avoid floating-point precision errors.

What are the edge cases?

Edge cases can cause unexpected behavior in banking applications. Here are some to watch for.

JAVA
package com.bank.edge;

public class EdgeCaseDemo {
    public static void main(String[] args) {
        // Edge case 1: null handling
        String accountId = null;
        try {
            System.out.println(accountId.length());
        } catch (NullPointerException e) {
            System.out.println("Null account ID");
        }

        // Edge case 2: empty vs null
        String emptyId = "";
        System.out.println(emptyId.isEmpty());  // true
        System.out.println(emptyId == null);     // false

        // Edge case 3: boundary values
        java.math.BigDecimal zero = java.math.BigDecimal.ZERO;
        java.math.BigDecimal negativeBalance = new java.math.BigDecimal("-0.01");
        System.out.println(zero.compareTo(negativeBalance) > 0);  // true
    }
}
TEXT
Null account ID
true
false
true

Always test with null values, empty strings, boundary values, and negative amounts in banking applications.

What are common interview follow-up questions?

Interviewers often follow up with these related questions:

  • How does this concept apply to a high-throughput banking transaction system?
  • What are the performance implications in a production environment?
  • How would you debug issues related to this concept?
  • What tools would you use to monitor this in production?
  • How does this behave differently in Java 8 vs Java 17+?

When answering follow-up questions, always relate your answer back to practical experience. For example, at National Bank of India, Rajesh debugged a production issue related to this concept by using JFR recordings and heap dumps. Providing concrete examples from banking systems shows the interviewer that you understand both the theory and the practice.

Visual Overview

DIAGRAM
  Arrays utility methods:
    sort(arr)         → in-place sort
    binarySearch(arr,key) → index in sorted array
    equals(a, b)      → element-by-element comparison
    toString(arr)     → readable [1, 2, 3]
    copyOf(arr, len)  → new array, resized
    fill(arr, val)    → set all elements
    stream(arr)       → IntStream/Stream for functional ops

Key Takeaway

The Arrays class provides static utility methods for sorting, searching, comparing, copying, filling, and streaming arrays. Use Arrays.sort() for in-place sorting, binarySearch() for fast lookup in sorted arrays, and stream() for functional operations.

One sentence to remember: Arrays.sort() sorts, binarySearch() finds, equals() compares, toString() prints, copyOf() copies, stream() enables functional operations.

---

Share
Use ← → arrow keys to navigate