Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Monday 10 April 2017

StringBuffer in Java

 StringBuffer

StringBuffer is a class in java.lang package. StringBuffer class is mutable i.e it is used to create mutable or modifiable string in java. StringBuffer class is same as String class in java except it is mutable i.e it can be changed.

StringBuffer class is thread safe class in java i.e multiple thread cannot access it simultaneously.


StringBuffer


StringBuffer Class Constructors

(1) public StringBuffer()

It creates an empty StringBuffer object with initial capacity of 16.

(2) public StringBuffer(String s)

It creates the StringBuffer object with the specified string.

(3) public StringBuffer(int capacity)

It creates an empty StringBuffer object with the specified capacity. Capacity value should be positive value. if we will give negative value, it will throw an exception.

(4) public StringBuffer(charSequence s)

It creates StringBuffer object characters available in passed charSequence object.


Methods of StringBuffer class in Java

  1. public synchronized StringBuffer append(String s)
  2. public synchronized StringBuffer insert(int offset, String s)
  3. public synchronized StringBuffer reverse()
  4. public int capacity()
  5. public int length()
  6. public String substring(int beginIndex)
  7. public String substring(int beginIndex, int endIndex)
  8. public char charAt(int index)
  9. public void ensureCapacity(int minimumCapacity)
  10. public synchronized StringBuffer replace(int startIndex, int endIndex, String s)
  11. public synchronized StringBuffer delete(int startIndex, int endIndex)

 Mutable String in Java

Mutable string means, A string that can be updated or changed or modified is called mutable string in java. StringBuffer class is used to create mutable string in java but String class is immutable in java.



1) StringBuffer append() method

This append() method is used to add the new string at the end of original string.

For example :

class Simple
{
public static void main(String args[])
{
StringBuffer sb = new StringBuffer("hello ");
sb.append("java");//original string will changed
System.out.println(sb);
}
}

output : hello java


2) StringBuffer insert() method

The insert() method of StringBuffer class is used to insert the new string with original string at specified position.

For example : 

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

output : hejavallo


3) StringBuffer replace() method

The replace() method of StringBuffer class is used to replace any old string with new string based on index value.

For example : 

class Simple2
{
public static void main(String args[])
{
StringBuffer sb = new StringBuffer("hello ");
sb.replace(1,3,"java");
System.out.println(sb);
}
}

output : hejavallo


4) StringBuffer delete() method

The delete() method of StringBuffer class deletes the string from the specified beginIndex to endIndex.

For example : 

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

output : hlo


5) StringBuffer reverse() method

The reverse() method of StringBuffer class is used to reverse the given string.

For example :

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

output : olleh


6) StringBuffer capacity() method

The StringBuffer capacity() method returns the current capacity of the buffer. The default capacity of the buffer is 16. If the total number of characters increases from it current capacity , it increases the capacity by ( oldcapacity * 2) +2. For example, if your current capacity is 16, it will be (16*2)+2 = 34.

For example : 

class CapacityMethodExample
{
public static void main(String args[])
{
StringBuffer sb = new StringBuffer();
System.out.println(sb.capacity());//default capacity 16
sb.append("hello");
System.out.println(sb.capacity());//still capacity is 16
sb.append("java is most popular language");
System.out.println(sb.capacity());//now (16*2)+2 = 34
}
}

output : 16
               16
               34


7) StringBuffer ensureCapacity() method



The ensureCapacity() method ensures that the given capacity is minimum to the current capacity. If it is greater than the current capacity the it increase the capacity by (oldcapacity*2)+2. for example current capacity is 16.

For example : 

class EnsureCapacityMethodExample
{
public static void main(String args[])
{
StringBuffer sb = new StringBuffer();
System.out.println(sb.capacity());//default capacity 16
sb.append("hello");
System.out.println(sb.capacity());//still capacity is 16
sb.append("java is most popular language");
System.out.println(sb.capacity());//now (16*2)+2 = 34
sb.ensureCapacity(10);//now no change
System.out.println(sb.capacity());//now 34
sb.ensureCapacity(50);//(34*2)+2

System.out.println(sb.capacity());//70
}

}

output : 16
              16
              34
              34
              70

Visit : Java String Interview Questions and String Programming Interview Questions.

Share:

0 comments:

Post a Comment

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate