Java String Interview Questions and Answers
Java String Interview Questions and Answers
Answers
String is one of the most widely used Java Class. Here I am listing some important Java
String Interview Questions and Answers.
This will be very helpful to get complete knowledge of String and tackle any questions
asked related to String in interview.
4. Write a method that will remove given character from the String?
16. Why Char array is preferred over String for storing password?
When we create a String using double quotes, JVM looks in the String pool to find if any
other String is stored with same value. If found, it just returns the reference to that String
object else it creates a new String object with given value and stores it in the String
pool.
When we use new operator, JVM creates the String object but dont store it into the
String Pool. We can use intern() method to store the String object into String pool or
return the reference if there is already a String with equal value present in the pool.
if (str == null)
return false;
strBuilder.reverse();
return strBuilder.toString().equals(str);
Sometimes interviewer asks not to use any other class to check this, in that case we
can compare characters in the String from both ends to find out if its palindrome or not.
if (str == null)
return false;
System.out.println(length / 2);
return false;
return true;
}
Write a method that will remove given character from
the String?
We can use replaceAll method to replace all the occurance of a String with another
String. The important point to note is that it accepts String as argument, so we will
use Character class to create String and use it to replace all the characters with
empty String.
if (str == null)
return null;
Check this post for extensive details about String vs StringBuffer vs StringBuilder.
Read this post for benchmarking of StringBuffer vs StringBuilder.
It increases security because any hacker can't change its value and it's used for
storing sensitive information such as database username, password etc.
Since String is immutable, it's safe to use in multi-threading and we don't need
any synchronization.
Strings are used in java classloader and immutability provides security that
correct class is getting loaded by Classloader.
Check this post to get more details why String is immutable in java.
String s1 = "abc";
String s2 = "abc";
2. package com.journaldev.strings;
3.
5.
9. System.out.println(s1 = s2);
10. }
11.
}
It's a simple yet tricky program, it will print "PANKAJ" because we are assigning s2
String to s1. Don't get confused with == comparison operator.
14.
16.
18. System.out.println("String");
19. }
20.
22. System.out.println("StringBuffer");
23. }
24.
27. }
28.
The above program will not compile with error as "The method foo(String) is
ambiguous for the type Test". For complete clarification read Understanding the
method X is ambiguous for the type Y error.
29. What is the output of below code snippet?
System.out.println(s1 == s2);
It will print false because we are using new operator to create String, so it will be
created in the heap memory and both s1, s2 will have different reference. If we
create them using double quotes, then they will be part of string pool and it will
print true.
System.out.println(s1.equals(s2));
It will print false because s2 is not of type String. If you will look at the equals
method implementation in the String class, you will find a check
using instanceof operator to check if the type of passed object is String? If not,
then return false.
38. s2.intern();
System.out.println(s1 ==s2);
It's a tricky question and output will be false. We know that intern() method will
return the String object reference from the string pool, but since we didn't assigned
it back to s2, there is no change in s2 and hence both s1 and s2 are having
different reference. If we change the code in line 3 to s2 = s2.intern(); then
output will be true.
39. How many String objects got created in below code snippet?
Answer is 3.
First - line 1, "Hello" object in the string pool.
Second - line 1, new String with value "Hello" in the heap memory.
Third - line 2, new String with value "Hello" in the heap memory. Here "Hello"
string from string pool is reused.