Data Types
Strings
Working with Text
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.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);
}
}
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
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
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.
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);
}
}
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!
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!
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!
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!
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.
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());
}
}
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
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
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!
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!");
}
}
}
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): trueif (input.equalsIgnoreCase("yes"))
13String input = "yes"; //@input="no", "YES"14if (input.equalsIgnoreCase("yes")) {15 System.out.println("User agreed!");16}outputUser agreed!
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
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): trueif (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.
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));
}
}
}
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: ofor (int i = 0; i < word.length(); i++)
pass 1 of 515// 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: HAll 5 passes — pass 1 is the card above pass i1 0 2 1 3 2 4 3 5 4
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: dfor (int i = 0; i < word.length(); i++)
pass 1 of 515// 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: WAll 5 passes — pass 1 is the card above pass i1 0 2 1 3 2 4 3 5 4
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: afor (int i = 0; i < word.length(); i++)
pass 1 of 415// 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: JAll 4 passes — pass 1 is the card above pass i1 0 2 1 3 2 4 3
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