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
public class Name {
public static void main(String[] args) {
String name = ;
System.out.println("Hello, " + name + "!");
System.out.println("Your name is: " + name);
}
}
public class Name {
public static void main(String[] args) {
String name = ;
System.out.println("Hello, " + name + "!");
System.out.println("Your name is: " + name);
}
}
public class Name {
public static void main(String[] args) {
String name = ;
System.out.println("Hello, " + name + "!");
System.out.println("Your name is: " + name);
}
}
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
public class Combine {
public static void main(String[] args) {
String firstName = ;
String lastName = ;
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 = ;
String lastName = ;
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 = ;
String lastName = ;
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 = ;
String lastName = ;
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 = ;
String lastName = ;
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 = ;
String lastName = ;
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 = ;
String lastName = ;
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 = ;
String lastName = ;
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 = ;
String lastName = ;
String fullName = firstName + " " + lastName;
System.out.println("Full name: " + fullName);
// Building a greeting
String greeting = "Hello, " + fullName + "!";
System.out.println(greeting);
}
}
concatenation
Joining strings with `+`: `"Hello" + " " + "World"` → `"Hello World"`
Get string length
Find out how many characters are in a string.
Length.java
public class Length {
public static void main(String[] args) {
String word = ;
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 = ;
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 = ;
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());
}
}
length
Number of characters: `"Hello".length()` → `5`
Compare two strings
Check if strings are equal - but be careful with == in Java!
Compare.java
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 = ;
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 = ;
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 = ;
if (input.equalsIgnoreCase("yes")) {
System.out.println("User agreed!");
}
}
}
Always use .equals() for string comparison, not ==.
Access character by index
Get individual characters from a string by their position.
Index.java
public class Index {
public static void main(String[] args) {
String word = ;
// 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 = ;
// 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 = ;
// 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));
}
}
}
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