Java Technical Question: Download Java Technical PDF

What is a StringBuffer class and how does it differ from String Class?

Tweet Share WhatsApp

Answer:

In contrast to the String class which implements immutable character strings, the StringBuffer class implements mutable character strings. Not only can the character string in a string buffer gets changed, but the buffer’s capacity can also change dynamically. The capacity of a String Buffer is the maximum number of characters that a string buffer can accommodate, before its size is automatically augmented.

Both String and StringBuffer class are thread-safe.

The syntax for initializing String class object is: String str1=”Hello”;

The syntax for initializing StringBuffer class object is:
StringBuffer sbr1= new StringBuffer (“hello”);
StringBuffer sbr2= new StringBuffer (10);
StringBuffer sbr3= new StringBuffer();

Download Java Technical PDF Read All 10 Java Technical Questions
Previous QuestionNext Question
Explain widening conversion and Narrowing conversion?How does the native language C or C++ understand data types in JAVA?