Top String interview questions
Question 1 : What is String in java. Is string a data type.
Answer : It will be first question to start so your answer will be. String is not a primitive data type in java like long and int. String is a class in java which contain it’s content as character array. It resides in java.lang package. It provide different methods like equal,concat,substring etc. to provide character operations .
Question 2 : How to define String in java. What is string pool .
Answer : There are two ways to define string in java.
String s1 = new String("string1");
String s2 = "string2";
In first one we have created a new object of string class by having content as “string1”. We can also define string by only using double quotes as defined in second line. If he have not asked about string pool still it’s time that you mention stringpool to make your interview excited by telling difference between both methods. Difference between both in java is that in first one object is created in heap but in second way object will be created in string pool if only there is not a object having same content “string2” in pool. If there is an object having content “string2” in pool then JVM will point String s2 to the same object . Like in below code string3 and string2 of above code will point to same object in pool .
String s3 ="string2";
Note, benefit of string pool is that JVM is not creating so many objects for single content .
Question 3 : If two references are pointing to same object then what will happen when we perform operation on one referenced object.
Answer : Well now he wants to know if you know about immutability of string class. You will answer that as string is immutable(String class is final) in java so you can not change already created object. So if two reference will point to same object it will be same for both reference like s2 and s3 having above.
When you perform operation on s2 it will create a new object. If you want s2 to hold changed data then you have to assign s2 that new object.
s2 = s2.subString(0,1);
Now s2 have a separate object with content “s” and s3 still having object with content “string2”.
Question 4 : How can i put a string object created with new keyword in string pool.
Answer : To achieve that you have to execute intern() method on that object because by default that object will be in heap memory not in pool. This question will be asked by interviewer if he have knowledge about intern method.
Question 5 : How to compare two Strings in java program.
Answer : String class overrides equals method of object class to provide it’s own functionality and that equal method compare content of two string object and return true and false accordingly. Also there is equalsIgnoreCase method which compare string without caring about their upper case or lower case .It uses String CASE_INSENSITIVE_ORDER Comparator for case insensitive comparison.
Question 6 : How does substring works.
Answer : Most people have used this method in his life but many of us does not have idea how doest it work internally. In substring method string class convert first string to char array. Then it define one more char array of same length. Then it took characters from first char array to another char array on basis of given index in sustring method. Well if an interviewer is smart then he may ask if original string and substring are using same length char array then it may create memory leakage then answer is yes unintentionally but yes. Only when a very big string is converting into a very small string by substring method.
Question 7 : Can we use string inside switch case.
Answer : After java7 we can use string in switch case. But upto java6 we can not use string in switch case we have make if-elseif chain to acheive this.
Question 8 : Is string threadsafe.
Answer : Due to immutabilty string is thread-safe in java because thread can not change it and it’s value will be same for every thread.
Question 9 : What is difference between String,StringBuffer,StringBuilder.
Answer : Basic difference between all three is that string is immutable class but stringbuffer and stringbuilder are not.
Between stringbuffer and stringbuilder dufference is that buffer is thread safe so it’s slow performancewise respected to builder which is not thread safe.To know detailed discussion about them go to String vs StringBuffer vs StringBuilder.
Question 10 : What are other benefits of immutable functionality of string .
Answer : Due to being immutable is popular key inside Hashmap because it can not be changed it’s hashcode is cached at time of creation so it make best choice inside hashmap key because hashmap works searching on basis of hashcode.
Also we can use string to store password due to immutabilty hacker can not change it.
Other related posts are :
String vs StringBuffer vs StringBuilder
Top 10 spring interview questions and their answers. Spring basic concepts.
JPA Hibernate Sequence generator generating odd ID value and IntegrityConstraintVolation exception is coming
Set datasource spring bean properties dynamically if server is not available
Please share these posts on facebook,Linkeedin,Google+,Twitter by clicking on below icons.