String StringBuffer Interview Questions


String StringBuffer Interview Questions

Read well String StringBuffer Interview Questions.

  1. 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.
  2. 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).
  3. 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.
  4. 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".

10 thoughts on “String StringBuffer Interview Questions”

  1. my self sundar.sir, i read java from yure blog but i want spring and webservice notes like yure blog with easily undestable format.plz suggest me…i am not easily to go with internet first basic plz suggest me .

  2. Hi Sir,

    In the case of strings,we are having literal pool,the same concept is not available in other wrapper classes.

    Please update me the reason.

  3. actually when compile your program thread suspend that the message are occure what type of exception i cant understand plz help me….
    Note: mth4.java uses or overrides a deprecated API.
    Note: Recompile with -Xlint:deprecation for details.

Leave a Comment

Your email address will not be published.