Strings & Arrays
String immutability and the String pool, StringBuilder, text blocks, and 1D/2D arrays.
On this page
Almost every program juggles text and lists of data. In Java, those are Strings and arrays. They have a couple of surprising behaviors that trip up beginners - so let's understand them properly.
Strings hold text
A String is a sequence of characters wrapped in double quotes. Strings
come with many useful built-in methods:
String name = "Ada Lovelace";
System.out.println(name.length()); // 12
System.out.println(name.toUpperCase()); // ADA LOVELACE
System.out.println(name.substring(0, 3)); // Ada
System.out.println(name.contains("Love"));// true
System.out.println(name.replace("a", "@"));// Ad@ Lovel@ceStrings are immutable
Here's the surprise: a String can never be changed after it's created. Every method that seems to "modify" a String actually returns a brand-new String and leaves the original untouched.
String s = "hello";
s.toUpperCase(); // this result is thrown away!
System.out.println(s); // still "hello"
s = s.toUpperCase(); // reassign to keep the new String
System.out.println(s); // "HELLO"A String is like words carved into a stone tablet - you can't edit the carving. To get "HELLO", you carve a new tablet and point your label at it. The old tablet still says "hello". That's immutability: the value never changes; you just point to a different one.
Why make Strings immutable?
Immutability makes Strings safe to share, safe across threads, and lets Java cache them efficiently in the String pool. It prevents a whole category of bugs - you never have to worry that some other code secretly changed your text.
Building strings efficiently with StringBuilder
Because each change creates a new String, building text in a big loop by
repeatedly using + is wasteful. For that, use StringBuilder, which is
mutable:
StringBuilder sb = new StringBuilder();
for (int i = 1; i <= 3; i++) {
sb.append("Item ").append(i).append("\n");
}
System.out.println(sb.toString());
// Item 1
// Item 2
// Item 3Text blocks (multi-line strings)
Modern Java has text blocks for multi-line text using triple quotes - great for JSON, HTML, or SQL:
String json = """
{
"name": "Ada",
"role": "pioneer"
}
""";Arrays: fixed-size lists
An array holds multiple values of the same type in a single variable. Its size is fixed when created.
int[] scores = { 90, 85, 100, 70 };
System.out.println(scores[0]); // 90 (first element)
System.out.println(scores[2]); // 100
System.out.println(scores.length); // 4
scores[1] = 88; // change an element
System.out.println(scores[1]); // 88Arrays start at index 0!
The first element is at index 0, not 1. So an array of length 4 has valid
indexes 0, 1, 2, 3. Reaching for scores[4] throws an
ArrayIndexOutOfBoundsException - one of the most common beginner errors.
Loop over an array with a for-each loop:
int total = 0;
for (int score : scores) {
total += score;
}
System.out.println("Total: " + total);Two-dimensional arrays: grids
An array of arrays forms a grid - perfect for tables, game boards, or matrices:
int[][] grid = {
{ 1, 2, 3 },
{ 4, 5, 6 }
};
System.out.println(grid[0][2]); // 3 (row 0, column 2)
System.out.println(grid[1][0]); // 4 (row 1, column 0)Need a resizable list?
Arrays have a fixed size. When you need a list that grows and shrinks, use an
ArrayList - you'll meet it in Stage 3's Collections lesson. Arrays are the
foundation; collections build on the idea.
Quick check
After: String s = "hi"; s.toUpperCase(); - what does s hold?
Key takeaways
- A String holds text and offers methods like length, substring, contains, and replace.
- Strings are immutable - methods return new strings; the original never changes.
- Use StringBuilder to build text efficiently, especially inside loops.
- Text blocks (triple quotes) hold multi-line strings.
- Arrays store a fixed number of same-typed values; indexing starts at 0.
- Reaching outside an array's bounds throws ArrayIndexOutOfBoundsException; 2D arrays model grids.
Programs go wrong constantly - that's normal. Next, we'll build the single most important beginner skill: reading errors and debugging.