1. java-hell.de
  2. Topics
  3. Strings & Switch
Topic 7 of 11

Learn Java Strings & Switch – Exercises Online

Why == on strings will bite you one day and equals() won't: here you learn to work with text and to get rid of long if-else chains with switch.

The exercises

What you'll learn in this topic

  1. String Length
  2. Comparing Strings
  3. Switch
Exercise 1 of 3

String Length

Strings come with handy methods. The simplest one: .length()

String text = "Hello";
IO.println(text.length()); // 5

Watch out: For strings it's .length() with parentheses — for arrays it's .length without!

Replace ??? to print the length of the word.

Starter code
void main() {
    String word = "Programming";
    IO.println(???);
}
Background
Useful String Methods

Strings have lots of built-in methods: .toUpperCase(), .toLowerCase(), .substring(start, end), .indexOf("text"), .replace("old", "new"), .trim() (removes whitespace at the start and end), .contains("text"), .startsWith("prefix").

Since strings are immutable, all of these methods return a new string — the original string stays unchanged.

Exercise 2 of 3

Comparing Strings

Classic Java trap: You do not compare strings with ==, you use .equals():

if (name.equals("Anna")) {
    // ...
}

Replace ??? with the correct comparison.

Starter code
void main() {
    String input = "yes";

    if (???) {
        IO.println("Let's go!");
    } else {
        IO.println("Cancelled");
    }
}
Background
The String Pool

Java has a string pool — a cache for string literals. If you write "hello" twice in your code, both point to the same object in the pool. That's why == happens to work when you compare two literals — but that's luck, not a rule you should rely on!

Strings created at runtime (e.g. with new String("hello"), by concatenation, or from user input) do not end up in the pool. So: always use .equals().

Exercise 3 of 3

Switch

When you have a long chain of if-else if, a switch is easier to read:

switch (value) {
    case 1: ...; break;
    case 2: ...; break;
    default: ...;
}

Important: Don't forget break; — otherwise Java just falls through into the next case!

Add the missing case for Wednesday.

Starter code
void main() {
    int day = 3;

    switch (day) {
        case 1: IO.println("Monday"); break;
        case 2: IO.println("Tuesday"); break;
        // Add case 3 for "Wednesday"
        default: IO.println("Other day");
    }
}
Background
Switch Expressions (since Java 14)

Modern Java has a more elegant switch variant with ->:

String day = switch (nr) { case 1 -> "Monday"; case 2 -> "Tuesday"; default -> "Other"; };

Advantages: no break needed, it can return a value directly, and the compiler refuses to compile it if a case isn't covered (a switch expression must be exhaustive). The classic switch is still around, though.

Enough reading. Your turn.

In the tutorial you write the code yourself, hit Run and instantly see whether your program works. No JDK, no IDE, no bullshit.

Practice Strings & Switch in your browser