Strings: Working with Text
Duration: 5 min
What are Strings?
A String is a sequence of characters. In Java, strings are objects of the String class. Strings are immutable, meaning once created, they cannot be changed. This is an important concept that affects how you work with strings in Java.
Creating Strings
public class StringCreation {
public static void main(String[] args) {
// Method 1: String literal
String name = "Alice";
// Method 2: Using new keyword
String greeting = new String("Hello");
// Method 3: String concatenation
String message = "Hello, " + name + "!";
System.out.println(name);
System.out.println(greeting);
System.out.println(message);
}
}Alice
Hello
Hello, Alice!String Methods
The String class provides many useful methods for working with text. Here are some of the most common ones:
• length(): Returns the number of characters
• charAt(index): Returns the character at a specific index
• substring(start, end): Returns a portion of the string
• toUpperCase(): Converts to uppercase
• toLowerCase(): Converts to lowercase
• contains(substring): Checks if string contains a substring
• equals(other): Compares two strings
• indexOf(substring): Finds the index of a substring
public class StringMethods {
public static void main(String[] args) {
String text = "Hello, World!";
// length()
System.out.println("Length: " + text.length());
// charAt()
System.out.println("Character at index 0: " + text.charAt(0));
// substring()
System.out.println("Substring (0, 5): " + text.substring(0, 5));
// toUpperCase() and toLowerCase()
System.out.println("Uppercase: " + text.toUpperCase());
System.out.println("Lowercase: " + text.toLowerCase());
// contains()
System.out.println("Contains 'World': " + text.contains("World"));
// equals()
String other = "Hello, World!";
System.out.println("Equals: " + text.equals(other));
// indexOf()
System.out.println("Index of 'World': " + text.indexOf("World"));
}
}Length: 13
Character at index 0: H
Substring (0, 5): Hello
Uppercase: HELLO, WORLD!
Lowercase: hello, world!
Contains 'World': true
Equals: true
Index of 'World': 7String Immutability
Strings in Java are immutable. This means once a string is created, it cannot be changed. When you perform operations on a string, a new string is created. This is important for performance and memory management.
public class StringImmutability {
public static void main(String[] args) {
String original = "Hello";
System.out.println("Original: " + original);
// This creates a NEW string, doesn't modify the original
String modified = original.toUpperCase();
System.out.println("Modified: " + modified);
System.out.println("Original (unchanged): " + original);
// Concatenation also creates a new string
String result = original + " World";
System.out.println("Result: " + result);
System.out.println("Original (still unchanged): " + original);
}
}Original: Hello
Modified: HELLO
Original (unchanged): Hello
Result: Hello World
Original (still unchanged): HelloStringBuilder for Efficient String Building
When you need to build strings by concatenating many pieces, use StringBuilder instead of String concatenation. StringBuilder is mutable and more efficient for this purpose.
public class StringBuilderExample {
public static void main(String[] args) {
// Using String concatenation (inefficient for many operations)
String result1 = "";
for (int i = 1; i <= 5; i++) {
result1 += "Number " + i + ", ";
}
System.out.println("Result 1: " + result1);
// Using StringBuilder (efficient)
StringBuilder sb = new StringBuilder();
for (int i = 1; i <= 5; i++) {
sb.append("Number ").append(i).append(", ");
}
String result2 = sb.toString();
System.out.println("Result 2: " + result2);
}
}Result 1: Number 1, Number 2, Number 3, Number 4, Number 5,
Result 2: Number 1, Number 2, Number 3, Number 4, Number 5,💡 Tip: Use String for fixed text and StringBuilder when you need to build strings dynamically. This improves performance, especially in loops.
Learn more: https://docs.oracle.com/javase/tutorial/java/data/strings.html
❓ What does it mean that strings are immutable?
❓ What is a best practice when working with Strings?
💡 Tip: Pro Tip: Master Strings thoroughly. This foundation is crucial for writing professional Java code.