Java String To StringBuffer

Java String To StringBuffer | In this blog, we will convert the string to a StringBuffer. The StringBuffer is a class that is a synchronized and thread-safe model and method local operations because locking and unlocking objects is not necessary for StringBuffer.

We use StringBuffer when we want to modify and store the object in a multi-threading environment, whereas we use the string when we don’t want to modify the string data or when we want to store an already modified string in a new object. Now let us see how to convert this string to a string buffer.

There are a few methods like the reverse() method which is available in the StringBuffer object but not in the String class. In that case, we can convert string to StringBuffer object and call reverse() method, later convert StringBuffer to String object.

Java String To StringBuffer using Constructor

The StringBuffer class has fothe llowing constructor to create a StringBuffer object from the given String object:- public StringBuffer(String str)

It constructs a string buffer initialized to the contents of the specified string. The initial capacity of the string buffer is 16 plus the length of the string argument.

public class Main {
    public static void main(String[] args) {
        String string = "We are learning Java programming language.";
        System.out.println("String: " + string);
        System.out.println(string.getClass().getName());

        // converting string to StringBuffer
        StringBuffer sb = new StringBuffer(string);
        System.out.println("StringBuffer: " + sb);
        System.out.println(sb.getClass().getName());
    }
}

Output:-

String: We are learning Java programming language.
java.lang.String
StringBuffer: We are learning Java programming language.
java.lang.StringBuffer

String To StringBuffer In Java using append() Method

Let us use the append() method to convert the string to the StringBuffer.

public class Main {
    public static void main(String[] args) {

        String string = "Are you learning Java programming language? ";
        StringBuffer sb = new StringBuffer();
        sb.append(string);
        System.out.println("StringBuffer: " + sb);

        String string1 = "If you are new to programming, ";
        sb.append(string1);
        System.out.println("StringBuffer: " + sb);

        String string2 = "you can learn it from www.knowprogram.com";
        sb.append("\n" + string2);
        System.out.println("StringBuffer: " + sb);
    }
}

Output:-

StringBuffer: Are you learning Java programming language?
StringBuffer: Are you learning Java programming language? If you are new to programming,
StringBuffer: Are you learning Java programming language? If you are new to programming,
you can learn it from www.knowprogram.com

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.