Import StringBuffer In Java

Import StringBuffer In Java | The StringBuffer class is present in java.lang package. Since java.lang package is the default package available to every Java program therefore there is no need to import StringBuffer explicitly.

Let us see some examples of the StringBuilder class to demonstrate some methods. In Java, except String class, there are two more string manipulation classes: StringBuilder and StringBuffer. The StringBuffer class is synchronized whereas the StringBuilder class is non-synchronized. StringBuilder is used for single threading and StringBuffer is best suits for multi-threading.

Lets us know some facts about the StringBuffer class:-
1. The StringBuffer class is inherited from the Object class.
2. StringBuffer class inherits all methods from the object class. They are:- clone(), equals(), finalize(), getclass(), notifyAll(),notifies(), hashCode()
3. The StringBuffer implements the following interfaces:- Serializable, Comparable, CharSequence.
4. All methods of the StringBuffer class are safe for using multithreading operations because they are synchronized.

Constructors of StringBuffer class are as follows:-
1. StringBuffer()
2. StringBuffer(int size)
3. StringBuffer(String str)

How To Import StringBuffer In Java

Let us see some examples of StringBuffer class methods. Since the StringBuffer class is given in java.lang package therefore we don’t need to import them explicitly, we can call methods of the StringBuffer class directly.

public class Main {
   public static void main(String[] args) {
      StringBuffer sb = new StringBuffer("Java Programming");
      System.out.println("Length: " + sb.length());
      System.out.println("Capacity: " + sb.capacity());

      sb.append(" Language");
      System.out.println("StringBuffer: " + sb);
   }
}

Output:

Length: 16
Capacity: 32
StringBuffer: Java Programming Language

The StringBuffer class contains all the methods of the StringBuilder class. Both classes are very similar but the main difference is:- the StringBuffer class is synchronized whereas the StringBuilder class is non-synchronized. All the methods of both classes perform the same operation.

Let us see another example of the StringBuffer class append() method. The append() method is used to add primitives, strings, objects, and CharSequence to the StringBuffer class.

public class Main {
   public static void main(String[] args) {
      StringBuffer sb = new StringBuffer("Java");
      sb.append(" Programming");
      sb.append(" Language");
      System.out.println(sb);

      sb.append("-");
      sb.append(2030);
      System.out.println(sb);
   }
}

Output:-

Java Programming Language
Java Programming Language-2030

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

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