StringBuffer and StringBuilder functional characteristics and differences (Basic Edition)

table of Contents

Feature

Features

the difference


When the string changes, and requires the use of StringBuffer StringBuilder class (a container for storing data).

And String class is different, and the StringBuffer StringBuilder class objects can be modified many times, and does not generate new unused objects.

Since the StringBuffer StringBuilder compared to the speed advantage, so in most cases we recommend using the StringBuilder class.

 

Feature

  1. Variable length
  2. Different types of data may be stored
  3. Eventually turn into a string using
  4. String may be modified

 

Features

public class TestStringBuilder {
	public static void main(String[] args) {
		String str;
		StringBuilder sBuilder=new StringBuilder();//可变字符序列:相比String 无final修饰
		StringBuffer sbBuffer=new StringBuffer("abcdefg");
		//StringBuilder线程不安全,效率高(一般使用它),StringBuffer线程安全,效率低
		 
		System.out.println(Integer.toHexString(sbBuffer.hashCode()));
		System.out.println(sbBuffer);
		
		System.out.println("********");
		//更改字符串的内容
		sbBuffer.setCharAt(2, 'M');
		System.out.println(Integer.toHexString(sbBuffer.hashCode()));//hashcode一样,说明对象没变,
		System.out.println(sbBuffer);
	}

}
 
public class TestStringBuilder2 {
	public static void main(String[] args) {
		StringBuilder sbStringBuilder=new StringBuilder();
		
		for(int i=0;i<26;i++) {
			sbStringBuilder.append((char)('a'+i));//append扩展,可以链式调用
		}
		
		System.out.println(sbStringBuilder);
	
		//reverse 倒序
		sbStringBuilder.reverse();
		System.out.println(sbStringBuilder);
		
		//setCharAt 替换
		sbStringBuilder.setCharAt(3, '可');
		System.out.println(sbStringBuilder);
		
		//insert 插入
		sbStringBuilder.insert(4, '变').insert(5, '的');
		//insert最后会调用return this,返回自己,所以可以链式调用。
		System.out.println(sbStringBuilder);
		
		//delete 删除,和insert一样,可以链式调用
		sbStringBuilder.delete(3, 5).delete(17, 19);
		System.out.println(sbStringBuilder);
	}
}
//{
//	和String类含义类似的方法
//	public int indexOf(String str)
//	public int indexOf(String str,int fromindex)
//	public String substring (int start)
//	public String subString (int start,int end)
//	public int length()
//	char charAt(int index)
//}
/*
 * 测试String和StringBuilder的使用陷阱
 */
public class TestStringBuilder3 {
	public static void main(String[] args) {
		
		//使用String进行字符串的拼接 (!*不能出现这样的代码*!)
		String string="";
		for(int i=0;i<5000;i++) {
			string=string+i;  //相当于产生了10000个对象(i代表的数字为了和string相加,会变成一个字符串对象以和string联结)
		}
		
		//要用StringBuilder的append
		StringBuilder stringBuilder=new StringBuilder();
		for(int i=0;i<5000;i++) {
			stringBuilder.append(i);
		}
	}

}

the difference

StringBuilder class is presented in Java 5, the biggest difference between it and the StringBuilder StringBuffer that the method is not thread-safe (can not synchronize access).

StringBuffer basically call the parent's method and are added to the synchronized modification, it is thread-safe; but frequent locking and releasing locks result in reduced performance, the execution speed is slower than StringBuffer.
StringBuilder all methods are not modified with the synchronized keyword, so thread-safe, but because the process releases the lock guide is not locked, faster than StringBuffer on execution speed.

Published 38 original articles · won praise 6 · views 1925

Guess you like

Origin blog.csdn.net/weixin_43827227/article/details/97144694