| title | Java Strings |
|---|---|
| description | Complete guide to Java Strings with examples. Learn String methods, manipulation, comparison, StringBuilder, StringBuffer, and string formatting. |
| icon | Type |
In Java, strings are objects that represent sequences of characters. The String class is one of the most commonly used classes in Java programming.
A string is a sequence of characters. In Java, strings are objects of the String class. Unlike primitive data types, strings are reference types.
String message = "Hello, World!";Here, message is a string variable that holds the value "Hello, World!".
There are two main ways to create strings in Java:
String str1 = "Java Programming";
String str2 = "Javaistic";String str1 = new String("Java Programming");
String str2 = new String("Javaistic");Note: String literals are stored in the string pool for memory efficiency, while strings created with new keyword are stored in the heap memory.
Java provides many useful methods for string manipulation:
Returns the number of characters in the string.
class Main {
public static void main(String[] args) {
String str = "Java";
System.out.println("Length: " + str.length()); // Output: 4
}
}Returns the character at the specified index.
class Main {
public static void main(String[] args) {
String str = "Java";
System.out.println("Character at index 1: " + str.charAt(1)); // Output: a
}
}Extracts a portion of the string.
class Main {
public static void main(String[] args) {
String str = "Java Programming";
System.out.println("Substring: " + str.substring(5)); // Output: Programming
System.out.println("Substring: " + str.substring(0, 4)); // Output: Java
}
}Convert string to lowercase or uppercase.
class Main {
public static void main(String[] args) {
String str = "Java Programming";
System.out.println("Lowercase: " + str.toLowerCase()); // java programming
System.out.println("Uppercase: " + str.toUpperCase()); // JAVA PROGRAMMING
}
}Returns the index of the first occurrence of a character or substring.
class Main {
public static void main(String[] args) {
String str = "Java Programming";
System.out.println("Index of 'a': " + str.indexOf('a')); // Output: 1
System.out.println("Index of 'Programming': " + str.indexOf("Programming")); // Output: 5
}
}class Main {
public static void main(String[] args) {
String str1 = "Java";
String str2 = "Java";
String str3 = new String("Java");
System.out.println(str1.equals(str2)); // true
System.out.println(str1.equals(str3)); // true
}
}class Main {
public static void main(String[] args) {
String str1 = "Java";
String str2 = "JAVA";
System.out.println(str1.equalsIgnoreCase(str2)); // true
}
}class Main {
public static void main(String[] args) {
String str1 = "Apple";
String str2 = "Banana";
System.out.println(str1.compareTo(str2)); // negative value (Apple < Banana)
}
}Important: Never use == operator to compare strings. It compares references, not content.
class Main {
public static void main(String[] args) {
String firstName = "John";
String lastName = "Doe";
String fullName = firstName + " " + lastName;
System.out.println(fullName); // John Doe
}
}class Main {
public static void main(String[] args) {
String str1 = "Hello";
String str2 = " World";
String result = str1.concat(str2);
System.out.println(result); // Hello World
}
}When you need to perform many string operations, use StringBuilder or StringBuffer for better performance.
class Main {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder();
sb.append("Java");
sb.append(" ");
sb.append("Programming");
System.out.println(sb.toString()); // Java Programming
// Insert at specific position
sb.insert(5, "is ");
System.out.println(sb.toString()); // Java is Programming
// Delete characters
sb.delete(5, 8);
System.out.println(sb.toString()); // Java Programming
}
}| Feature | StringBuilder | StringBuffer |
|---|---|---|
| Thread Safety | Not thread-safe | Thread-safe |
| Performance | Faster | Slower |
| Usage | Single-threaded applications | Multi-threaded applications |
class Main {
public static void main(String[] args) {
String name = "John";
int age = 25;
System.out.printf("Name: %s, Age: %d%n", name, age);
// Output: Name: John, Age: 25
}
}class Main {
public static void main(String[] args) {
String name = "Alice";
double score = 95.5;
String formatted = String.format("Student: %s, Score: %.1f", name, score);
System.out.println(formatted); // Student: Alice, Score: 95.5
}
}class StringExample {
public static void main(String[] args) {
String text = " Java Programming Language ";
// Remove whitespace
String trimmed = text.trim();
System.out.println("Trimmed: '" + trimmed + "'");
// Split string
String[] words = trimmed.split(" ");
for (String word : words) {
System.out.println("Word: " + word);
}
// Replace characters
String replaced = trimmed.replace("Java", "Python");
System.out.println("Replaced: " + replaced);
// Check if string contains substring
boolean contains = trimmed.contains("Programming");
System.out.println("Contains 'Programming': " + contains);
// Check if string starts with or ends with
boolean startsWith = trimmed.startsWith("Java");
boolean endsWith = trimmed.endsWith("Language");
System.out.println("Starts with 'Java': " + startsWith);
System.out.println("Ends with 'Language': " + endsWith);
}
}Output:
Trimmed: 'Java Programming Language'
Word: Java
Word: Programming
Word: Language
Replaced: Python Programming Language
Contains 'Programming': true
Starts with 'Java': true
Ends with 'Language': true
- Strings in Java are immutable - once created, they cannot be changed
- Use
StringBuilderfor frequent string modifications - Always use
.equals()method to compare string content - String literals are stored in the string pool for memory efficiency
- Use
.trim()to remove leading and trailing whitespace - The
Stringclass provides many useful methods for manipulation
Performance Tip: When concatenating many strings, use StringBuilder instead of the + operator to avoid creating multiple intermediate string objects.