Top 50 Java String Interview Questions And Answers

What is the String Pool?

A specially designed memory area for the String literals/objects. String Pool is a pool of Strings stored in Java heap memory. Instead of creating a separate object for every requirement, we can create only one object and we can reuse the same object for every requirement. This approach improves performance and memory utilization.

What is the disadvantage of SCP?

In SCP as several references pointing to the same object by using one reference if we are performing any changes, the remaining references will be inflected. To prevent this compulsory String objects should be immutable. That is immutability is the disadvantage of SCP.

Why SCP like concept available only for the String but not for the StringBuffer?

As String object is the most commonly used object sun people provided a specially designed memory area like SCP to improve memory utilization and performance.

But StringBuffer object is not commonly used object hence specially designed memory area is not at all required.

Why String objects are immutable whereas StringBuffer objects are mutable?

In the case of String as several references pointing to the same object, by using one reference if we are allowed to perform the change the remaining references will be impacted. To prevent this once we created a String object we can't perform any change in the existing object that is immutability is only due to SCP.

But in the case of StringBuffer for every requirement we are creating a separate object will be created by using one reference if we are performing any change in the object the remaining references won't be impacted hence immutability concept is not required for the StringBuffer.

Similar to String objects any other objects are immutable in java?

In addition to String objects, all wrapper objects are immutable in java.

Is it possible to create our own mutable class?

Yes. We can create it.

What is the interning of String objects?

By using the heap object reference, if we want to get the corresponding SCP object, then we should go for the intern() method.

package com.javacodestuffs.core.strings; public class StringInternExample1 { public static void main(String[] args) { String s1 = new String("Abhijit"); String s2 = s1.intern(); System.out.println(s1 == s2); //false String s3 = "Abhijit"; System.out.println(s2 == s3); //true } } output: false true

If the corresponding object is not there in SCP then the intern() method itself will create that object and returns it.

package com.javacodestuffs.core.strings; public class StringInternExample2 { public static void main(String[] args) { String s1 = new String("Abhijit"); String s2 = s1.concat("Telecom"); String s3 = s2.intern(); String s4 = "AbhijitTelecom"; System.out.println(s3 == s4); //true } } output: true

Other than immutability and mutability is there any other difference between String and StringBuffer?

In String .equals( ) method meant for content comparison whereas in StringBuffer meant for reference comparison.

Can we use String in a switch statement?

Yes, you can use String in switch statements in java 7. Prior to java 7, you had to use if-else statements to achieve the task.

What is the difference between StringBuilder and StringBuffer in java?

1). StringBuilder is not thread-safe. StringBuffer is a thread-safe.
2). StringBuilder is not synchronized and StringBuffer is synchronized.
3). StringBuilder is faster while StringBuffer is slower as it is thread-safe

How will you create your own immutable class in java?

for creating the immutable class we have to do the following steps:

1). Declare all mutable fields as final so that it values can be assigned only once.
2). Make all fields private so that no one can access them from outside the class.
3). Do not provide setter methods for the variables
4). Make the class final so it can not be extended.

How many objects will be created for the following code.

String str1 = "Abhijit"; String str2 = new String("Abhijit");

Two objects are created. The object created using a new operator is stored in the heap memory (str2).

The object created using String literal str1 is stored in the string constant pool.

Explain the difference between the below statements

String str = new String("Abhijit"); String str = "Abhijit";

In the first case,

String str = new String("Abhijit");

JVM will create one object in the heap memory.also put object in the String constant pool, if the object is not present.if present in the String constant pool ,it will return the reference to it.

In the second case,

String str = "Abhijit";

JVM checks for the string "Abhijit" in the String constant pool. If the string is not present in the constant pool then it will create a new String object and stores it in the pool.

If the string "Abhijit" is found in a string constant pool, then it simply creates a reference to it but does not create a new object.

How many objects will be created for the code given below?

String str1 = "Abhijit"; String str2 = "Abhijit";

Only one object is created. String str1 will create a new object in String constant pool, while String str2 will create a reference to the String str1.

How many objects will be created for the code given below?

String str1 = new String("Abhijit"); String str2 = new String("Abhijit");

3 objects will be created. For the first statement(str1) two objects will create one in String constant pool and one in heap memory.

But for the second statement(str2), compulsory 1 new object is created in heap memory but no new object is created in string constant pool as it is already present.

Hence, total of 3 objects will be created.

Why is String immutable in java?

There are various reasons to make String immutable in java.

1. Security: String is made immutable to help increase the Security. Sensitive data like username, the password can be stored as the Strings can't be modified once created.

2. Classloading: String objects are used for Classloading. It is possible that the wrong class has been loaded in the JVM if the String is mutable i.e modifiable.

3. ThreadSafe: Immutable Strings are thread-safe. Synchronization is not required when we use them in the multithreading environment.

How to find the character in string in java?

package com.javacodestuffs.core.strings; public class StringInternExample2 { public static void main(String[] args) { String str = "Hello"; char ch1 = 'o'; for (int i = 0; i < str.length(); i++) { char ch = str.charAt(i); System.out.println((ch == ch1) ? "character found": "character not found"); } } } output: character not found character not found character not found character not found character found

Post/Questions related to  Top 50 Java String Interview Questions
And Answers

What is the string in java?
How to get a list of string in java?
How to remove part of a string in java?
How to find all occurrences of the string in a string java?
How to get the value of the string in java?
Reverse A String In Java.

In this article, we have seen the  Top 50 Java String Interview Questions And Answers with examples. All source code in the article can be found in the GitHub repository.