Strings
A free lesson from Swipe Coding · Java, level 2 · updated October 2026
By the Swipe team, founded by Colin · how we make lessons
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);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
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.
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)); // trueTo correctly compare the text content of two String objects, you should use the ____ method rather than the == operator.
What does "Java".substring(1, 3) return?
Once created, the characters inside a String object can never be changed.
Strings
10 questions · pass with 60% — just like in the app.
String is a primitive type in Java, just like int or boolean.
What does the + operator do when used with two Strings?
The ____ method returns how many characters are in a String.
What does "Java".charAt(0) return?
String indexes, like those used in charAt() and substring(), start counting at 0.
"Java".substring(1, 3) returns ____.
Which method should you use to correctly compare the text content of two Strings?
Once created, the contents of a String object can be changed directly.
Concatenating a String with a number using + automatically ____ the number to text.
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 →