Java Interview Questions - String & StringBuffer
1. What is String and how String differ from C Lang?
String is a predefined class present in java.lang package. String class is used to create string object. Generally String is a sequence of character but in java String is an object.
2. What StringBuffer class?
StringBuffer class is used to create mutable String. it is same as String except it is mutable.
3. What is the difference between == operator and equals method in java?
The (==) operator compares references not value. WHERE Equals method compare the original content of the string which compares values of string for equality.
4. String is mutable or not,satisfy yes or no by program?
No. String is not mutable in java. because java support the concept of string literal. If one reference variable changes the value of the object , it will be affected to all the reference variable. That’s why String object are immutable in java.
5. StringBuffer is mutable or not, Describe by program?
StringBuffer class is mutable in java because it can be changed.
6. What is difference between compareTo () and compareToIgnoreCase ()?
compareTo () method compares two string and returns int. Where compareToIgnorcase () method compares two string ,ignoring case differences.
7. What is the difference between compareToIgnoreCase () and equalsIgnoreCase ()?
compareToIgnoreCase () method compares two string, ignoring case differences. WHERE equalsIgnoreCase () method compares the string to another string, ignoring case.
8. How (+) symbol works in case of String?
(+) symbol in case of string works as a concatenate operator.
9. If I do not provide any arguments on the command line, then the String array of Main method will be empty or null?
By default it is empty.
10. Why String cannot Inherit in java?
As String is a Final class in java ,so it cannot be inherited.
11. What is the difference between String and StringBuffer?
The difference between String andStringBuffer is String is immutable in nature but StringBuffer is mutable & faster in nature.
12. How String can construct?
String can construct by two ways:
- By String literal
- By new keyword.
13. What is String pool?
If String constructed by String literal it allocate memory from buffer which known as String pool.
14. Why String pool never store duplicate value?
As String pool is a temporary memory it never store any duplicate value.
15. Which two operator has special meaning in case of String?
“+’’ and “==’’ has special meaning in case of String.
16. How to convert String to StringBuffer & viceversa?
By passing argument inside the constructor we convert String to StringBuffer & viceversa.
17. What is the use of StringBuffer class?
StringBuffer class is used to create mutable(modifiable) String.
18. What are the commonly used constructor of StringBufferclass?
Commonly used constructors are:
- StringBuffer(int capacity).
19. How to create Immutable class?
Immutable class can be creating by creating final class that has final data member.
20. What is the use of toString () method?
If you want to represent any object as a String toString () method comes into existence.
21. Why java use concept of String literal?
To make java more memory efficient.
22. Why String object are immutable in java?
Because java use the concept of String literal i.e. if in any case one reference variable changes the value of the object, it will be affected to all the reference variable .that why String object are immutable in java.
23. How many ways to compare String object and what are they?
There are 3ways to compare String object. They are:
- By equals() method
- By == operator
- By compareTo() method.
24. Difference between public Boolean equals() and public Boolean equalsIgnoreCase() method?
Difference between public Boolean equals() and public Boolean equalsIgnoreCase () is Boolean equals() method compares the String to the specified object and Boolean equalsIgnoreCase () method compares the String to another String.
25. What are the ways for String Concatenation and describe them?
There are 2 ways for String Concatenation:
- By + (Sting Concatenation) operator
- (String Concatenation) operator is used to add String .this can Concatenate not only String but primitive values also. Concat () method Concatenates the specified String to the end of current String.
26. State the work of by compareTo() method?
compareTo () method compares value and returns as int which tells if the values compare less than, equal or greater than.
27. State the work of concat() method?
concat() method concatenates the specified string to the end of current string.
28. What is the difference between String toUpperCase() and String toLowerCase() method?
String toUpperCase() method converts all the character in this string to upper case. where String toLowerCase() metod converts all the character in the string to lower case.
29. State the work of String trim() method?
This method returns a copy of the string with leading and trailing whitespace omitted.
30. State the difference between Boolean startsWith() and booleanendsWith() method?
Boolean startsWith() tests if this string starts with the specified prefix. Where booleanendsWith() method test if the string ends with the specified suffix.
31. State the difference between synchronized StringBufferappend() and synchronized StringBuffer insert() method?
synchronized StringBufferappened() method used to append the specified string with the string. Where synchronizedStringBuffer insert() method used to insert the specified string with this string at the specified position.
32. State the difference between synchronized StringBufferreplace() and synchronized StringBuffer delete() method?
synchronized StringBuffer replace() method used to replace the string from specified startIndex and endIndex. Where synchronizedStringBuffer delete() method used to delete the string from specified startIndex and endIndex.
33. What is the difference between intcapacity() and void ensureCapacity() method?
int capacity() method used to return the current capacity. Where voidensureCapacity() method used to ensure the capacity at least equal to the given minimum.
34. Which method returns the character at the specified position?
public char charAt() method.
35. What is the use of intlength()?
This method used to return the length of the string.
36. What is the difference between creating String as new() and literal?
If the string created with String literal, it allocates memory from String pool which from stack area where as String created by new keyword then it allocates memory from heap area.
37. What is difference between StringBuffer and StringBuilder in Java?
StringBuffer is mutable means one can change the value of the object. The object created through StringBuffer is stored in the heap. StringBuffer has the same methods as the StringBuilder , but each method in StringBuffer is synchronized that is StringBuffer is thread safe.
StringBuilder is same as the StringBuffer, that is it stores the object in heap and it can also be modified. The main difference between the StringBuffer and StringBuilder is that StringBuilder is also not thread safe. StringBuilder is fast as it is not thread safe.