English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
ofStringType is a Java class used to represent a set of characters. Strings in Java are immutable and their values cannot be changed after creation.
Since strings are immutable, if you try to reassign the value of a string, its reference will point to a new String object, leaving an unused String in memory.
Java provides the StringBuffer class as a replacement for String in places where a large number of modifications need to be made to character strings.
You can modify it repeatedly./operate the content of StringBuffer without leaving many new unused objects.
StringBuilder class is from Java 5The main difference between StringBuffer and StringBuilder introduced from Java
It is recommended to use StringBuilder as much as possible, as it is faster than StringBuffer. However, if thread safety must be guaranteed, the best choice is the StringBuffer object.
StringBuilderclassappend()The method accepts a String value and adds it to the currentObject.
To convert a String value to a StringBuilder object, simply use the append() method to attach it.
In the following Java program, we will convert a string array to a single StringBuilder object.
public class StringToStringBuilder { public static void main(String args[]) { String strs[] = {"Arshad", "Althamas", "Johar", "Javed", "Raju", "Krishna"}; StringBuilder sb = new StringBuilder(); sb.append(strs[0]); sb.append(" ")+strs[1]); sb.append(" ")+strs[2]); sb.append(" ")+strs[3]); sb.append(" ")+strs[4]); sb.append(" ")+strs[5]); System.out.println(sb.toString()); } }
Arshad Althamas Johar Javed Raju Krishna