Let's learn java programming language with easy steps

Sunday, 13 August 2017

Difference Between String and StringBuffer

Difference Between String and StringBuffer with Example

Difference Between String and StringBuffer

Here we will discuss what is the difference between String and StringBuffer in java. This is the most important topic for java interview.

So let's start with java String.

Java String

  • If the data which enclosed within double quote(" ") is by default treated as a String class.
  • String class object is immutable i.e after creating a string object, we cannot modify or update this string object.
  • The performance of String is slow because it creates a new instance every time when we concat a string.
  • String class consumes more memory when you concat too many string because every time it creates a new instance.
  • By default, no additional character memory space is created when we created String class object.
  • String class overrides the equals() method of Object class.


Java String Example

This the simple example of String class. Java String class belongs to the java.lang package.

class StringExample
{
public static void main(String args[])
{
String s = "India is a nice country";
System.out.println(s);
}
}

Output: India is a nice country 

Another Example of String with concat() Method

As we know that String is immutable i.e it cannot be modified or changed after it created, but if we will concat string(add another string) then it will make a new instance but still original string will not change.

class ConcatExample
{
public static void main(String args[])
{
String s = "Micro";
s.concat("soft");//concat() combines specified string at the end 
System.out.println(s);
}
}

Output: Micro

In the above example, Here s variable will print Micro not Microsoft but here it will make another new instance "Microsoft" in constant pool memory.



Java StringBuffer

  • If the data which enclosed within double quote(" ") is not by default treated as StringBuffer class.
  • StringBuffer class object is mutable i.e it can be modified or update after it created.
  • The performance of StringBuffer class is fast in comparison of String because it can be modified or change in existing string buffer object.
  • It consumes less memory because it doesn't create a new instance when we append another string.
  • By default, it takes 16 characters memory space when we create StringBuffer object.
  • StringBuffer class does not override the equals() method of Object class.

There are some StringBuffer methods in java e.g append() method, insert() method, replace() method etc.

There are given below some StringBuffer examples.

Java StringBuffer class is thread safe, StringBuffer is thread safe means, multiple threads cannot access it simultaneously.

StringBuffer append() Example

The append() method of StringBuffer class is used to add the new string to the original string.

class Demo
{
public static void main(String args[])
{
StringBuffer sb = new StringBuffer("hello");
sb.append(" rahul");
System.out.println(sb);
}
}

Output: hello rahul


StringBuffer insert() Example

This is another StringBuffer example where we will use insert() method.

class Test
{
public static void main(String args[])
{
StringBuffer sb = new StringBuffer("hello");
sb.insert(1,"world");
System.out.println(sb);
}
}

Output: hworldello

So there are many differences between String and StringBuffer and StringBuilder class in java.

I hope you have learned well StringBuffer vs String in this post.
Share:

0 comments:

Post a Comment

Facebook Page Likes

Follow javatutorial95 on Google+

Follow javatutorial95 on twitter

Popular Posts

Get Updates By Email

Link

Translate