Swipe
SwipeSwipe
Swipe Apps

Paths

Motivation

Information

Strings · Lesson 26 of 28

Strings

A free lesson from Swipe Coding · Java, level 2 · updated October 2026

By the Swipe team, founded by Colin · how we make lessons

🤖
Strings look simple, but under the hood they work differently from int. 🧵

String is one of the most-used types in Java, but it works differently from the primitives you just learned: String is a reference type (a class from the java.lang package), not a primitive. A String variable doesn't store the text directly the way an int stores a number — it stores a reference to a String object living elsewhere in memory. In everyday code, though, you can create and use Strings almost as easily as primitives.

String name = "Ada";
int points = 95;
System.out.println("Hello, " + name + "!");
System.out.println("Score: " + points);
The + operator concatenates (joins) strings together, and automatically converts numbers to text when needed.
🤖
These three methods will show up again and again in your code. 🔧

String comes with many useful methods. length() returns how many characters are in the string. charAt(index) returns the single character at a given position, counting from 0. substring(start, end) returns a new string containing the characters from start up to (but not including) end.

String word = "Java";
System.out.println(word.length());        // 4
System.out.println(word.charAt(0));       // J
System.out.println(word.substring(1, 3)); // av
length(), charAt(), and substring() are some of the most frequently used String methods.
🤖
Once created, a String's text is locked in for good. 🔒

Strings are immutable: once a String object is created, its contents can never change. Methods like substring() don't modify the original string — they return a brand-new String with the result, leaving the original untouched.

🤖
This is one of Java's classic gotchas — read closely before you answer. 👀

Because String is a reference type, the == operator compares whether two variables point to the exact same object in memory — not whether their text content is equal. To correctly check if two Strings contain the same characters, always use the .equals() method instead.

String a = "cat";
String b = "cat";
String c = new String("cat");
System.out.println(a == b);      // true
System.out.println(a == c);      // false
System.out.println(a.equals(c)); // true
a and b happen to be ==, but c is a separate object — only .equals() reliably compares String content.
Fill in the blank

To correctly compare the text content of two String objects, you should use the ____ method rather than the == operator.

Quick check

What does "Java".substring(1, 3) return?

True or false

Once created, the characters inside a String object can never be changed.

Chapter quiz

Strings

10 questions · pass with 60% — just like in the app.

True or false

String is a primitive type in Java, just like int or boolean.

Quick check

What does the + operator do when used with two Strings?

Fill in the blank

The ____ method returns how many characters are in a String.

Quick check

What does "Java".charAt(0) return?

True or false

String indexes, like those used in charAt() and substring(), start counting at 0.

Fill in the blank

"Java".substring(1, 3) returns ____.

Quick check

Which method should you use to correctly compare the text content of two Strings?

True or false

Once created, the contents of a String object can be changed directly.

Fill in the blank

Concatenating a String with a number using + automatically ____ the number to text.

Quick check

Why is == unreliable for comparing the content of two String objects?

Keep your streak going in Swipe Coding

Swipe through every level of Java on your iPhone — with XP, stars, streaks, a personal advisor and the full track beyond these free levels.

Get the Swipe apps →