Java String Interview Questions and Answers - Extracted From JournalDev
Java String Interview Questions and Answers - Extracted From JournalDev
When we create a String using double quotes, JVM looks in the String pool to nd 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 don’t 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.
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 nd out if it’s palindrome or not.
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.
compareTo(String anotherString) method compares the String object with the String argument
passed lexicographically. If String object precedes the argument passed, it returns negative integer and if
String object follows the argument String passed, it returns positive integer. It returns zero when both the
String have same value, in this case equals(String str) method will also return true.
compareToIgnoreCase(String str): This method is similar to the rst one, except that it ignores the case. It
uses String CASE_INSENSITIVE_ORDER Comparator for case insensitive comparison. If the value is zero
then equalsIgnoreCase(String str) will also return true.
Check this post for String compareTo example.
Check this post for extensive details about String vs StringBu er vs StringBuilder.
Read this post for benchmarking of StringBu er vs StringBuilder.
Check this post to get more details why String is immutable in java.
String s1 = "abc";
String s2 = "abc";
String s3= new String("abc");
System.out.println("s1 == s2 ? "+(s1==s2)); //true
System.out.println("s1 == s3 ? "+(s1==s3)); //false
System.out.println("s1 equals s3 ? "+(s1.equals(s3))); //true
package com.journaldev.strings;
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.
package com.journaldev.strings;
The above program will not compile with error as “The method foo(String) is ambiguous for the type
Test”. For complete clari cation read Understanding the method X is ambiguous for the type Y error.
String s1 = "abc";
StringBuffer s2 = new StringBuffer(s1);
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 nd a check using instanceof operator to check if the type of passed object
is String? If not, then return false.
String s1 = "abc";
String s2 = new String("abc");
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 di erent reference. If we change the code in line 3 to s2 =
s2.intern(); then output will be true.
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.