1.

Differentiate between StringBuffer and StringBuilder classes in the context of Java.

Answer»

Strings are JAVA objects that are INTERNALLY supported by a char array. Strings are immutable because arrays are immutable (they can't grow). Every time you make a change to a String, a new String is produced. Java, on the other hand, has a number of classes that can be used to manipulate strings. StringBuffer and StringBuilder are two such classes.

StringBufferStringBuilder
The StringBuffer class is synchronized. This means that several threads cannot call the StringBuffer methods at the same time.The StringBuilder class is asynchronized. This means that several threads cannot call the StringBuilder methods at the same time.
StringBuffer is known as a thread-safe class because of its synchronisation.StringBuilder is not a thread-safe class due to its asynchronous nature.
StringBuffer is MUCH slower than StringBuilder due to synchronisation.StringBuilder is much faster than StringBuffer SINCE there is no preliminary check for multiple threads.


Discussion

No Comment Found