The difference between StringBuilder's construction method, member method (partial) and String

The difference between String and StringBuilder:

**StringBuilder: is a variable string. (Also called string buffer class)
The content of String is fixed and
the content of StringBuilder is variable

Why use StringBuilder:

Every time you use String to splice, a new string object will be generated, which is time-consuming and wastes the address pool space in the method area.
However, the StringBuilder is used to splice strings from the beginning to the end using the same StringBuilder container.
If possible, it is recommended to use this class first. , Because it is faster than StringBuffer in most implementations.

Construction methods and member methods:

Construction method:
StringBuilder sb=new StringBuilder();
Member method:
public int capacity(): returns the current capacity ---- theoretical value (16 characters, the number of characters will gradually increase with the splicing)
public int length(): return Length (number of characters) ---- actual value**

package MyString;
/*
    StringBuilder:是一个可变的字符串。(也叫字符串缓冲区类)
    String和StringBuilder的区别:
	        String的内容是固定的
	        StringBuilder的内容是可变的

	为什么要使用StringBuilder:
	         每次用String拼接都会产生新的字符串对象,耗时又浪费方法区内的地址池空间,
	         而利用StringBuilder来拼接字符串自始至终用的都是同一个StringBuilder容器,
	         如果可能,建议优先采用该类,因为在大多数实现中,它比 StringBuffer 要快。

	 构造方法:
	        StringBuilder sb=new StringBuilder();
	 成员方法:
	        public int capacity():返回当前容量----理论值(16个字符,会随着拼接逐渐增加字符数)
	        public int length():返回长度(字符数)----实际值
 */
public class Stringbuilder1 {
    
    
    public static void main(String[] args) {
    
    
        StringBuilder sa=new StringBuilder("hello777");
        System.out.println("sa:"+sa);
        System.out.println("sa.capacity():"+sa.capacity());
        System.out.println("sa.length():"+sa.length());
    }

}

Guess you like

Origin blog.csdn.net/m0_52646273/article/details/114867521