String StringBuffer Interview Questions
Read well String StringBuffer Interview Questions.
- What is a String in Java?
In Java, String does not terminate with \0. String is a class from java.lang package and can be manipulated with String class methods.
- How strings are immutable?
In case of variables, if a variable value is changed, the value in the location is changed but not the location itself. That is, varibale memory address does not change and it is the same before changing and after. But in case of strings, it is very different. When a string value is changed, the new value is stored in a new location and the old location with old value is garbage collected. This happens any number of times whenever a string is reassigned. For this behavior, the strings are said to be immutable (cannot be changed).
- When String exists, why it is necessary to introduce StringBuffer?
String’s immutable nature decreases performance. To overcome this, Java designers introduced StringBuffer as StringBuffer is mutable.
- Explain StringBuffer's mutability?
When a string stored in StringBuffer is changed, the location of the StringBuffer is not changed. There will be very less overhead of memory manipulation. This increases performance (there is StringBuilder also in Java).
Pass your comments and suggestions to improve the quality on this tutorial "String StringBuffer Interview Questions".