Java String – 50 Interview Questions and Answers
1. What is a String in Java?
A String is a sequence of characters represented by [Link]. It is immutable...
2. What does it mean that Strings are immutable?
Immutability means once a String is created, it cannot be changed...
3. What is the String Constant Pool (SCP)?
SCP is a special memory area inside heap which stores string literals...
4. Difference between String literal and new String()?
Literal goes to SCP, new goes to Heap...
5. Why String class is final?
To preserve immutability, security, and prevent overriding sensitive methods...
6. Why Strings are immutable? Give reasons.
Security, thread-safety, caching, hashing consistency, class loading...
7. What is string interning?
A method to store a single copy of String in SCP...
8. What is StringBuilder?
A mutable alternative to String...
9. Difference between String and StringBuilder?
String immutable, StringBuilder mutable, faster...
10. Difference between StringBuilder and StringBuffer?
Buffer is synchronized, Builder is not...
11. How compareTo() works?
Lexicographical comparison based on Unicode values...
12. How equals() works?
Checks content equality...
13. Why == should not be used?
Because it compares references, not content...
14. Explain substring().
Returns part of a string using index ranges...
15. How trim() works?
Removes leading and trailing spaces...
16. What is split()?
Splits string by delimiter...
17. replace() vs replaceAll().
replace uses simple matching, replaceAll uses regex...
18. Lexicographical order?
Dictionary-like ordering...
19. Can a String be empty or null?
Yes, empty "" and null are different...
20. Difference between "" and null?
Empty string is object, null is no object...
21. Convert String to int.
[Link]()...
22. Convert int to String.
[Link]()...
23. toCharArray().
Converts string to char array...
24. String concatenation.
Using + or concat()...
25. Why concatenation in loops is slow?
Creates many objects...
26. [Link]().
Used for formatted strings...
27. Unicode.
Java uses Unicode to support all languages...
28. Strings in switch-case?
Allowed since Java 7...
29. intern() method?
Returns pooled reference...
30. Ways to create Strings.
Literal, new, char[], byte[], builder, buffer...
31. GC behavior with strings.
Heap strings collectible, SCP also collectible after Java 7...
32. Why StringBuilder for JSON/XML?
Fast concatenation...
33. contentEquals() vs equals().
contentEquals supports CharSequence...
34. regionMatches().
Compares specific regions...
35. join() method.
Joins multiple strings with delimiter...
36. Escape characters.
\n, \t, \\, \"...
37. Can strings store emojis?
Yes, supported by Unicode...
38. startsWith().
Checks prefix...
39. endsWith().
Checks suffix...
40. matches() vs contains().
matches for regex, contains for substring...
41. Reverse string.
Using [Link]()...
42. Why StringBuilder reverse faster?
Works in-place...
43. Whitespace meaning.
Spaces, tabs, newlines...
44. Can a string be empty but not null?
Yes, ""...
45. isEmpty().
True if length==0...
46. isBlank().
True if only whitespace (Java 11)...
47. equalsIgnoreCase().
Case-insensitive comparison...
48. Why String good for HashMap keys?
Immutability ensures hashing stability...
49. Security uses of String immutability.
Used in file paths, URLs, no modification...
50. Compile-time concatenation.
Compiler combines literals into one...
What is Unicode?
Unicode is a universal character encoding standard that assigns a unique numeric value to every
character across all languages, including symbols and emojis. Java uses Unicode internally, allowing it
to represent multilingual text consistently across platforms.