String StringBuffer and StringBuilder.

String and string buffer

 

We are going to discuss the difference between String and StringBuffer and StringBuilder.

1.Differences between String and StringBuffer.

String: Once we create a string object we can’t perform any changes in the existing object. If we try to perform any changes with those changes a new object will be created.  This non-changeable is nothing but the immutability of the string object. we can’t change the sting object once we create.

  1. String class is immutable, which means it can’t change.
  2. It is slow, because every time it creates new instance, so it is consuming more memory when you concat too many strings.
  3. String class overrides the equals() method of Object class.

 

Programing Example of String.

package com.technicalRound;

class Example {
public static void main(String args[]) {
String s = "Niraj";
s.concat(" Singh");// concat() method is appends the string at the end of existing object
System.out.println(s);// It will print Niraj
// because strings are immutable objects that's why it not appends
}
}

Output:

Niraj

 

StringBuffer: Once we create a StringBuffer object we can perform any type of changes in the existing object.
This changeable is nothing but the mutability of the string object. We can change the StringBuffer object after the creation of the object.

  1. StringBuffer class is mutable, which means it can change.
  2. It is fast and consumes less memory when you concat two strings.
  3. StringBuffer class doesn’t override the equals() method of Object class.

Programing Example of StringBuffer.

package com.technicalRound;

class Example {
public static void main(String args[]) {
StringBuffer sb = new StringBuffer("Niraj");
sb.append(" Singh");// append() method is appends the StringBuffer at the end of existing object
System.out.println(sb);// It will print Niraj Singh
// because StringBuffer are mutable objects that's why it appends
}
}

Output:

Niraj Singh

 

2.Differences between StringBuffer and StringBuilder.

StringBuilder is exactly the StringBuffer (including Method and Constructors) except the following differences.

StringBuffer:

  1. Every method present in StringBuffer is synchronized.
  2. At a time only one thread is allowed to operate on StringBuffer object. Hence StringBuffer object is Thread safe.
  3. It increases waiting time of threads and hence relatively performance is low.
  4. Introduced in java 1.2 version.

 

StringBuilder:

  1. Every method present in StringBuilder is not synchronized.
  2. At a time multiple threads are allowed to operate on StringBuilder object. Hence StringBuilder object is not thread-safe.
  3. Threads are not required to wait to operate on StringBuilder and hence relatively performance is high.
  4. Introduced in Java 1.5 version.

 

Programing Example of StringBuilder.

package com.technicalRound;

class Example {
public static void main(String args[]) {
StringBuilder sb = new StringBuilder("Niraj");
sb.append(" Singh");// append() method is appends the StringBuilder at the end of existing object
System.out.println(sb);// It will print Niraj Singh
// because StringBuilder are mutable objects that's why it appends
}
}

Output:

Niraj Singh

 

When to use String, StringBuffer, and StringBuilder?

  1. If the content is fixed, and won’t change frequently then we should go for String.
  2. when the content is not fixed and keep on changing but thread-safe is required, then we should go for StringBuffer.
  3. If the content is not fixed and keeps on changing but thread-safe is not required, then we should go for StringBuilder.

 

Programming Example of String, StringBuffer, and StringBuilder.

package com.technicalRound;

public class Example{ 
public static String concatWithString() { 
String t = "Java"; 
for (int i=0; i<10000; i++){ 
t = t + "TechnicalRound"; 
} 
return t; 
} 
public static String concatWithStringBuffer(){ 
StringBuffer sb = new StringBuffer("Java"); 
for (int i=0; i<10000; i++){ 
sb.append("TechnicalRound"); 
} 
return sb.toString(); 
} 
public static String concatWithStringBuilder(){ 
StringBuilder sb = new StringBuilder("Java"); 
for (int i=0; i<10000; i++){ 
sb.append("TechnicalRound"); 
} 
return sb.toString(); 
} 

public static void main(String[] args){ 
long startTime = System.currentTimeMillis(); 
concatWithString(); 
System.out.println("Time taken by Concating with String: "+(System.currentTimeMillis()-startTime)+"ms"); 
startTime = System.currentTimeMillis(); 
concatWithStringBuffer(); 
System.out.println("Time taken by Concating with StringBuffer: "+(System.currentTimeMillis()-startTime)+"ms");
startTime = System.currentTimeMillis(); 
concatWithStringBuilder(); 
System.out.println("Time taken by Concating with StringBuilder: "+(System.currentTimeMillis()-startTime)+"ms"); 
} 
}

Output:

Time taken by Concating with String: 1665ms
Time taken by Concating with StringBuffer: 2ms
Time taken by Concating with StringBuilder: 1ms

 

Read more topics related to java

 

Hope this was helpful for you. If you have any questions please feel free to leave a comment. Thank you for reading.

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *