Coding/Learn/java/strings
Java Foundational Course
01.Introduction to Java
02.Your First Java Program
03.Syntax & Code Structure
04.Comments in Java
05.Variables & Declaration
06.Primitive Data Types
07.Numbers & Arithmetic Operators
08.Strings in Java
09.Booleans & Relational Operators
10.Type Casting & Conversion
11.Scanner & Interactive Input
12.Logical Operators (&&, ||, !)
13.If Statements
14.If-Else Statements
15.If-Else-If Ladders
16.Switch Statements
17.While Loops
18.Do-While Loops
19.Standard For Loops
20.Break & Continue Statements
21.Arrays in Java
22.Array Indexing & Traversal
23.Enhanced For-Each Loop
24.Essential String Methods
25.Methods in Java
26.Method Parameters & Arguments
27.Method Overloading
28.Classes & Objects in Java
29.Constructors in Java
30.Exception Handling (try-catch-finally)
31.Capstone Mini Projects
Course Certificate0%

Complete all lessons to earn

LearnStrings in Java
8 / 31 0%
Lesson 08 of 31
6 mins read

Strings in Java

PreviousNext Lesson
In Plain English

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.

The Mental Model

"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 ==.

PointCore Rules to Remember

Point star
String Immutability: String methods return new strings; they do not alter the original string in memory.
Point star
The length() Method: String uses .length() with parentheses, unlike arrays which use .length without parentheses.
Point star
Safe Comparison: Always use str1.equals(str2) or str1.equalsIgnoreCase(str2).

Common Beginner Traps & How to Fix Them

Mistake: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.

Pro Tip: Use str.equalsIgnoreCase(...) when checking user input where capitalization doesn't matter (e.g. "yes" vs "YES").

Live Interactive Example

Hit Run Code to see it live
String Methods Demonstration
Java 13
1
2
3
4
5
6
7
8
9
10
Output Console
Click "Run Code" to view the rendered output.
How it works: trim() removes leading/trailing spaces, and toUpperCase() returns a new uppercase string.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Format user badge string

  • PointGiven the username "alex_dev", create an uppercase badge.
  • PointPrint "USER: " + username.toUpperCase().
1
2
3
4
5
6
7
8
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates