Strings in Java
A String in Java is a sequence of characters. Unlike primitive types (int, char, boolean), String is a class, so every string is an object.
String name = "Saurabh"; // string literal (recommended)String city = new String("Mumbai"); // using new keywordString Pool and Immutability
Java stores string literals in a special memory area called the String Pool (inside the heap). If two literals have the same value, they share the same object.
String a = "hello";String b = "hello"; // points to the SAME object in the pool
String c = new String("hello"); // creates a NEW object in the heapStrings in Java are immutable — once created, their value cannot be changed. Any operation that “modifies” a string actually creates a new string object.
String s = "Java";s.concat(" Rocks"); // creates a new string, s is unchangedSystem.out.println(s); // Java
s = s.concat(" Rocks"); // reassign to capture the new stringSystem.out.println(s); // Java RocksWhy are Strings immutable?
- Security — strings are used for file paths, URLs, and credentials.
- Thread safety — multiple threads can share a string without synchronization.
- String Pool — sharing is only safe because values can never change.
Common String Methods
String s = "Hello World";
s.length(); // 11s.charAt(0); // 'H's.indexOf("World"); // 6s.contains("World"); // trues.startsWith("Hello"); // trues.endsWith("World"); // true
s.toUpperCase(); // "HELLO WORLD"s.toLowerCase(); // "hello world"s.trim(); // removes leading/trailing spacess.replace('l', 'p'); // "Heppo Worpd"
s.substring(6); // "World"s.substring(0, 5); // "Hello" (end index is exclusive)
s.split(" "); // ["Hello", "World"]String.join("-", "a", "b", "c"); // "a-b-c"String Comparison
Never compare strings with == — it compares references, not values. Use equals().
String a = "hello";String b = new String("hello");
System.out.println(a == b); // false (different objects)System.out.println(a.equals(b)); // true (same value)System.out.println(a.equalsIgnoreCase("HELLO")); // true
a.compareTo(b); // 0 if equal, negative/positive for lexicographic order| Comparison | What it checks |
|---|---|
== | Same object in memory (reference) |
equals() | Same character sequence (value) |
equalsIgnoreCase() | Same value, ignoring case |
compareTo() | Lexicographic (dictionary) order |
String Concatenation
String first = "Hello";String second = "World";
String result = first + " " + second; // using + operatorString result2 = first.concat(" ").concat(second); // using concat()
// With other types, + converts them to stringString info = "Age: " + 25; // "Age: 25"Concatenating strings inside a loop is slow, because each + creates a new object. Use StringBuilder instead.
StringBuilder and StringBuffer
StringBuilder is a mutable sequence of characters — it modifies the same object instead of creating new ones.
StringBuilder sb = new StringBuilder("Hello");
sb.append(" World"); // Hello Worldsb.insert(5, ","); // Hello, Worldsb.replace(0, 5, "Hi"); // Hi, Worldsb.delete(2, 3); // Hi Worldsb.reverse(); // dlroW iH
String result = sb.toString(); // convert back to StringString vs StringBuilder vs StringBuffer
| Feature | String | StringBuilder | StringBuffer |
|---|---|---|---|
| Mutability | Immutable | Mutable | Mutable |
| Thread-safe | Yes (immutable) | No | Yes (synchronized) |
| Performance | Slow for many changes | Fastest | Slower than StringBuilder |
| Use when | Value rarely changes | Heavy modification, single thread | Heavy modification, multiple threads |
// Bad: creates a new String object on every iterationString s = "";for (int i = 0; i < 1000; i++) { s += i;}
// Good: modifies the same StringBuilder objectStringBuilder sb = new StringBuilder();for (int i = 0; i < 1000; i++) { sb.append(i);}String result = sb.toString();Useful Conversions
// Number to StringString s1 = String.valueOf(42); // "42"String s2 = Integer.toString(42); // "42"
// String to numberint n = Integer.parseInt("42"); // 42double d = Double.parseDouble("3.14"); // 3.14
// String to char array and backchar[] chars = "hello".toCharArray();String s3 = new String(chars);String Formatting
String name = "Saurabh";int age = 25;
String msg = String.format("My name is %s and I am %d years old", name, age);System.out.printf("Name: %s, Age: %d%n", name, age);
// Text blocks (Java 15+) for multi-line stringsString html = """ <html> <body>Hello</body> </html> """;| Format specifier | Meaning |
|---|---|
%s | String |
%d | Integer |
%f | Floating point (%.2f → 2 decimal places) |
%c | Character |
%b | Boolean |
%n | New line |
Summary
- A
Stringis an object holding a sequence of characters, stored in the String Pool when created as a literal. - Strings are immutable — every modification creates a new object.
- Compare values with
equals(), never with==. - Use
StringBuilderfor heavy string modification (orStringBufferwhen thread safety is needed). String.format()and text blocks help build formatted and multi-line strings.