Q1. Tell us Hashcode function of string class?
Ans. Hash code h of a string s of length n is calculated as
Ans. Hash code h of a string s of length n is calculated as
Q2. If you want to extend the “java.lang.String” class, what methods will you override in your extending class?
Ans. You would be tempted to say equals(), hashCode() and toString() but the “java.lang.String” class is declared final and therefore it cannot be extended
Q3. What is an intern() method in the String class?
Ans. A pool of Strings is maintained by the String class. When the intern() method is invoked, equals() method is invoked to determine if the String already exist in the pool. If it does, then the String from the pool is returned. Otherwise, this String object is added to the pool and a reference to this object is returned. For any two Strings s1 & s2, s1.intern() == s2.intern() only if s1.equals(s2) is true.
Q4. What is the difference between StringBuffer and StringWriter?
Ans. StringWriter is a Writer, it is nothing like StringBuffer and the purpose of each is so far from the other that it would be easier to explain the similarities which would be relegated to similarities that exist between all Objects. You should use a StringWriter when you want a Writer and a StringBuffer when you need a mutable buffer for constructing Strings.
So StringWriter is basically an adapter that adapts a StringBuffer to the Writer interface. A StringWriter is simply an implementation of TextWriter that uses a StringBuilder internally for storage. So you use StringWriter with APIs that operate on TextWriters.
Q5. What is the difference between String, StringBuilder and StringBuffer?
Ans. String objects are immutable and thus can’t be changed. If you try to modify and existing String object, a new String object will be created and its reference will be assigned to existing object.
StringBuilder objects are like String objects, except that they can be modified. String builder gives an additional capacity of 16 (16 empty elements), to each object by default.Strings should always be used unless string builders offer an advantage in terms of simpler code or better performance. For example, if you need to concatenate a large number of strings, appending to a StringBuilder object is more efficient.
A number of operations (for example, append(), insert(), or setLength()) can increase the length of the character sequence in the string builder so that the resultant length() would be greater than the current capacity(). When this happens, the capacity is automatically increased.
There is also a StringBuffer class that is exactly the same as the StringBuilder class, except that it is thread-safe by virtue of having its methods synchronized.
Q6. What is difference between String and StringTokenizer?
Ans. A StringTokenizer is utility class used to break up string. For example:
StringTokenizer st = new StringTokenizer(”Hello World”);
while (st.hasMoreTokens()) {
System.out.println(st.nextToken());
}
Output:
Hello
World
Q7. Which design pattern is used by String class?
Ans. Flyweight. When you create a String constant as shown below:
String s3 = “A”;
s4= “A”;
It will check to see if it is already in the String pool. If it is in the pool, it will be picked up from the pool instead of creating a new one. Flyweights are shared objects and using them can result in substantial performance gains.
Q8. What happens when you use String concatenation operator?
Ans. When you are using String concatenation operator i.e "+", compiler creates a StringBuilder object to build the String s, and calls append() for each of the pieces. Finally, it calls toString() to produce the result. From this, it looks like we need not worry about using String or StringBuilder since compiler automatically takes care of that, but that's not true.
Ex: Say you are using "+" operator inside a loop; compiler will create a StringBuilder object every time inside a loop. However, as an efficient practice, you should create a StringBuilder outside the class.
Q9. Talk about Formatter class
Ans. Formatter is a translator that converts your format string and data into the desired result using format() method. When you create a Formatter object, you tell it where you want this result to go by passing that information to the constructor as below:
Formatter f = new Formatter(System.out);
Q10. What is difference b/w printf() and format()?
Ans. Both are same and produce a single format string.
Q11. What is the difference b/w format() method in String and Formatter class.
Ans. String.format() is a static method which takes all the same arguments as Formatter’s format() but returns a String. It can come in handy when you only need to call format() once.
Ans. You would be tempted to say equals(), hashCode() and toString() but the “java.lang.String” class is declared final and therefore it cannot be extended
Q3. What is an intern() method in the String class?
Ans. A pool of Strings is maintained by the String class. When the intern() method is invoked, equals() method is invoked to determine if the String already exist in the pool. If it does, then the String from the pool is returned. Otherwise, this String object is added to the pool and a reference to this object is returned. For any two Strings s1 & s2, s1.intern() == s2.intern() only if s1.equals(s2) is true.
Q4. What is the difference between StringBuffer and StringWriter?
Ans. StringWriter is a Writer, it is nothing like StringBuffer and the purpose of each is so far from the other that it would be easier to explain the similarities which would be relegated to similarities that exist between all Objects. You should use a StringWriter when you want a Writer and a StringBuffer when you need a mutable buffer for constructing Strings.
So StringWriter is basically an adapter that adapts a StringBuffer to the Writer interface. A StringWriter is simply an implementation of TextWriter that uses a StringBuilder internally for storage. So you use StringWriter with APIs that operate on TextWriters.
Q5. What is the difference between String, StringBuilder and StringBuffer?
Ans. String objects are immutable and thus can’t be changed. If you try to modify and existing String object, a new String object will be created and its reference will be assigned to existing object.
StringBuilder objects are like String objects, except that they can be modified. String builder gives an additional capacity of 16 (16 empty elements), to each object by default.Strings should always be used unless string builders offer an advantage in terms of simpler code or better performance. For example, if you need to concatenate a large number of strings, appending to a StringBuilder object is more efficient.
A number of operations (for example, append(), insert(), or setLength()) can increase the length of the character sequence in the string builder so that the resultant length() would be greater than the current capacity(). When this happens, the capacity is automatically increased.
There is also a StringBuffer class that is exactly the same as the StringBuilder class, except that it is thread-safe by virtue of having its methods synchronized.
Q6. What is difference between String and StringTokenizer?
Ans. A StringTokenizer is utility class used to break up string. For example:
StringTokenizer st = new StringTokenizer(”Hello World”);
while (st.hasMoreTokens()) {
System.out.println(st.nextToken());
}
Output:
Hello
World
Q7. Which design pattern is used by String class?
Ans. Flyweight. When you create a String constant as shown below:
String s3 = “A”;
s4= “A”;
It will check to see if it is already in the String pool. If it is in the pool, it will be picked up from the pool instead of creating a new one. Flyweights are shared objects and using them can result in substantial performance gains.
Q8. What happens when you use String concatenation operator?
Ans. When you are using String concatenation operator i.e "+", compiler creates a StringBuilder object to build the String s, and calls append() for each of the pieces. Finally, it calls toString() to produce the result. From this, it looks like we need not worry about using String or StringBuilder since compiler automatically takes care of that, but that's not true.
Ex: Say you are using "+" operator inside a loop; compiler will create a StringBuilder object every time inside a loop. However, as an efficient practice, you should create a StringBuilder outside the class.
Q9. Talk about Formatter class
Ans. Formatter is a translator that converts your format string and data into the desired result using format() method. When you create a Formatter object, you tell it where you want this result to go by passing that information to the constructor as below:
Formatter f = new Formatter(System.out);
Q10. What is difference b/w printf() and format()?
Ans. Both are same and produce a single format string.
Q11. What is the difference b/w format() method in String and Formatter class.
Ans. String.format() is a static method which takes all the same arguments as Formatter’s format() but returns a String. It can come in handy when you only need to call format() once.