java2db

String vs StringBuffer vs StringBuilder in java



String vs StringBuffer  is one of the most popular interview questions.So the main differences are given below with an example.

 

String :

String vs StringBuffer String is a immutable class, their contents cannot be modified once you declared .

String vs StringBuffer If you try to change the String content , The JVM will copy the  content of the original String into a temporary space , make your changes , then save those whole changes into a new memory array .

 

StringBuffer :

String vs StringBuffer StringBuffere is mutable , so any change operation occurs on the existing memory location .

String vs StringBuffer It is a synchronized class , so it allows threads one after another to execute.

String vs StringBuffer Performance is bit low and reliable .

 

StringBuilder :

String vs StringBufferStringBuffer is also a mutable  class , operations can occur on the existing memory location .

String vs StringBufferIt is not a synchronized class , allows more threads to execute simultaneously .

String vs StringBuffer Performance is bit high , but the result will be inaccurate in few cases.

 

Simply we can say String vs StringBuffer and StringBuilder as follows .

String vs StringBuffer  Difference between String and StringBuffer : String is immutable , StringBuffer is mutable.

String vs StringBuffer  Difference between String and StringBuilder : String is immutable , StringBuilder is mutable.

String vs StringBuffer  Difference between StringBuffer and StringBuilder : StringBuffer is synchronized , StrignBuilder is not synchronized .

 

The data modification methods are almost same in both StringBuffer and StringBuilder . But those methods are not available in String .

 

Example for String vs StringBuffer :

Execution Result:

String vs StringBuffer StringBuffer is faster then String when performing simple concatenations .




Related Posts :
   [will not be published]



^ <