Arrays and strings in Java — what they are and why they matter

java dev.to

When I started learning DSA, arrays were the first topic I encountered — and honestly, they seemed too simple to matter. Then I saw them show up in almost every interview problem I practiced. Turns out, understanding arrays and strings well is not optional.

In this post, I'll walk you through what arrays and strings are, how they work in Java, and why they're foundational to everything else in DSA.


What is an array?

An array is a collection of elements stored in contiguous memory locations. Think of it like a row of lockers — each locker has a number (index), and you can jump to any locker directly without opening the ones before it.

// Declaring and initializing an array in Java
int[] numbers = {10, 20, 30, 40, 50};

// Accessing an element — O(1) time
System.out.println(numbers[2]);  // Output: 30

// Length of the array
System.out.println(numbers.length);  // Output: 5
Enter fullscreen mode Exit fullscreen mode

💡 Indexing starts at 0 in Java. So the first element is at index 0, not 1. This trips up almost everyone at the start.


Common array operations

Here's a quick summary of time complexities you should know:

Operation Time Complexity Why
Access by index O(1) Jump directly to the position
Search (linear) O(n) Check each element one by one
Insert at end O(1) If space is available
Insert at middle O(n) Elements need to shift
Delete O(n) Elements need to shift

The biggest strength of arrays is random access in O(1). The biggest weakness is that inserting or deleting from the middle is expensive because you have to shift elements.


What is a string?

A string is a sequence of characters. In Java, strings are objects of the String class — and they are immutable, meaning once created, you cannot change the characters in place.

String name = "Sahithi";

// Common string methods
name.length();           // 7
name.charAt(0);          // 'S'
name.substring(0, 3);    // "Sah"
name.toLowerCase();      // "sahithi"
name.contains("hit");    // true
Enter fullscreen mode Exit fullscreen mode

⚠️ Since strings are immutable in Java, every time you do str = str + "x", a new string object is created. For repeated concatenation, use StringBuilder instead — it's much more efficient.


Arrays vs strings — key differences

Feature Array String
Mutable? Yes No (in Java)
Data type Any type Characters only
Size Fixed Fixed (but new obj on change)
Access index[] charAt() or index[]

A simple problem to try

Here's a classic beginner problem: reverse an array in place.

void reverse(int[] arr) {
    int left = 0, right = arr.length - 1;
    while (left < right) {
        int temp = arr[left];
        arr[left] = arr[right];
        arr[right] = temp;
        left++;
        right--;
    }
}
Enter fullscreen mode Exit fullscreen mode

This is the two-pointer technique — a pattern that shows up constantly in DSA problems. Once you get comfortable with arrays, you'll see it everywhere.


Thanks for reading! In the next post, I'll cover two-pointer problems on arrays and strings — one of the most common interview patterns. If you found this helpful, share it with a fellow student who's just starting out. 🙌

Source: dev.to

arrow_back Back to Tutorials