String Interview Questions in Java
Names, roll numbers, addresses, messages, file paths: almost every program spends a lot of its time working with text. That is why String is the class interviewers ask about most, and why its quirks (the string pool, immutability, == versus equals) make such popular trick questions. This page collects the String questions asked most often, from basic to advanced, plus the string programs that come up in coding rounds. Answer each one aloud, then reveal the answer.
Basic Questions
What is a String in Java?
An object of the java.lang.String class that holds an immutable sequence of characters. String literals such as "Navika" are stored in the string pool and shared.
What are the ways to create a String?
With a literal (String s = "Java";), which reuses a pooled object, or with new (new String("Java")), which always creates a new object on the heap. Strings also come from methods such as String.valueOf, substring and StringBuilder.toString.
What is the difference between == and equals() for strings?
== checks whether two references point to the same object. equals() checks whether the characters are the same. Two equal strings created at runtime are usually different objects, so always use equals (or equalsIgnoreCase).
Is String a primitive type?
No. It is a class, and String variables hold references. Java gives it special support: literals, the + operator for joining, and the string pool.
Intermediate Questions
Why is String immutable?
So pooled strings can be shared safely; so the hash code can be cached, making strings fast and reliable HashMap keys; for thread safety without locks; and for security, since file names, URLs and class names can't be changed after they are checked.
What is the string pool?
A special area on the heap where the JVM keeps one copy of each string literal. When code uses a literal that already exists in the pool, it gets the same object instead of a new one, which saves memory.
What does intern() do?
It returns the pooled copy of a string, adding the string to the pool if it isn't there yet. After s = s.intern(), s == "literal" is true when the text matches.
What is the difference between String, StringBuilder and StringBuffer?
String is immutable. StringBuilder and StringBuffer are mutable and can be changed in place. StringBuffer's methods are synchronized (thread-safe but slower); StringBuilder isn't. Use StringBuilder for building strings in a loop on one thread.
How many objects does String s = new String("Java") create?
Up to two: the literal "Java" in the pool (if it isn't there already) and a new String object on the heap that s points to.
Advanced Questions
Why is a char[] preferred over a String for passwords?
A String stays in memory until it is garbage collected and can't be cleared, so it might show up in a memory dump. A char[] can be overwritten with zeros as soon as the password has been checked.
How does string concatenation with + work in a loop?
Each += creates a new String and copies the old characters, so building a long string in a loop is O(n²). The compiler optimizes + within a single statement, but not across loop iterations. Use StringBuilder there.
What are compact strings?
Since Java 9, a string whose characters all fit in Latin-1 is stored with one byte per character instead of two, roughly halving memory for most text. It is automatic and invisible to your code.
Which String methods were added in Java 11 and later?
isBlank, strip, stripLeading, stripTrailing, lines and repeat (11); indent and transform (12); formatted (15). strip understands all Unicode white space, unlike the older trim.
The Answers in Code
Let's see the pool, intern, mutability and a common string program together.
public class StringFacts {
public static void main(String[] args) {
String a = "Navika";
String b = "Navika";
String c = new String("Navika");
String d = "Nav" + "ika"; // joined at compile time
String part = "Nav";
String e = part + "ika"; // joined at runtime
System.out.println(a == b);
System.out.println(a == c);
System.out.println(a == d);
System.out.println(a == e);
System.out.println(a == e.intern());
System.out.println(a.equals(e));
StringBuilder sb = new StringBuilder("Advitha");
sb.append(" Anamaneni").insert(0, "Hi ");
System.out.println(sb);
String word = "level";
boolean palindrome = new StringBuilder(word).reverse()
.toString().equals(word);
System.out.println(word + " palindrome? " + palindrome);
}
}true
false
true
false
true
true
Hi Advitha Anamaneni
level palindrome? trueExplanation:
a,banddare the same pooled object:"Nav" + "ika"is a constant expression, so the compiler joins it into one literal.eis built at runtime from a variable, so it is a new object;==is false until it is interned.equalsis true whenever the text matches.StringBuilderchanges in place:appendandinsertmodify the same object and return it, so calls can be chained.
Interview Corner
Which string programs should you be ready to write?
Reverse a string, check a palindrome, count vowels and consonants, count character frequency, check anagrams, find the first non-repeated character, and remove duplicate characters. All appear in "Java Programs for Practice".
How do you reverse the words of a sentence?
Split on spaces, reverse the array or list, and join: String.join(" ", reversedList). Mention strip() and splitting on "\\s+" to handle extra spaces.
Practice
- Easy: Predict, then run:
String x = "ab"; String y = x + ""; System.out.println(x == y); - Harder: Write a method that removes duplicate characters from "anamaneni" while keeping the first occurrence of each (answer: "anmei").
Key Takeaways
Was this tutorial helpful?