Arrays & Strings in Java – Organize and Manipulate Data

sumaya
SM
Published on Feb, 17 2026 3 min read 0 comments
image

Introduction

In previous article, we learned control flow statements: if-else, switch, and loops.

Now, in this article, we’ll focus on storing and managing multiple pieces of data using:

  • Arrays – for collections of similar items
  • Strings – for text manipulation
  • StringBuilder & StringBuffer – for efficient string operations

By the end of this article, you’ll be able to store, access, and modify data effectively in Java programs.

1️⃣ Arrays in Java

An array is a container that stores multiple values of the same type in a single variable.

Syntax

dataType[] arrayName = new dataType[size];

Example:

int[] numbers = new int[5]; // array of 5 integers
numbers[0] = 10;
numbers[1] = 20;
numbers[2] = 30;
numbers[3] = 40;
numbers[4] = 50;

System.out.println(numbers[2]); // prints 30

Declare and Initialize in One Line

int[] numbers = {10, 20, 30, 40, 50};
String[] fruits = {"Apple", "Banana", "Orange"};

Access Array Elements

  • Use index (starts at 0)
System.out.println(fruits[0]); // Apple
System.out.println(fruits[2]); // Orange

Modify Array Elements

numbers[1] = 25;
System.out.println(numbers[1]); // 25

Array Length

System.out.println(numbers.length); // 5

Loop Through an Array

for(int i = 0; i < numbers.length; i++) {
    System.out.println(numbers[i]);
}

Or use enhanced for-loop:

for(int num : numbers) {
    System.out.println(num);
}

2️⃣ Multi-Dimensional Arrays

Used to store tables or matrices.

Syntax

dataType[][] arrayName = new dataType[rows][columns];

Example:

int[][] matrix = {
    {1, 2, 3},
    {4, 5, 6},
    {7, 8, 9}
};

System.out.println(matrix[1][2]); // 6

3️⃣ Strings in Java

A String stores a sequence of characters (text).

Example:

String greeting = "Hello, Java!";
System.out.println(greeting.length()); // 12
System.out.println(greeting.toUpperCase()); // HELLO, JAVA!
System.out.println(greeting.charAt(0)); // H

Common String Methods

| Method                                      | Description                   | Example                          |
| ------------------------------------------- | ----------------------------- | -------------------------------- |
| length()                                    | Get number of characters      | `"Java".length()` → 4            |
| charAt(int index)                           | Character at position         | `"Java".charAt(1)` → a           |
| toUpperCase()                               | Convert to uppercase          | `"java".toUpperCase()` → JAVA    |
| toLowerCase()                               | Convert to lowercase          | `"JAVA".toLowerCase()` → java    |
| substring(int start, int end)               | Extract substring             | `"Hello".substring(1,4)` → ell   |
| contains(CharSequence s)                    | Check if string contains text | `"Java".contains("va")` → true   |
| replace(CharSequence old, CharSequence new) | Replace characters            | `"Java".replace("a","o")` → Jovo |

4️⃣ StringBuilder & StringBuffer

Strings are immutable in Java. Every modification creates a new object, which is inefficient for frequent changes.

  • StringBuilder – mutable, faster, not thread-safe
  • StringBuffer – mutable, thread-safe

Example: StringBuilder

StringBuilder sb = new StringBuilder("Hello");
sb.append(" World");
System.out.println(sb); // Hello World
sb.insert(6, "Java ");
System.out.println(sb); // Hello Java World
sb.reverse();
System.out.println(sb); // dlroW avaJ olleH

5️⃣ Real-World Example: Student Names

public class StudentsExample {
    public static void main(String[] args) {
        String[] students = {"Rahim", "Karim", "Selim"};

        // Print all students
        for(String student : students) {
            System.out.println(student);
        }

        // Append " - Passed" to each name using StringBuilder
        for(String student : students) {
            StringBuilder sb = new StringBuilder(student);
            sb.append(" - Passed");
            System.out.println(sb);
        }
    }
}

Output:

Rahim
Karim
Selim
Rahim - Passed
Karim - Passed
Selim - Passed

Common Beginner Mistakes

  • Accessing index outside array bounds
  • Forgetting array initialization
  • Using == for string comparison (use .equals() instead)
  • Repeated string concatenation without StringBuilder (performance issue)

Example: String comparison

String a = "Java";
String b = "Java";
System.out.println(a == b); // may not always work
System.out.println(a.equals(b)); // always true

Summary

In this article, you learned:

  • Single and multi-dimensional arrays
  • Accessing and modifying array elements
  • Strings and common string operations
  • StringBuilder vs StringBuffer
  • Efficient ways to manipulate text and collections

Arrays and Strings are core building blocks for all Java programs.

Next Article Preview 👀

Next week, we’ll dive into Object-Oriented Programming (OOP):

  • Classes & objects
  • Constructors
  • The this keyword
  • Real-world OOP examples

Java becomes truly powerful when you start modeling real-world problems with objects.

0 Comments