Java String Interview Questions and Answers

Java String Interview Questions and Answers

1) What is the difference between creating an object of string class using the new operator and using doubles quotes?
A) Using double quotes object of the string class representing a specific sequence of characters could be created only once. Whereas using new operator we can create any number of String class objects representing the same sequence of characters.

2) Explain about concatenation operator (+) in Java Strings?
A) The functionality of the concatenation operator is to create a new String class object with the data present at the left-hand side and right-hand side.

3) What is String Buffer?
A) String Buffer is like a String but can be modified in the same memory location. Every method of the String Buffer class would be changing the data within the same object and returns the address of the same object.

4) What is the String Builder?
A) String Builder is a mutable sequence of characters. String Builder class is designed for use as a drop-in replacement for StringBuffer in places where the String buffer was being used by a single thread.

5) What is the default capacity of String?
A) Strings do not have default capacity. The passed String length is its capacity.

6) What is the default capacity of String Buffer?
A) String Buffer has default capacity as 16 buffers. It increases its capacity automatically when sizes reach maximum using its capacity formula ( ( current capacity * 2 ) + 2).

7) In how many ways we can concat String in Java?
A) Using String object, we can concat new string to the current in two ways.
1) using + operator.
2) using concat() method.

8) In how many ways we can concat String Builder in Java?
A) Using String Buffer we can perform concat operation only in one way.
1) using append() method.

9) What is toString() method?
A) If you want to represent an object as a string, toString() method is used.

10) What is the advantage of the toString() method?
A) By overriding the toString() method of the object class, we can returns the values of the object.

11) What is the use of the charAt() method?
A) CharAt() method returns the character at the specified location.
String name = “TutorialsWeb”
Char ch = name.charAt(7);

12) What are the interfaces String class Implements?
A) String class implements “3” interfaces in Java. They are:
1) Serializable.
2) charsequence.
3) comparable.

13) Is String class final in Java?
A) Yes. String class is declared as final in Java.

14) What is difference between equals() and “ == ” operator?

A) The equals() method compares the characters inside a String object. The “==” operator compares two object references to see whether they refer to the same instance.

15) Is String thread-safe in Java?
A) Since String is immutable, it is threaded safe and it can be shared between multiple threads without external synchronization.

16) Why java uses the concept of String literal?
A) To make Java more memory efficient, because no new objects are created if it exists already in string constant pool.

17) What is the String Constant pool (String literal pool)?
A) String Constant pool is a separate block of memory where the string objects are held by JVM.

18) Explain the difference between the following statements?
String s = “Hello”;
String s = new String(“Hello”);
A) In first statement assignment operator is used to assign the string. In this case, JVM first checks the String pool whether the same value is already available in the String container or not, if available then it creates another reference to it, if not available then it creates the new one.
In the second case each and every time it creates a new object of string.

19) How to concat two different String?
A) We can concat two different Strings with the help of “+” assignment operator or concat() method of String class.

20) What do you mean by immutability?
A) Immutability means once an object is initialized we can’t change.

21) What is the String pool in Java?
A) String pool is a special storage area in java heap, mostly located on Bergen space, to store string literals like “ABC”. When Java program creates a new string using String literal, JVM checks for that string in the pool and, if string literal is already present in the pool then the same object is returned instead of creating a whole new object. String pool check is performed only when you create a string as literal, if you create String using the new() operator, a new String object will be created even if String with the same content is available in the pool.

22) How to make a class immutable in java?
A) 1) Make a class final or private.
2) Remove constructor.
3) Allow only the getter method in your class.

23) What is the difference between String and StringBuffer classes?
1) String class object is immutable hence there contain cannot be modified.
2) The method that directly manipulates data of the object in String class.
3) Append(),reverse() method not available in String class.


1) StringBuffer object is mutable so they can be modified.
2) Such methods are available in String class.
3) Append(),reverse() method available in StringBuffer class.

24) There are any other classes in java that is immutable?
A) All the wrapper classes in Java are immutable like Integer, Float, Double etc.

25) What is the String Tokenizer class?
A) String Tokenizer class is used to break the string in the form of tokens.

26) There is an alternative to string Tokenizer?
A) The String class is an alternative of string tokenizer, with the help of split() method of string.

27) What is the difference between StringBuffer and StringBuilder?
A) StringBuffer is synchronized and StringBuilder is not.

28) How to convert Integer to String?
A) With the help of toString() method of String class.

29) The string is Thread safe or not?
A) Yes, String is thread safe because string object is immutable that’s way string is thread safe.

30) What is difference between compareTo() and equals() method?
A) compareTo(): compareTo() method is provided by java.lang.String package that checks the difference string to the character by character if both the strings are same then return 0 otherwise it returns (-value) for each character.
Equals(): Equals() method is a java.object package Object class method that checks the two different object and returns true or false.

31) How to convert Integer to String?
A) There are many ways but two ways are shown below:
1) String s = 7 + “ ”;
2) String s = String.valueOf(7);

In the first case, JVM consumes a lot of space for memory. In the second case, JVM does not consume memory.

Leave a comment

Your email address will not be published. Required fields are marked *