Sitemap
Mobolaji Adekanmbi

Mobolaji Adekanmbi

String

3 min readSep 25, 2026

--

UNDERSTANDING STRING IN JAVA

WHAT IS A STRING?

A String represents a sequence of characters — a word, a sentence, a name, any piece of text. In Java, it lives in the java.lang package, so it’s available automatically without any import.

String greeting = “Hello, Bolaji!”;

Internally, a String is backed by a character array, but you never touch that array directly, you work through String’s built-in methods instead.

THE DEFINING TRAIT: IMMUTABILITY

Once a String is created, its content can never change. Anything that looks like “modifying” a string — concatenation, replacing characters, actually builds a brand-new String object in memory and leaves the original untouched.

String name = “Bolaji”;

name.concat(“ Adewale”); // this does NOT change ‘name’

System.out.println(name); // still “Bolaji”

name = name.concat(“ Adewale”); // now reassigned to the new object

System.out.println(name); // “Bolaji Adewale”

Immutability exists for good reasons: it makes Strings safe to share across threads (no one can change a value out from under another part of the program), and it enables Java’s String pool, an optimization where identical literals share the same memory instead of duplicating.

String a = “code”;

String b = “code”;

System.out.println(a == b); // true — same object in the pool

String c = new String(“code”);

System.out.println(a == c); // false — new object on the heap

THE GOLDEN RULE: NEVER COMPARE STRINGS WITH ==

== checks whether two variables point to the same object in memory, not whether their content matches. Always use .equals() or .equalsIgnoreCase() instead.

String a = “code”;

String b = new String(“code”);

System.out.println(a == b); // false

System.out.println(a.equals(b)); // true

This trips up almost every beginner at least once, it’s one of the most common bugs in early Java code, especially when comparing user input (e.g. from Scanner) against a fixed value.

THE COST OF REPEATED CONCATENATION

Because Strings are immutable, doing this inside a loop is more expensive than it looks:

String result = “”;

for (int i = 0; i < 1000; i++) {

. result += i; // creates a NEW String object every single iteration

}

Get Mobolaji Adekanmbi’s stories in your inbox

Join Medium for free to get updates from this writer.

Each += throws away the old object and builds a new one. For small tasks it doesn’t matter, but for heavy text-building, Java gives you a mutable alternative: StringBuilder.

StringBuilder sb = new StringBuilder();

for (int i = 0; i < 1000; i++) {

. sb.append(i); // modifies the same object in place — no waste

}

String result = sb.toString();

StringBuilder has methods like append(), insert(), delete(), reverse(), and toString() (to convert back into a normal String when you’re done).

COMMON STRING METHODS

length() — Number of characters. “Bolaji”.length() -> 6

charAt(i) — Character at index i. “Bolaji”.charAt(0) -> ‘B’

substring(a,b) — Extract portion. “Bolaji”.substring(0,3) -> “Bol”

toUpperCase()/toLowerCase() — Case conversion. “BOLAJI” / “bolaji”

trim() — Strips leading/trailing whitespace. “ hi “.trim() -> “hi”

split(regex) — Breaks string into an array. “a,b,c”.split(“,”) -> [a,b,c]

indexOf(str) — Position of first occurrence. “Bolaji”.indexOf(“l”) -> 3

replace(old,new) — Replace characters/substrings. “cat”.replace(“c”,”b”) -> “bat”

equals() — Compares content

equalsIgnoreCase() — Compares content, ignoring case

contains() — Checks if substring exists

isEmpty()/isBlank() — Checks for empty / whitespace-only string

toCharArray() — Converts to a char array. “hi”.toCharArray() -> [‘h’,’i’]

A QUICK PRACTICE EXAMPLE

String matric = “ csc/2021/045. “;

matric = matric.trim().toUpperCase();

System.out.println(matric); // “CSC/2021/045"

String[] parts = matric.split(“/”);

System.out.println(parts[0]); // “CSC”

System.out.println(parts[1]); // “2021"

This kind of cleanup, trimming whitespace, standardizing case, splitting on a delimiter — is something you’ll do constantly when processing real input.

WHERE STRINGS SHOW UP

Names, IDs, messages, file paths, user input from Scanner, output display. Basically any text your program touches, ever.

CONCLUSION

A String is a fixed, unchangeable sequence of characters, optimized for safety and reuse. Every “edit” you perform on one is really the creation of a new object. Understanding that single fact clears up most of the confusion beginners run into with text handling in Java, and it’s why StringBuilder exists for the cases where mutability actually matters.

--

--