String Vs StringBuffer in Java

Wikitechy | 3865 Views | java | 13 Jun 2016

 

String:

  • In java String is immutable (once created cannot be changed) object. 
  • The object created as a String is stored in the Constant String Pool. 
  • String cannot be used by two threads simultaneously.
  • String once assigned cannot be changed.

For Example:

String  sample1= " wikitechy " ;
// The object is stored in constant string pool and its value cannot be modified.
sample1="wikitechy.com" ;    
//new "wikitechy.com" string is created in constant pool and referenced by the sample1 variable.            

So in this example " wikitechy " string still exists in string constant pool and its value is not overridden but we lost reference to the “wikitechy "string.

StringBuffer:

  • StringBuffer is mutable once we change the value of the object. 
  • The object created through StringBuffer is stored in the heap. 
  • StringBuffer does not allow two threads to simultaneously access the same method.
  • Each method can be accessed by one thread at a time.
  • StringBuffer value can be changed, which means that it can be assigned to the new value. 
  • String Buffer can be converted to the string by using toString() method.

For Example:

StringBuffer sample1 = new StringBuffer("wikitechy") ;
// The above object stored in heap and its value can be changed.
sample1=new StringBuffer("wikitechy.com");
// Above statement is right as it modifies the value which is allowed in the StringBuffer

Difference Between the String & StringBuffer:

StringBuffer concatenation is significantly faster than String concatenation. 

StringBuffer is used in this type of operation.

1. The first and most significant difference between String and StringBuffer in Java is String is immutable in Java while StringBuffer is mutable.

String str = new String ("wikitechy “);
str += "java article!!";
  • Which means that if you perform any operation on String it will create new String object that can be modifiable. 
  • But in StringBuffer we cannot create an object using new String object

2. If we are using + operator for concatenating multiple Strings, then we should not be worried much because based upon Java implementation call to the + operator, it is replaced with either StringBuffer or StringBuider based upon JVM (Java Visual Machine) implements Java 1.5 or lower version.

StringBuffer  str = new StringBuffer  ("wikitechy  ");
Str.append = "java article!!";
3. StringBuffer.append() method is used to perform String concatenation in Java.

4. Creating StringBuffer from String is easy, as StringBuffer accept a String input. Similarly converting StringBuffer to String is also easy by using toString() method in Java.

5. Another significant difference between String and StringBuffer is that StringBuffer and String do not share same type hierarchy, which means we cannot cast String to StringBuffer in Java (any such attempt will result in ClassCastException in Java).

Applies to:

  • J2SE 1.3
  • J2SE 1.4
  • J2SE 5.0
  • Java SE 6
  • Java SE 7
  • Java SE 8

Related Tags:

  • Difference between String and StringBuffer
  • Difference Between String , StringBuilder and StringBuffer Classes
  • Difference between String and StringBuffer
  • StringBuffer versus String
  • Difference between String and StringBuffer in Java
  • String vs StringBuffer vs StringBuilder in Java 
  • String vs stringbuffer vs stringbuilder 
  • What is the difference between String and StringBuffer in Java?
  • java - String, StringBuffer, and StringBuilder




Workshop

Bug Bounty
Webinar

Join our Community

Advertise
<