Top 26 Java String Interview Questions and Answers

Here, we have listed the most important java string handling interview questions with short and pointed answers.

It will be very helpful for you to get complete knowledge of string and tackle any questions asked related to the String concept in the different company interviews.

String is the most widely used object in the world of programming language. Therefore, string is the most favorite topic for the interviewer.

Without making command on the string, you cannot crack any technical tests and interviews. So, prepares the answers to these string questions wholeheartedly.

Java String Handling Interview Questions with Answers

1. What is String in Java?

Ans: In Java, String is an object that represents a sequence of characters. For example, “Pencil” is a string of 6 characters. String is not a primitive data type like int and long.

2. Is String a class or data type in Java?

Ans: String is a class that is present in java.lang package. But all classes in java are also considered as data types. So, we can say a string as data type also.

3. How many ways can we create string object in Java?

Ans: Generally, there are two ways to create the string object in java. They are:

  1. By string literal
  2. By new keyword

By string literal: String literal is created by using double quotes. For example:

String s = "Hello";

The string literal is always created in the string constant pool. Whenever we create a string object using string literal, JVM checks string constant pool first.

If string already exists in the string constant pool, JVM will not create a new string object in string constant pool. JVM will point to the same string object by a reference to save memory.

But if the string does not exist in the string pool, JVM creates a new string object and store it in the pool.

By new keyword: It is the second way of creating string object in java.  It is just like creating an object of any class. It can be declared as:

String s = new String("Hello");

When we use new keyword to create an object of string class, JVM will create two objects. First, it will create an obejct in Heap memory and stores string “Hello” into the object and then will point a reference to object in the heap.

Now, JVM will create the second object as a copy for literal “Hello” in string constant pool for future purposes. There is no explicit reference variable pointing to the copy object in the pool.

4. What is String Constant Pool in Java?

Ans: String constant pool is a special memory area in heap which is used for storing string objects. Internally, the string class uses a string constant pool.

5. What is the meaning of Immutable in terms of String?

Ans: The meaning of immutable is unchangeable or unmodifiable. It means that once string object with value has been created, its value cannot be changed or modified.

But if we try to change with a new value, a new string object will be created by storing a new value. Hence, we cannot perform any changes with existing string object. This concept is called immutability in java.

6. Why string object is immutable in java?

Ans: String class is immutable and final in java because of several benefits. They are:

a) String is immutable in java because java uses the concept of string literal. Suppose there are five reference variables, all refer to the same object “Hello”.

If one reference variable of them changes the value of an object, it will affect all reference variables.

b) Java implements this immutability concept to minimize the duplication of string value.

c) It provides security because any hacker can not change its value. It helps to store sensitive information such as database username and password.

d) It is safe to use in multi-threading. So, we do not need any synchronization.

7. What is the superclass of string class in Java?

Ans: Object class is the superclass of string. String class extends object class.

8. What is String literal in java? How is string literal stored in memory?

Ans: String literal is a concept of Java language where string class is optimized to cache all the strings created in double-quotes.  They are stored in string constant pool.

9. Why Java uses the concept of string literal?

Ans: Java uses string literal concept to make it more memory efficient because no new object will be created if it already exists in the constant pool.

10. How are String literals cleared from String constant pool?

Ans: It is the responsibility of garbage collector to clear string object from the string constant pool.

11. What are the drawbacks of creating a large number of string literals in the memory pool?

Ans: Creating a large number of string literals in the memory pool can lead to memory leaks and performance issues.

12. How many objects will be created for the identical string?

Ans: Two identical string literal would create two separate string references but both will refer to the same object because string class is immutable in java.

13. How many total objects will be created in the following code?

String s1 = "Hello";
String s2 = "Hello";
String s3 = "Hello";

Ans: Only one object will be created in the string constant pool.

14. How many total objects will be created in the following code?

String s = new String("Hello");

Ans: A total of two objects will be created, one in the heap area and another in string constant pool.

15. How many total objects will be created in the following code?

String s1 = new String("Scientech");
String s2 = new String("Scientech");
String s3 = "Scientech";
String s4 = "Scientech";

Ans: A total of three objects will be created, two in the heap area and one in string constant pool.

16. Can we use reflection to clear a string object?

Ans: Yes, we can use reflection to clear string object from the memory but it is not recommended to do.

17. Why string class is declared as final in java?

Ans: String class has been marked as final so that we could not override the immutable behavior of string class.

18. How many interfaces are implemented by String class?

Ans: String class implements three interfaces as Serializable, Comparable, and CharSequence.

19. Does String is thread-safe in java?

Ans: Yes, String is thread-safe in java because string is immutable. So, we cannot change its value once created in the program.

This means it is thread-safe and can be safely used in a multi-threading environment. No synchronization is required for the string objects.

20. What is the disadvantage of string class in java?

Ans: String class can not be extended to get additional features.

21. Is String a wrapper class?

Ans: No, String is not a wrapper class.

22. How to compare two Strings in Java?

Ans: In Java, two strings can be compared on the basis of content and reference. Basically, there are three general ways by which we can compare strings.

  • By equals() method
  • By = = operator (double equal operators)
  • By compareTo() method.

23. What is the difference between = = (double equal operator) and equals method in Java?

Ans: The difference between double equal operator and equals method are as follows:

a) The = = (double equal operator) compares two object references, not characters. i.e. it compares the memory address of the object references.

Whereas, string equals() method is used to compare the original content of the string. It compares the two strings for equality.

b) In double equal operator, if both references are pointing to the same object then it will return true whereas, in equals method, if all the characters are the same, it will return true.

c) If both references are pointing to the different objects, it will automatically return false whereas, if any character is not matching, it returns false.

24. What will be the output of the following code?

public class Test {
public static void main(String args[])
 String s1 = "Hello";
 String s2 = "Hello";
 String s3 = new String("Good bye");
 String s4 = new String("Hello");


Ans: The output of following code is as follows:

  • True because content and case are the same.
  • False because content is not the same.
  • True because content and case are the same.
  • False
  • False

25. What will be the output of the below code?

public class Test {
public static void main(String args[]) 
 String s1 = "GOOD BYE"; 
 String s2 = new String("Good bye"); 

Ans: Output:

  • False because content is the same but the case is different.
  • True

26. What will be the output of the following program?

public class Test {
 public static void main(String args[]) 
  String s1 = "Cricket"; 
  String s2 = "Cricket"; 
  String s3 = new String("Cricket"); 

Ans: The output of this program is as follows:

  • True because s1 and s2 both are pointing to the same object created in the SCP area.
  • False because both have a different reference number.

Hope that this tutorial has covered all the important Java string interview questions with the best possible answers. These string handling interview questions in Java are very important for technical tests and interviews.

You must prepare the answers of these questions before going to interviews. Our team will update regularly more important interview questions.
All the best!!!

Leave a Comment