A string is an object which is used continuously in Java and this is why the String has exclusive handling in Java. Java has made the exclusive arrangement to store the string objects. String objects are stored in String Constant pool in heap memory.
- The String class signifies the character strings.
- Entire string literals in Java programs, such as “efg”, are applied as the instances of this class.
- Strings are mostly like constants which means once they are created then its value cannot be changed.
- String objects are actually immutable and therefore string objects can be shared.
- String Buffer & String Builder can be used in the position of String if a many String Operations has to be performed.
The questions related to Java String will be answered in the Java String interview questions stated below.
Ques1. What is a String in Java?
In a java.lang package string is a final class which is used to describe the set of characters in Java.
Ques2. Mention the two ways in which coder can create string objects in java?
The two ways in which coder can create string objects in Java are:-
Using new operator
Using the string literals
The objects which are created by using the new operator are stored in heap memory and the objects which are created by using string literals are stored in a string constant pool.
String s1 = new String(“lmn”); //Creation of String object by using new operator
String s2 = “lmn”; // Creation of string object by using string literal
Ques 3. What do you mean by String Constant pool?
The most used data objects in Java are the String objects. Therefore, Java has a specific provision to store string objects. String Constant Pool is one of the arrangements to store the string objects. String Constant Pool is the space in the heap memory which is particularly allocated for the string objects which are created through the string literals.
The main point about String Constant Pool is that two string objects cannot have the same content. When a developer creates a string object by using string literals then the JVM (Java Virtual Machine) completely checks the content of the new object that has to be created and if JVM finds the content of the new object matching the content of the existing object then JVM returns the reference of the existing object and the new object is not created and if the content is different then, a new string object is created.
This is an important Java String interview question.
Ques 4. What is so special about string objects?
What makes the string object special is that string objects can be created without the use of the new operator. It means strings objects can also be created by using string literals which is impossible with the other derived types except for the wrapper class.
Another feature of the string object is that coder can concatenate two strings with the use of + operator and java also gives special space for storing the string object in heap memory.
Ques 5. String, StringBuffer and StringBuilder are all classes but which one is the final class among them?
All three String, StringBuffer and StringBuilder are the final classes.
Ques 6. Mention the difference between String and StringBuffer
|1||string class is designed to have immutable objects||StringBuffer is designed to have mutable objects.|
|2||String class objects can be stored in pools for string constants or on a heap.||StringBuffer objects can be stored only on a heap.|
|3||String class can cause performance issues when used for frequent concatenations.||StringBuffer class gives your better performance compared to string class for concatenations.|
|4||It is slow and also consumes a lot of memory||It is fast and also consumes less memory|
An interviewer can ask this Java String interview question to confuse you
Ques 7. How to find a unique character in a string?
We can find a unique character in a string in two ways:-
- By Using a LinkedHashMap
- By Using an indexOf and lastIndexOf methods.
Ques 9. How mutable string objects can be created in Java?
Mutable string objects can be created by using StringBuffer class and StringBuilder class
Ques 10. Mention the number of objects that will be created in this code
String str1= “javastring”;
String str2= “javastring”;
In this code, only one object will be created and this object will get stored in the string constant pool.
Ques 11. Name the method to convert String to the char array in java.
toCharArray() method will be used to convert String to the char array in java
Ques 12. Compare StringBuffer and StringBuilder in java.
|1||Thread-safe||StringBuffer is completely threadsafe. Two threads can’t call methods of StringBuffer concurrently.||StringBuilder:-StringBuilder is not at all thread safe that means two threads can call methods of the StringBuilder concurrently.|
|2||Performance||since it is threadsafe therefore it is less efficient||since it is not thread safe therefore it is more efficient|
Ques 13. Mention the number of objects that will be created in this code
String str1= new String(“javastring”);
String str2= new String(“javastring”);
In this code three objects will be created. Two will be created in the heap memory and one will be created in the String constant pool.
Ques 14. How will you create the duplicate characters in String?
- Step 1:-Create a Hash Map and the character of the String will be inserted as key and then its count as a value.
- Step 2:-If the Hash map already has char, then increase its count by 1, or else put the char in Hash Map.
- Step 3:-If the value of Char is more than 1, this means it is a duplicate character in that String.
Ques 15. Mention the difference between equals () method and == operator?
The equals () method actually compares the content of the strings and on the other hand = = operator compares the reference or object of the strings.
Ques 16. Which one you will choose in between equals () method and == operator?
equals () method because it compares the content of the two string objects. This method delivers more logical evaluation of the two string objects.
“==” operator; it only compares the references of two objects. This might not be appropriate in all the circumstances.
This is one of the maximum asked Java String interview questions.
Ques 17. Why strings are made immutable in java? Explain
The security is increased because of Immutable strings. Immutable strings cannot be altered once they are created and therefore they are safe to store the sensitive data such as password, username, email address.
Since Immutable strings are thread safe therefore they can be used without any synchronisation in multi-threaded code.
String objects are used in loading the class and it may happen that sometimes wrongs class gets loaded. Since string objects are Immutable, wrong classes can be modified.
Ques 18. How can we check whether one String is the rotation of another String?
Let say we have to check whether string1 and string2 is a rotation of each another or not.
Step 1:-Create a new String string3 (str3= str1 + str1)
Step 2:- Now check if string3 encloses string2 or not.
Step 3:- if string3 encloses string 2 then string2 is the rotation of string1 otherwise it is not.
Ques 19. Explain the key methods in String class in Java?
The frequently used methods in String class in Java are:-
- toCharArray() – It returns an array of the characters of the string
- chatAt() – It returns the character at a particular index point of the string
- equals () – It returns true if the given string matches with the text of another string object.
- equalsIgnoreCase() – It returns true if the given string matches with the text of another string object overlooking the case of the string.
- length () – It returns the string length.
- subString() – It returns only a portion of the string. Coder has to state the start index and the end index.
- concat() – It appends the given string with another string
- split () – It splits the string which is based on a delimiter.
Ques 20. Explain the key methods in StringBuilder class & StringBuffer class in Java?
Some of the key methods in StringBuilder class & StringBuffer class are:-
- append() – It appends the given string with another string.
- insert() –It inserts to the given string another string. The code has to state the index at which the string has to be inserted.
- delete() – It deletes a portion of the string.
- reverse() – It reverses the string’s characters.
- toString() – It returns a string representation of the StringBuffer or the StringBuilder.
Ques 21:- How can we check if the two given strings are the anagrams of each other?
Definition of Anagram- Two strings are said to be the anagrams of each other if these two strings contain the equal count of each character. Example – “School master” is a pure anagram of “The classroom”
Assumption – Let’s assume that the upper case and spaces are important for the comparison. i.e. “School master ” is different from ” The classroom We can solve this problem two ways.
((Do share the assumption with the interviewer))
Strings can be checked in two ways:-
1. First Sort the given two strings and then check whether they are equal or not
2. First, check if the given two strings have the same count of each character or not.
Ques 22:- What is the algorithm to remove a particular character from the string?
The string is “Java string interview questions” and I have to be removed from this string.
- Step 1:-Loop over all the characters of the given string.
- Step 2:-Now check whether the character is ‘i’ or not.
- Step 3:-If the character is not ‘i’ then add the character to the StringBuffer object
- Step 4:-Then If the character is ‘i’ then ignore it
- Step 5:-Finally convert the StringBuffer to String and then finally print.
Go through this Java String interview Question thoroughly.
Ques 23:-How can we convert String to a byte array and then bye array to string?
String getBytes () method is used to convert the String to the byte array
String constructor new String (byte  arr) is used to convert byte array to String.
Ques 24:- What is the role of the String intern () method in Java?
When the intern method is applied, the string from the pool is returned if the pool already comprises a string which is equal to the given String object as determined by the equals (Object) method. Else, the given String object is added to the pool and then a reference to this added String object is returned.
Intern method always returns a String which has the same contents as the given string but is certain to be from a pool of unique strings.
Ques 25:- What will the output of this code:-
String s = “Welcome “;
s += “Students “;
s.trim ( );
The output of this code will be:-
Ques 26:- Compare between the string in C language and the string in the Java Language.
|S No.||String in C language||String in java language|
|1||It is a NULL terminated character Array||It is an object|
Ques 27:- Explain about StringJoiner in Java.
StringJoiner is actually a class which got introduced in Java 8 for joining the distinct strings into one, such as taking a list of colours and returning them as a comma-delimited string.
StringJoiner joiner = new StringJoiner (“,”, “[“, “]”);
assertEquals (“[Orange, Black, white]”, joiner.toString());
Ques 28:- How can you convert a string to integer and vice versa in Java?
- To convert String to Integer:-
int num = Integer.parseInt(“23”);
- To convert Integer to String:-
String Str = Integer.toString(num);
Ques 29:- How can you convert a string to uppercase and also to lowercase?
- String to Upper Case:-
String s = “interview questions”;
assertEquals(“INTERVIEW QUESTIONS”, s.toUpperCase(Locale.US));
- String to Lower Case:-
String s = “INTERVIEW QUESTIONS”;
assertEquals (“interview questions”, s.toLowerCase(Locale.UK));
Ques 30:- Can you explain how to check whether a string is a palindrome or not?
A palindrome is some sequence of the characters which reads exactly same backward as forward, such as “radar”, “Level”, “Nitin”
Whether a string is a palindrome or not:-
Start iterating the mentioned string forward & backwards in a single loop and only one character at a time. The loop exits at the very first mismatch.
Ques 31:- Are interned strings qualified for a garbage collection?
Yes, if there are no references from the program then all the Strings in the string constant pool are qualified for the garbage collection