String is Java's text type—immutable, UTF-16 internally. Concatenation with + is convenient; repeated concatenation in loops should use StringBuilder for performance.
Common operations
String s = " Hello ";
s.trim().toLowerCase();
s.length();
s.equals("hello"); // false — case sensitive
s.equalsIgnoreCase("hello");
s.contains("ell");
"Ada".substring(0, 2); // "Ad"
Text blocks (Java 15+)
String json = """
{"name": "Ada"}
""";
Important interview questions and answers
- Q: Why are Strings immutable?
A: Security (pooling, hashing), thread safety, and safe sharing across APIs. - Q: String vs StringBuilder?
A: String for fixed text; StringBuilder for efficient mutation in loops.
Self-check
- Which method compares String content?
- Why avoid
+in a tight loop building large text?
Tip: Strings are immutable—methods like trim() return new instances; assign the result if you need the trimmed value.