In this post, we will see questions asked on String during java interview
1. Explain String pool in java?
- String Pool is java string literals pool that JVM maintains a pool of unique strings
- when new string is created, JVM will check if an equivalent string exists in the pool. If it does, the reference to the existing string is returned, instead of creating new object
- Main purpose of string pool is to optimize java heap memory to maintain unique string constants in String Pool
2. Explain how string pool works ?
- JVM maintains a special memory area in the heap called the “String pool” for storing strings.
- String s = “jayaram”;
- In the above case, When new string is created using double quotes (” “) then JVM will check if any string”jayaram” already exists in the pool.
- If exists then reference to existing string will be returned
- If not exists then new string object is created and added to the pool
- When a string is created using the “new” operator, a new string object is always created, regardless of whether an equivalent string exists in the pool or not.
String s = "Jayaram"; // If Jayaram already exists in String pool then reference to that object will be returned String s1 = new String("Jayaram"); // Always new Object in Heap will be created and that will be referred to "Jayaram" String
3. In Enterprise or Large scale applications, How can String Pool can be used to Improve Performance?
This is one of tricky questions and answer is that you no need to implement any thing. Java String Pool will take care of this and follows as:
- Reducing memory usage: String pool ensures that two strings with the same value refer to the same object in memory, which helps reduce the overall memory usage of the application.
- Faster string comparisons: Comparing two strings in Java is done by comparing their references, not their contents. If two strings refer to the same object in the String pool, they are considered equal, and the comparison is done in constant time.
- Caching frequently used strings: Strings that are frequently used in an application can be added to the String pool, so that they can be retrieved quickly.
- String interning: String interning is process of adding a string to the String pool. Interning a string can be useful for performance optimization, especially for strings that are used frequently.
4. How does String Pool handled concurrent access in multi threading environment?
- String pool in Java does not handle multithreading but it still have impact on the behavior of a multithreaded application.
- Concurrent access: multiple threads can access concurrently and since pool holds a shared pool of unique strings, multiple threads may try to add the same string to the pool at the same time which leads to synchronzation issues.
- . When multiple threads trying to add the same string simultaneously then synchronization is required to avoid race conditions and ensure that only one copy of the string is added to the pool.
5. What is difference between the == and .equals() operators for comparing strings in Java ?
- The “==” operator compares object references, not the contents of the objects.
- equals method compares the contents of the two strings, not their references
6. Explain difference between a String and a StringBuffer in Java
- Immutability: String object is immutable, which means that once created then value cannot be changed. StringBuffer object is mutable, which means that its value can be changed after it has been created.
- Performance: Because strings are immutable, creating a new string requires the creation of a new object. This can lead to performance issues when concatenating strings, as multiple objects are created in memory. StringBuffer is designed to be mutable and provides efficient methods for concatenating strings, making it faster than String for string manipulation.
- Synchronization: StringBuffer is thread-safe where as String is not thread-safe
7. StringBuilder vs StringBuffer
StringBuilder and StringBuffer both used to manipulate strings
- Performance: StringBuilder is faster than StringBuffer as it is not thread-safe, meaning that it does not require synchronization when used in a single-threaded environment. “StringBuffer”, on the other hand, is thread-safe, meaning that it can be safely used in a multithreaded environment, but its slower performance is due to the overhead of synchronization
- Usage: StringBuilder is recommende in single-threaded environments and StringBuffer is recommended for use in multithreaded environments
- StringBuilder and StringBuffer provides same methods for string manipulation