Tuesday, June 10, 2014

Top Java Strings Interview Questions

The favourite of the interview questions when it comes to Java. In my entire career I don't recall how many String questions have I asked and how many have I answered. It's like an ocean. The more you soak, the deeper you get.

This importance comes from the fact that Java designers too gave special significance to Strings. Strings are stored separately and plays and important roles in all programs. 

1. Where is String stored in Java?

String are considered special data type in Java. They stored StringPool which is a separate storage space on Java Heap.

2. What do we mean by "Spring is Immutable"?

All member variable data type is final. Hence String Object once created becomes immutable in Java. 

String literals are stored separately in a String Pool. Once a String is Created, say "javaonjava". Whenever a new variable needs "javaonjava" String, a new String is not created. The old one is referenced. This is what we mean by immutability. So String Object is not immutable, String itself is immutable.

Compare it with any User defined object. When that object is updated, content is re-written on the same memory space.

3. Is String a primitive Data Type?

No, String is not a primitive data type. String is a user defined data type. java.lang.String. 
Just like all other objects, Strings extend java.lang.Object. 
As we see in the String class, private final char value[]; stores the data of the String Object.

4. Is String in Java the same as in C?

No, In C, String is just a character array, terminated by null.
In Java String is a Class Object with multiple methods, like equals() to compare Strings, concat() to add two Strings, split() to split a String etc.

5. What does toString() method does?

toString() is an Object level method (java.lang.Object). It returns the String representation of any Object.

6. We see that String class is defined as final. Why?

The answer is the same, to make it immutable. As to Why, there are multiple reasons as to Why Java Designers made String final.

7. Can String be converted to int?

Yes. We have a special function parseInt() in Integer class for this purpose. Check the below code,

8. Can String be used with Switch/Case?

A serious drawback of Switch-Case was it just worked with expressions that evaluated to int (int, short, byte, char, Enum). Not anymore. With Java 7 it can take Strings too. Isn’t is awesome?
String fruit = "apple";
switch (fruit) {
case "orange":

9. How to compare two Strings?

There are multiple ways to compare Strings. But the one that we should follow is equals(). equals() checks if the data is equal. "==" just checks if String Objects are same or not.

10. What is String Pool in Java?

String Pool is a separate storage area on Heap Memory where String Objects are stored. Once a String is Created, say "javaonjava". Whenever a new variable needs "javaonjava" String, a new String is not created. The old one is referenced from String Pool

11. Why is char array preferred over String when it comes to saving password?

Strings as we now know are immutable. They are stored in String Pool and can't be erased till Garbage Collection kicks in. Hence application is susceptible to password leaks. Whereas in char[], each index can be set to null, when we want to erase the password.

12. What is intern() method used for?

In earlier versions of java, String created by new(), was not created in String Pool. intern() method, used to move it to String Pool.

13. Is String Thread safe?

Should be simple to answer by now. Since String is immutable, it cannot be modified. What is immutable is Thread Safe.

14. Differentiate between String/StringBuffer/StringBuilder.

thread safe
thread safe
not thread safe

No comments:

Post a Comment