It2EDU

Monday, November 21, 2016

Java Interview Questions in Strings


1) String is mutable or immutable in java?



Ans: In java all string are immutable. ( a mutable string can be changed but immutable string cannot be changed).



2) StringBuffer is thread safe or not?



Ans: A StringBuffer is thread safe because it is synchronized. StringBuffer object is mutable.



3)What is StringTokenizer?



Ans: StringTokenizer is a class in java.util package this allows the string into tokens (pieces).



4)What is StringBuffer?



Ans: The StringBuffer is a class provided by java which is used to represent characters that can be modified.



5)What is StringBuilder?



Ans: StringBuilder is class in java introduced in java5. It allows you to expand the number of characters that it encapsulates it you can specify a value for the maximum number of characters that it can hold. if you need to concatenate a large number of strings, appending to a StringBuilder object is more efficient.



6) What is String in java?



Ans: String is a class in Java and available in java.lang package.String is immutable and final in Java and JVM uses String Constant Pool to store all the String objects.



7) What is the operator is used to compare two strings?



Ans:  equals() or equalsIgnoreCase() method should be used to compare two string objects in Java.



8) How to create String objects in java?



Ans: Creating string objects in java is of two types.

          1.String s1 = new String("java");

          2. String s1="java";



9). What does intern() method do in Java?



Ans: String object created by new() operator is by default not added in String pool as opposed to String literal the intern() method allows putting string object into pool.



10) What is the difference when String is gets created using literal or new() operator ?



Ans: When we create string with new() its
created in heap and not added into string pool while String created
using literal are created in String pool itself which exists in Perm
area of heap.















  

0 comments:

Post a Comment