String Class
The String class in Java represents a sequence of characters and is one of the most important and frequently used classes in Core Java. It is heavily tested in interviews and widely used in real-time applications.
What Is a String in Java?
- A String is an object that represents a sequence of characters
- Belongs to the java.lang package
- Immutable in nature
- Stored in String Constant Pool (SCP) or Heap
String s = "Java";
Why Is String So Important?
- Used for user input, messages, logs, data exchange
- Frequently used in APIs and frameworks
- Optimized for memory using String Constant Pool
- Core interview topic (immutability, == vs equals())
String Creation Ways (Very Important)
1. Using String Literal (SCP)
String s1 = "Java";
String s2 = "Java";
- Stored in String Constant Pool
- Same object reused
- Memory efficient
System.out.println(s1 == s2); // true
2. Using new Keyword (Heap)
String s3 = new String("Java");
String s4 = new String("Java");
- Stored in Heap memory
- Creates new object every time
System.out.println(s3 == s4); // false
String Immutability (Interview Favorite)
What Is Immutability?
- Once a String object is created, it cannot be changed
- Any modification creates a new object
String s = "Java";
s = s.concat(" World");
Result:
- "Java" remains unchanged
- New object "Java World" is created
Why Strings Are Immutable?
- Security (classpath, passwords)
- Thread safety
- Performance optimization via SCP
- Caching and hashcode stability
String Constant Pool (SCP)
- Special memory area inside heap
- Stores only unique string literals
- Improves memory efficiency
String a = "Test";
String b = "Test"; // reused from SCP
== vs equals() (Very Important)
Using ==
Compares references (memory address)
String a = "Java";
String b = "Java";
System.out.println(a == b); // true
Using equals()
Compares content
String c = new String("Java");
System.out.println(a.equals(c)); // true
Commonly Used String Methods
Length
int len = s.length();
Concatenation
String s = "Java" + " Programming";
String s2 = s.concat(" Language");
Character Access
char ch = s.charAt(1);
Comparison
s.equals("Java");
s.equalsIgnoreCase("java");
Substring
String sub = s.substring(0, 4);
Contains / StartsWith / EndsWith
s.contains("av");
s.startsWith("Ja");
s.endsWith("va");
Replace
String r = s.replace("Java", "Core Java");
Trim
String t = " Java ".trim();
Case Conversion
s.toUpperCase();
s.toLowerCase();
String Concatenation Internals
String s = "Java" + "World";
- Compiler optimizes literals
- At runtime, uses StringBuilder for variables
String vs StringBuilder vs StringBuffer
| Feature | String | StringBuilder | StringBuffer |
|---|---|---|---|
| Mutability | Immutable | Mutable | Mutable |
| Thread-Safe | Yes | No | Yes |
| Performance | Slow (modifications) | Fast | Slower than SB |
| Use Case | Read-only | Single-threaded | Multi-threaded |
Common Beginner Mistakes
- Using == instead of equals()
- Excessive string concatenation in loops
- Not understanding immutability
- Using new String() unnecessarily
- Ignoring performance impact
Interview-Ready Answers
Short Answer
String is an immutable class in Java used to represent a sequence of characters.
Detailed Answer
In Java, String is an immutable object stored in the String Constant Pool or heap. Any modification creates a new object. This design improves security, memory efficiency, and thread safety. Content comparison is done using equals() rather than ==.
Key Takeaway
The String class is simple on the surface but deep in behavior. Understanding immutability, SCP, memory behavior, and comparison methods is essential for writing efficient Java code and clearing interviews.