Conversion from String to StringBuffer and StringBuilder

Here in this context We can directly pass String class object to StringBuffer and StringBuilder class constructors. As String class is immutable in java, so for editing a string, we can perform same by converting it to StringBuffer or StringBuilder class objects.

Example.java

public class Example {
    public static void main(String[] args) {
        String str = "SILAN";
        //Conversion from String object to StringBuffer
        StringBuffer sb1 = new StringBuffer(str);
        System.out.println(sb1);
        sb1.reverse();
        System.out.println(sb1);
        //Conversion from String object to StringBuilder
        StringBuilder sb2 = new StringBuilder(str);
        sb2.append("Technology");
        System.out.println(sb2);
    }
}


Output:

SILAN

NALIS

SILANTechnology

Our Services:

Java Training | Python Training | Machine Learning with Python Training | Deep Learning with Python Training | Data Science with Python Training | Projects

SILAN Technology
Call to: 9439202111  |  9658317757  |  0674-2361252

Comment