Java Strings in Java
In Java, a String is an object that represents a sequence of characters. Strings are immutable, meaning once created, their character contents can never be changed in-place—methods that transform strings return brand new String objects.
"Strings are like printed paperback books. You can't rewrite page 4 in the existing book; if you want an edited version, you print a new edition (a new String object)."
Deep Dive: How It Works
Concatenation: Join strings with the `+` operator. If you add a string and a number, Java converts the number to text.
Key methods: `.length()` (character count), `.toUpperCase()`, `.toLowerCase()`, `.indexOf("target")`, `.trim()` (strips whitespace).
String equality: In Java, NEVER compare strings using `==`. Always use `.equals()` to compare actual character contents.
Syntax Blueprint
String greeting = "Hello, " + name;
int len = greeting.length();
boolean match = greeting.equals("Hello, Alex");Always use .equals() for string comparison, not ==.
Core Rules to Remember



Common Beginner Traps & How to Fix Them
Using == to compare string content: if (name == "Alex")Why it happens: == checks if both variables point to the exact same memory address, not content equality.
How to fix: Use if (name.equals("Alex")) instead.
Live Interactive Example
Hit Run Code to see it liveYour Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Format user badge string
Given the username "alex_dev", create an uppercase badge.
Print "USER: " + username.toUpperCase().
Finished reading and practicing?
Mark this lesson as completed to update your course progress.