Top 10 String interview question in Java

UPDATED: 10 February 2015
Top 10 String interview question in Java

What is String pool in Java?
Its the special area in Java heap to store the String literals like "javaQuery". When you create new String JVM first checks String in Pool, Its its available in Pool then it'll return the same object from Pool. If you create String using
new String("javaQuery") then it'll create the same object in Java heap not in String pool.


Why String is immutable or final in Java?
String objects are cached in String Pool and shared between multiple thread so it will lead to risk of value changed by one thread will affect value of other thread. This is one of the popular question in asked in Java interviews. This is explained with example and diagram Read more >>


What is the difference between str.equals("Vicky") and "Vicky".equals(str) in Java?
- name.equals("Vicky") Compare unknown value(name) with known value("Vicky").
- "Vicky".equals(name) Compare known value("Vicky") with unknown value(name) and its null safe. This is used to avoid java.lang.NullPointerException. Read more >>


What will be the output of following program? or Comparing two Strings in Java.
String str = "abc";
String str1 = "abc";
String str2 = new String("abc");

/**
 * '==' will compare the reference of two String.
 * str == str1 both points to same String in String Pool.
 */
System.out.println(str == str1); // true

/**
 * When we create String object using new String("abc"); then object will be created in Heap.
 * str belongs to String Pool and str2 belongs to Heap and they are not equal.
 */
System.out.println(str == str2); // false

/**
 * .equals() used with String compare the value of both String and they are equal.
 */
System.out.println(str.equals(str2)); //true

String vs StringBuilder vs StringBuffer
- String: When we have String that keeps changing then we have to use StringBuilder or StringBuffer and String is immutable.
- StringBuilder: Its fast compare to String and StringBuffer. However its not thread safe. StringBuilder is mutable.
- StringBuffer: Its slow compare to StringBuilder as its thread safe. StringBuffer is mutable.


How to reverse the String in Java? or How to check String is Palindrome or not in Java?
We can use StringBuilder.reverse() or StringBuffer.reverse() to reverse the String. Read more >>


How to convert String to char[] array and char[] array to String in Java?
- String to char[]: String.toCharArray()
- char[] to String:  String.valueOf(char[] data)
Read more >>


Why String is not good choice to store the password?
String is immutable, It means once you create the String it'll be available in memory for a long time. If one has access to memory dump then he/she can read the password from memory. char[] array is good choice for storing the password because you can wipe out the character array from memory.


How to move String object from Heap to String Pool?
String.intern() is used to move String object from Heap to String Pool.


Does String is thread-safe in Java?
Yes it it. String is immutable and we can't change value of String so it can be used in multi-threaded environment.


0 comments :