You're building a greeting card generator. The user enters their name "Alice" and you need to create "Hello, Alice!" - combining text, storing it, measuring it, and accessing individual letters.

Store and print a name

Create a string and display it.

name
Name.java
Replay: real traced execution (multi-file project)
public class Name {
    public static void main(String[] args) {
        String name = "Alice";
        System.out.println("Hello, " + name + "!");
        System.out.println("Your name is: " + name);
    }
}
public class Name {
    public static void main(String[] args) {
        String name = "Bob";
        System.out.println("Hello, " + name + "!");
        System.out.println("Your name is: " + name);
    }
}
public class Name {
    public static void main(String[] args) {
        String name = "Charlie";
        System.out.println("Hello, " + name + "!");
        System.out.println("Your name is: " + name);
    }
}
  1. name ← Alice

    1public class Name {2    public static void main(String[] args) {3        String name→ Alice = "Alice";  //@name="Bob", "Charlie"4        System.out.println("Hello, " + nameAlice + "!");5        System.out.println("Your name is: " + nameAlice);6    }
    outputHello, Alice!
    Your name is: Alice
  1. name ← Bob

    1public class Name {2    public static void main(String[] args) {3        String name→ Bob = "Bob";4        System.out.println("Hello, " + nameBob + "!");5        System.out.println("Your name is: " + nameBob);6    }
    outputHello, Bob!
    Your name is: Bob
  1. name ← Charlie

    1public class Name {2    public static void main(String[] args) {3        String name→ Charlie = "Charlie";4        System.out.println("Hello, " + nameCharlie + "!");5        System.out.println("Your name is: " + nameCharlie);6    }
    outputHello, Charlie!
    Your name is: Charlie

Strings use double quotes in Java. Single quotes are for single characters.

String A sequence of characters, like `"Hello"`. Immutable in Java.

Combine first and last name

Concatenate strings using the + operator.

example
Combine.java
Replay: real traced execution (multi-file project)
public class Combine {
    public static void main(String[] args) {
        String firstName = "Alice";
        String lastName = "Smith";

        String fullName = firstName + " " + lastName;
        System.out.println("Full name: " + fullName);

        // Building a greeting
        String greeting = "Hello, " + fullName + "!";
        System.out.println(greeting);
    }
}
public class Combine {
    public static void main(String[] args) {
        String firstName = "Bob";
        String lastName = "Smith";

        String fullName = firstName + " " + lastName;
        System.out.println("Full name: " + fullName);

        // Building a greeting
        String greeting = "Hello, " + fullName + "!";
        System.out.println(greeting);
    }
}
public class Combine {
    public static void main(String[] args) {
        String firstName = "Charlie";
        String lastName = "Smith";

        String fullName = firstName + " " + lastName;
        System.out.println("Full name: " + fullName);

        // Building a greeting
        String greeting = "Hello, " + fullName + "!";
        System.out.println(greeting);
    }
}
public class Combine {
    public static void main(String[] args) {
        String firstName = "Alice";
        String lastName = "Jones";

        String fullName = firstName + " " + lastName;
        System.out.println("Full name: " + fullName);

        // Building a greeting
        String greeting = "Hello, " + fullName + "!";
        System.out.println(greeting);
    }
}
public class Combine {
    public static void main(String[] args) {
        String firstName = "Alice";
        String lastName = "Lee";

        String fullName = firstName + " " + lastName;
        System.out.println("Full name: " + fullName);

        // Building a greeting
        String greeting = "Hello, " + fullName + "!";
        System.out.println(greeting);
    }
}
  1. firstName ← Alice, lastName ← Smith, fullName ← Alice Smith, greeting ← Hello, Alice Smith!

    1public class Combine {2    public static void main(String[] args) {3        String firstName→ Alice = "Alice";  //@firstName="Bob", "Charlie"4        String lastName→ Smith = "Smith";   //@lastName="Jones", "Lee"5        6        String fullName→ Alice Smith = firstNameAlice + " " + lastNameSmith;7        System.out.println("Full name: " + fullNameAlice Smith);8        9        // Building a greeting10        String greeting→ Hello, Alice Smith! = "Hello, " + fullNameAlice Smith + "!";11        System.out.println(greetingHello, Alice Smith!);12    }
    outputFull name: Alice Smith
    Hello, Alice Smith!
  1. firstName ← Bob, lastName ← Smith, fullName ← Bob Smith, greeting ← Hello, Bob Smith!

    1public class Combine {2    public static void main(String[] args) {3        String firstName→ Bob = "Bob";4        String lastName→ Smith = "Smith";5        6        String fullName→ Bob Smith = firstNameBob + " " + lastNameSmith;7        System.out.println("Full name: " + fullNameBob Smith);8        9        // Building a greeting10        String greeting→ Hello, Bob Smith! = "Hello, " + fullNameBob Smith + "!";11        System.out.println(greetingHello, Bob Smith!);12    }
    outputFull name: Bob Smith
    Hello, Bob Smith!
  1. firstName ← Charlie, lastName ← Smith, fullName ← Charlie Smith

    1public class Combine {2    public static void main(String[] args) {3        String firstName→ Charlie = "Charlie";4        String lastName→ Smith = "Smith";5        6        String fullName→ Charlie Smith = firstNameCharlie + " " + lastNameSmith;7        System.out.println("Full name: " + fullNameCharlie Smith);8        9        // Building a greeting10        String greeting→ Hello, Charlie Smith! = "Hello, " + fullNameCharlie Smith + "!";11        System.out.println(greetingHello, Charlie Smith!);12    }
    outputFull name: Charlie Smith
    Hello, Charlie Smith!
  1. firstName ← Alice, lastName ← Jones, fullName ← Alice Jones, greeting ← Hello, Alice Jones!

    1public class Combine {2    public static void main(String[] args) {3        String firstName→ Alice = "Alice";4        String lastName→ Jones = "Jones";5        6        String fullName→ Alice Jones = firstNameAlice + " " + lastNameJones;7        System.out.println("Full name: " + fullNameAlice Jones);8        9        // Building a greeting10        String greeting→ Hello, Alice Jones! = "Hello, " + fullNameAlice Jones + "!";11        System.out.println(greetingHello, Alice Jones!);12    }
    outputFull name: Alice Jones
    Hello, Alice Jones!
  1. firstName ← Alice, lastName ← Lee, fullName ← Alice Lee, greeting ← Hello, Alice Lee!

    1public class Combine {2    public static void main(String[] args) {3        String firstName→ Alice = "Alice";4        String lastName→ Lee = "Lee";5        6        String fullName→ Alice Lee = firstNameAlice + " " + lastNameLee;7        System.out.println("Full name: " + fullNameAlice Lee);8        9        // Building a greeting10        String greeting→ Hello, Alice Lee! = "Hello, " + fullNameAlice Lee + "!";11        System.out.println(greetingHello, Alice Lee!);12    }
    outputFull name: Alice Lee
    Hello, Alice Lee!
concatenation Joining strings with `+`: `"Hello" + " " + "World"` → `"Hello World"`

Get string length

Find out how many characters are in a string.

word
Length.java
Replay: real traced execution (multi-file project)
public class Length {
    public static void main(String[] args) {
        String word = "Hello";
        String sentence = "Hello World";

        System.out.println("'" + word + "' has " + word.length() + " characters");
        System.out.println("'" + sentence + "' has " + sentence.length() + " characters");

        // Empty string has length 0
        String empty = "";
        System.out.println("Empty string length: " + empty.length());
    }
}
public class Length {
    public static void main(String[] args) {
        String word = "Programming";
        String sentence = "Hello World";

        System.out.println("'" + word + "' has " + word.length() + " characters");
        System.out.println("'" + sentence + "' has " + sentence.length() + " characters");

        // Empty string has length 0
        String empty = "";
        System.out.println("Empty string length: " + empty.length());
    }
}
public class Length {
    public static void main(String[] args) {
        String word = "Hi";
        String sentence = "Hello World";

        System.out.println("'" + word + "' has " + word.length() + " characters");
        System.out.println("'" + sentence + "' has " + sentence.length() + " characters");

        // Empty string has length 0
        String empty = "";
        System.out.println("Empty string length: " + empty.length());
    }
}
  1. word ← Hello, sentence ← Hello World, empty ← (empty)

    1public class Length {2    public static void main(String[] args) {3        String word→ Hello = "Hello";     //@word="Programming", "Hi"4        String sentence→ Hello World = "Hello World";5        6        System.out.println("'" + wordHello + "' has " + word.length() + " characters");7        System.out.println("'" + sentenceHello World + "' has " + sentence.length() + " characters");8        9        // Empty string has length 010        String empty→ (empty) = "";11        System.out.println("Empty string length: " + empty.length());12    }
    output'Hello' has 5 characters
    'Hello World' has 11 characters
    Empty string length: 0
  1. word ← Programming, sentence ← Hello World, empty ← (empty)

    1public class Length {2    public static void main(String[] args) {3        String word→ Programming = "Programming";4        String sentence→ Hello World = "Hello World";5        6        System.out.println("'" + wordProgramming + "' has " + word.length() + " characters");7        System.out.println("'" + sentenceHello World + "' has " + sentence.length() + " characters");8        9        // Empty string has length 010        String empty→ (empty) = "";11        System.out.println("Empty string length: " + empty.length());12    }
    output'Programming' has 11 characters
    'Hello World' has 11 characters
    Empty string length: 0
  1. word ← Hi, sentence ← Hello World, empty ← (empty)

    1public class Length {2    public static void main(String[] args) {3        String word→ Hi = "Hi";4        String sentence→ Hello World = "Hello World";5        6        System.out.println("'" + wordHi + "' has " + word.length() + " characters");7        System.out.println("'" + sentenceHello World + "' has " + sentence.length() + " characters");8        9        // Empty string has length 010        String empty→ (empty) = "";11        System.out.println("Empty string length: " + empty.length());12    }
    output'Hi' has 2 characters
    'Hello World' has 11 characters
    Empty string length: 0
length Number of characters: `"Hello".length()` → `5`

Compare two strings

Check if strings are equal - but be careful with == in Java!

input
Compare.java
Replay: real traced execution (multi-file project)
public class Compare {
    public static void main(String[] args) {
        String a = "hello";
        String b = "hello";
        String c = "Hello";  // different case

        // Use .equals() for content comparison
        System.out.println("a.equals(b): " + a.equals(b));       // true
        System.out.println("a.equals(c): " + a.equals(c));       // false (case matters)
        System.out.println("a.equalsIgnoreCase(c): " + a.equalsIgnoreCase(c));  // true

        // Comparing with user input
        String input = "yes";
        if (input.equalsIgnoreCase("yes")) {
            System.out.println("User agreed!");
        }

    }
}
public class Compare {
    public static void main(String[] args) {
        String a = "hello";
        String b = "hello";
        String c = "Hello";  // different case

        // Use .equals() for content comparison
        System.out.println("a.equals(b): " + a.equals(b));       // true
        System.out.println("a.equals(c): " + a.equals(c));       // false (case matters)
        System.out.println("a.equalsIgnoreCase(c): " + a.equalsIgnoreCase(c));  // true

        // Comparing with user input
        String input = "no";
        if (input.equalsIgnoreCase("yes")) {
            System.out.println("User agreed!");
        }

    }
}
public class Compare {
    public static void main(String[] args) {
        String a = "hello";
        String b = "hello";
        String c = "Hello";  // different case

        // Use .equals() for content comparison
        System.out.println("a.equals(b): " + a.equals(b));       // true
        System.out.println("a.equals(c): " + a.equals(c));       // false (case matters)
        System.out.println("a.equalsIgnoreCase(c): " + a.equalsIgnoreCase(c));  // true

        // Comparing with user input
        String input = "YES";
        if (input.equalsIgnoreCase("yes")) {
            System.out.println("User agreed!");
        }

    }
}
  1. a ← hello, b ← hello, c ← Hello, input ← yes

    1public class Compare {2    public static void main(String[] args) {3        String a→ hello = "hello";4        String b→ hello = "hello";5        String c→ Hello = "Hello";  // different case6        7        // Use .equals() for content comparison8        System.out.println("a.equals(b): " + a.equals(bhello));       // true9        System.out.println("a.equals(c): " + a.equals(cHello));       // false (case matters)10        System.out.println("a.equalsIgnoreCase(c): " + a.equalsIgnoreCase(cHello));  // true11        12        // Comparing with user input  //#?why_equals13        String input→ yes = "yes";   //@input="no", "YES"14        if (input.equalsIgnoreCase("yes")) {
    outputa.equals(b): true
    a.equals(c): false
    a.equalsIgnoreCase(c): true
  2. if (input.equalsIgnoreCase("yes"))

    13String input = "yes";   //@input="no", "YES"14if (input.equalsIgnoreCase("yes")) {15    System.out.println("User agreed!");16}
    outputUser agreed!
  1. a ← hello, b ← hello, c ← Hello, input ← no

    1public class Compare {2    public static void main(String[] args) {3        String a→ hello = "hello";4        String b→ hello = "hello";5        String c→ Hello = "Hello";  // different case6        7        // Use .equals() for content comparison8        System.out.println("a.equals(b): " + a.equals(bhello));       // true9        System.out.println("a.equals(c): " + a.equals(cHello));       // false (case matters)10        System.out.println("a.equalsIgnoreCase(c): " + a.equalsIgnoreCase(cHello));  // true11        12        // Comparing with user input13        String input→ no = "no";14        if (input.equalsIgnoreCase("yes")) {
    outputa.equals(b): true
    a.equals(c): false
    a.equalsIgnoreCase(c): true
  1. a ← hello, b ← hello, c ← Hello, input ← YES

    1public class Compare {2    public static void main(String[] args) {3        String a→ hello = "hello";4        String b→ hello = "hello";5        String c→ Hello = "Hello";  // different case6        7        // Use .equals() for content comparison8        System.out.println("a.equals(b): " + a.equals(bhello));       // true9        System.out.println("a.equals(c): " + a.equals(cHello));       // false (case matters)10        System.out.println("a.equalsIgnoreCase(c): " + a.equalsIgnoreCase(cHello));  // true11        12        // Comparing with user input13        String input→ YES = "YES";14        if (input.equalsIgnoreCase("yes")) {
    outputa.equals(b): true
    a.equals(c): false
    a.equalsIgnoreCase(c): true
  2. if (input.equalsIgnoreCase("yes"))

    13String input = "YES";14if (input.equalsIgnoreCase("yes")) {15    System.out.println("User agreed!");16}
    outputUser agreed!

Always use .equals() for string comparison, not ==.

Access character by index

Get individual characters from a string by their position.

word
Index.java
Replay: real traced execution (multi-file project)
public class Index {
    public static void main(String[] args) {
        String word = "Hello";

        // Index:    0 1 2 3 4
        // Chars:    H e l l o

        char first = word.charAt(0);
        char last = word.charAt(word.length() - 1);

        System.out.println("Word: " + word);
        System.out.println("First character: " + first);
        System.out.println("Last character: " + last);

        // Print all characters with their indices
        for (int i = 0; i < word.length(); i++) {
            System.out.println("Index " + i + ": " + word.charAt(i));
        }
    }
}
public class Index {
    public static void main(String[] args) {
        String word = "World";

        // Index:    0 1 2 3 4
        // Chars:    H e l l o

        char first = word.charAt(0);
        char last = word.charAt(word.length() - 1);

        System.out.println("Word: " + word);
        System.out.println("First character: " + first);
        System.out.println("Last character: " + last);

        // Print all characters with their indices
        for (int i = 0; i < word.length(); i++) {
            System.out.println("Index " + i + ": " + word.charAt(i));
        }
    }
}
public class Index {
    public static void main(String[] args) {
        String word = "Java";

        // Index:    0 1 2 3 4
        // Chars:    H e l l o

        char first = word.charAt(0);
        char last = word.charAt(word.length() - 1);

        System.out.println("Word: " + word);
        System.out.println("First character: " + first);
        System.out.println("Last character: " + last);

        // Print all characters with their indices
        for (int i = 0; i < word.length(); i++) {
            System.out.println("Index " + i + ": " + word.charAt(i));
        }
    }
}
  1. word ← Hello, first ← H, last ← o

    1public class Index {2    public static void main(String[] args) {3        String word→ Hello = "Hello";  //@word="World", "Java"4        5        // Index:    0 1 2 3 46        // Chars:    H e l l o7        8        char first→ H = word.charAt(0);9        char last→ o = word.charAt(word.length() - 1);10        11        System.out.println("Word: " + wordHello);12        System.out.println("First character: " + firstH);13        System.out.println("Last character: " + lasto);
    outputWord: Hello
    First character: H
    Last character: o
  2. for (int i = 0; i < word.length(); i++)

    pass 1 of 5
    15// Print all characters with their indices16for (int i0 = 0; i < word.length(); i++) {17    System.out.println("Index " + i0 + ": " + word.charAt(i));18}
    outputIndex 0: H
    All 5 passes — pass 1 is the card above
    passi
    10
    21
    32
    43
    54
  1. word ← World, first ← W, last ← d

    1public class Index {2    public static void main(String[] args) {3        String word→ World = "World";4        5        // Index:    0 1 2 3 46        // Chars:    H e l l o7        8        char first→ W = word.charAt(0);9        char last→ d = word.charAt(word.length() - 1);10        11        System.out.println("Word: " + wordWorld);12        System.out.println("First character: " + firstW);13        System.out.println("Last character: " + lastd);
    outputWord: World
    First character: W
    Last character: d
  2. for (int i = 0; i < word.length(); i++)

    pass 1 of 5
    15// Print all characters with their indices16for (int i0 = 0; i < word.length(); i++) {17    System.out.println("Index " + i0 + ": " + word.charAt(i));18}
    outputIndex 0: W
    All 5 passes — pass 1 is the card above
    passi
    10
    21
    32
    43
    54
  1. word ← Java, first ← J, last ← a

    1public class Index {2    public static void main(String[] args) {3        String word→ Java = "Java";4        5        // Index:    0 1 2 3 46        // Chars:    H e l l o7        8        char first→ J = word.charAt(0);9        char last→ a = word.charAt(word.length() - 1);10        11        System.out.println("Word: " + wordJava);12        System.out.println("First character: " + firstJ);13        System.out.println("Last character: " + lasta);
    outputWord: Java
    First character: J
    Last character: a
  2. for (int i = 0; i < word.length(); i++)

    pass 1 of 4
    15// Print all characters with their indices16for (int i0 = 0; i < word.length(); i++) {17    System.out.println("Index " + i0 + ": " + word.charAt(i));18}
    outputIndex 0: J
    All 4 passes — pass 1 is the card above
    passi
    10
    21
    32
    43

Positions start at 0. First character is at index 0, second at index 1, etc.

charAt Get character at position: `"Hello".charAt(0)` → `'H'`

Exercise: Special.java

Explore special strings: empty string, escape sequences, multiline