Friday 15 December 2017

Difference Between String,StringBuffer and StringBuilder In Java?

String

String is immutable (once created cannot be changed)

The object created as a String is stored in the Constant String Pool

String is  thread safe because it cannot be used by two threads simultaneously.

String once assigned cannot be changed

String s1 = “LearningPoint92” 
// The object is stored in constant string pool and its value cannot be modified

Its performance is faster

String Buffer

String Buffer is mutable means one can change the value of the object

The object created through StringBuffer is stored in the heap

StringBuffer is synchronized because it cannot be used by two threads simultaneously.

StringBuffer is  thread safe due to synchronized nature.

StringBuffer s1 = new StringBuffer("Welcome To") ; 
// the object is stored in heap and its value can be changed .
s1=new StringBuffer ("LearningPoint92");
 // It is possible in StringBuffer because its value can be modified

StringBuffer  performance is slower 

StringBuilder

StringBuilder is mutable means one can change the value of the object

The object created through StringBuilder is stored in the heap

StringBuilder is nonsynchronized it can be used by two threads simultaneously.

StringBuilder isn’t thread safe due to nonsynchronized nature

StringBuilder s2= new StringBuilder ("Welcome To ") 
// The object is stored in the heap and its value can be modified
s2=new StringBuilder ("LearningPoint92"); 
// It is possible in StringBuilder because its value can be modified

StringBuilder performance is faster 

No comments:

Post a Comment

apply function in R

1) apply function: It takes 3 arguments matrix,margin and function.. Example: m<-matrix(c(1,2,3,4),nrow=2,ncol=2) m #1 indicates it is ap...