Introduction
Strings are one of the most widely used classes in Java. They are used to store and manipulate textual data. Unlike primitive data types, Strings in Java are objects of the java.lang.String
class. This article explores the String Pool, the concept of immutability, and common methods every developer should know.
What is a String in Java?
A String
in Java is a sequence of characters stored as an object. Java provides two ways to create Strings:
// Using String literal
String name = "Java";
// Using new keyword
String framework = new String("Spring");
Key Characteristics:
- Strings are immutable.
String
class is final and cannot be extended.- String objects are stored in a special memory region called the String Pool.
The Java String Pool
The String Pool is a special memory region inside the heap that stores string literals. When you create a string using literals, Java checks the pool:
- If the value exists, it reuses the reference.
- If not, it creates a new entry.
String s1 = "Hello";
String s2 = "Hello";
System.out.println(s1 == s2); // true (same reference from the pool)
Why String Pool?
- Saves memory by reusing objects.
- Improves performance in string-heavy applications.
String Immutability in Java
Java Strings are immutable, meaning once created, their value cannot change. Any modification creates a new String object in memory.
String lang = "Java";
lang.concat(" Programming");
System.out.println(lang); // Output: Java (original string unchanged)
Benefits of Immutability:
- Thread Safety: Immutable objects can be safely shared between threads.
- Security: Prevents accidental changes to critical string data like passwords.
- String Pooling: Works because strings don’t change after creation.
Commonly Used String Methods
Here are some essential methods provided by the String
class:
Method | Description | Example |
---|---|---|
length() |
Returns length of the string | "Java".length() → 4 |
charAt(int index) |
Returns character at given index | "Java".charAt(2) → 'v' |
substring(int begin) |
Returns substring from given index | "Programming".substring(6) → "mming" |
equals(Object o) |
Checks content equality | "Java".equals("java") → false |
equalsIgnoreCase() |
Checks equality ignoring case | "Java".equalsIgnoreCase("java") → true |
toUpperCase() |
Converts to uppercase | "java".toUpperCase() → "JAVA" |
toLowerCase() |
Converts to lowercase | "JAVA".toLowerCase() → "java" |
trim() |
Removes leading/trailing spaces | " Java ".trim() → "Java" |
replace(old, new) |
Replaces characters | "Java".replace('a','o') → "Jovo" |
split(regex) |
Splits string into array | "a,b,c".split(",") → ["a","b","c"] |
Real-World Use Case
Imagine a login system:
- You store usernames in a database as Strings.
- You validate inputs with
equalsIgnoreCase()
to avoid case sensitivity. - You trim user input to prevent leading/trailing spaces.
When to Use vs Avoid
✅ Use Strings:
- When working with small, fixed text values.
- For configuration keys, file paths, or identifiers.
❌ Avoid:
- For frequent modifications in loops. Use
StringBuilder
orStringBuffer
instead.
Java Version Relevance
- Java 7: Introduced String Pool inside the heap (previously in PermGen).
- Java 9+: Compact Strings optimization to reduce memory footprint.
Conclusion
Strings are a core part of Java, and understanding their immutability, String Pool behavior, and methods can significantly improve your code quality and performance. For mutable alternatives, consider using StringBuilder
.