Strings in Java: Methods, Comparison and the equals() Trap
Learn how to work with text in Java — String methods, concatenation, and the crucial rule to compare strings with .equals() not ==. Beginner-friendly with examples.
Text is everywhere in programming — names, messages, passwords, file paths, whole web pages. In Java, text is handled by the String type, and it comes with a rich toolkit of built-in methods. This lesson gives you everything you need to work confidently with text — and clears up the single most important "gotcha" that confuses nearly every beginner. This wraps up the Level 1 foundations. 🎉
Creating a String
A String is a sequence of characters wrapped in double quotes:
String name = "GyaanPost"; String empty = ""; // an empty string
Note the capital S — String, not string. (Single characters like 'A' use single quotes and the char type instead.)
Joining strings (concatenation)
The + operator joins strings together — you've seen this already:
String first = "Shiv"; String full = first + " Narayan"; // "Shiv Narayan" System.out.println("Age: " + 25); // numbers auto-convert to text: "Age: 25"
Handy String methods
Every String has built-in methods. Here are the ones you'll use constantly:
| Method | What it does | Example (s = "Hello") |
|---|---|---|
length() |
Number of characters | s.length() → 5 |
toUpperCase() |
Uppercase copy | "HELLO" |
toLowerCase() |
Lowercase copy | "hello" |
charAt(i) |
Character at index i (from 0) | s.charAt(0) → 'H' |
substring(a,b) |
Part from index a up to b | s.substring(0,3) → "Hel" |
contains("x") |
Is text inside? (true/false) | s.contains("ell") → true |
trim() |
Removes spaces at both ends | " hi ".trim() → "hi" |
replace(a,b) |
Swap text | s.replace("l","L") → "HeLLo" |
You call them with a dot after the string, like name.toUpperCase(). Notice length() here has parentheses — that's different from arrays' .length with none. A small but common mix-up!
.equals(), not ==.This is the classic Java trap. To check if two strings have the same text, always use
.equals():
String a = "hello"; String b = "hello"; a.equals(b) // true ✓ compares the TEXT (use this!) a == b // unreliable — compares memory location, not textWhy? For objects like String,
== asks "are these the exact same object in memory?" while .equals() asks "do they contain the same characters?" — which is almost always what you actually want. Burn this in: text comparison = .equals().Strings are immutable
An important fact: once created, a String cannot be changed — it's immutable. Methods like toUpperCase() don't modify the original; they return a brand-new string. So you must capture the result:
String s = "hello"; s.toUpperCase(); // does nothing useful — result thrown away! System.out.println(s); // still "hello" s = s.toUpperCase(); // ✓ capture the new string System.out.println(s); // "HELLO"
A practical example
String email = " User@GyaanPost.com "; String clean = email.trim().toLowerCase(); // chain methods! System.out.println(clean); // "user@gyaanpost.com" System.out.println(clean.contains("@")); // true
See how we chained .trim().toLowerCase() — each returns a new string you can immediately call the next method on. Very handy.
The == versus .equals() lesson is one I learned the hard way — an early login check of mine "randomly" failed because I compared strings with ==. It works often enough by accident to be genuinely confusing. Save yourself that debugging session: for text, it's always .equals().
Key takeaways
- A String holds text in double quotes; join strings with
+. - Use built-in methods like
length(),toUpperCase(),substring(),trim(),contains(). - Always compare text with
.equals(), never==. - Strings are immutable — methods return a new string, so capture the result.
← Previous: Lesson 10 — Arrays in Java
Next: Lesson 12 — Classes and Objects (Start of OOP) →
↑ Back to the full Java course roadmap