首页 > 其他分享 >String、StringBuffer 和 StringBuilder 的区别是什么?

String、StringBuffer 和 StringBuilder 的区别是什么?

时间:2022-08-26 16:47:12浏览次数:133  
标签:String StringBuffer strings StringBuilder class string

String 是只读字符串,它并不是基本数据类型,而是一个对象。从底层源码来看是一个 final 类型的字符串数组,一经定义,无法再增删改。每次对 String 的操作都会生成新的 String 对象。

The String class represents character strings. All string literals in Java programs, such as "abc", are implemented as instances of this class.

Strings are constant; their values cannot be changed after they are created. String buffers support mutable strings. Because String objects are immutable they can be shared. For example:

String str = "abc";

is equivalent to:

char data[] = {'a', 'b', 'c'};

String str = new String(data);

Here are some more examples of how strings can be used:

System.out.println("abc");

String cde = "cde";

System.out.println("abc" + cde);

String c = "abc".substring(2,3);

String d = cde.substring(1, 2);

The class String includes methods for examining individual characters of the sequence, for comparing strings, for searching strings, for extracting substrings, and for creating a copy of a string with all characters translated to uppercase or to lowercase. Case mapping is based on the Unicode Standard version specified by the Character class.

The Java language provides special support for the string concatenation operator ( + ), and for conversion of other objects to strings. String concatenation is implemented through the StringBuilder(or StringBuffer) class and its append method. String conversions are implemented through the method toString, defined by Object and inherited by all classes in Java. For additional information on string concatenation and conversion, see Gosling, Joy, and Steele, The Java Language Specification.

Unless otherwise noted, passing a null argument to a constructor or method in this class will cause a NullPointerException to be thrown.

A String represents a string in the UTF-16 format in which supplementary characters are represented by surrogate pairs (see the section Unicode Character Representations in the Character class for more information). Index values refer to char code units, so a supplementary character uses two positions in a String.

The String class provides methods for dealing with Unicode code points (i.e., characters), in addition to those for dealing with Unicode code units (i.e., char values).

public final class String
    implements java.io.Serializable, Comparable<String>, CharSequence {
    /** The value is used for character storage. */
    private final char value[];

}

每次 + 操作:隐式的在堆上 new 了一个跟原字符串相同的 StringBuilder 对象,再调用 append 方法拼接 + 后面

的字符。

abstract class AbstractStringBuilder implements Appendable, CharSequence {
    /**
     * The value is used for character storage.
     */
    char[] value;
}

StringBuffer 和 StringBuilder 它们两个都继承了 AbstractStringBuilder 抽象类,从 AbstractStringBuilder 抽象类中我们可以看到它们的底层都是可变的字符串数组,所以在频繁的字符串操作时,建议使用 StringBuffer 和 StringBuilder 来进行操作。另外 StringBuffer 对方法加了同步锁或对调用的方法加了同步锁,所以是线程安全的。StringBuilder 并没有对方法进行加同步锁,所以是非线程安全的。

标签:String,StringBuffer,strings,StringBuilder,class,string
From: https://www.cnblogs.com/jack-h-b-fan/p/16628039.html

相关文章

  • Java_String&ArrayList
    Java_String&ArrayListStringString类概述String概述java.lang.String类代表字符串,String类定义的变量可以用于指向字符串对象,然后操作该字符串。Java程序中的所......
  • Redis获取缓存异常:redis java.util.ArrayList cannot be cast to java.lang.String
    Redis获取缓存异常:redisjava.util.ArrayListcannotbecasttojava.lang.String在使用redis缓存数据时,增加一个配置类,修改key序列化器为string@Configurationpublic......
  • Remove Beginning and Ending Double Quotes from a String
    https://www.baeldung.com/java-remove-start-end-double-quote#:~:text=To%20remove%20double%20quotes%20just,be%20replaced%20by%20empty%20strings. 1.Overview......
  • [Oracle] LeetCode 415 Add Strings
    Giventwonon-negativeintegers,num1andnum2representedasstring,returnthesumofnum1andnum2asastring.Youmustsolvetheproblemwithoutusingany......
  • java的String.format中的百分号
    System.out.println(String.format("百分比%.2f%",(float)80/90));错误信息:Exceptioninthread"main"java.util.UnknownFormatConversionException:Conversion=......
  • Java 8 如何快速的将List<T>转换成List<Map<String,Object>>
    实际开发过程中,经常会遇到需要将List<T>转换List<Map<String,Object>>的情况,那么你们都是用什么方法实现的呢?下面是我开发过程中使用的方法,还望大佬看后轻喷。List<Map<......
  • StringBuilder类
    StringBuilder是字符串对象的缓冲区对象,缓冲区(出现目的,为了高效),提高String类的效率StringBuilder类的实现原理一个可变的字符序列,字符序列就是字符数组String......
  • Jedis操作string、Jedis操作hash
       Jedis操作stringJedls操作各种redis中的数据结构1)字符串类型stringsetget2)哈希类型hash:map格式hsethget3)列表类型list......
  • Object类和String类
    API的概念应用程序编程接口:每一个技术,官方都会定义出许多的功能,开发人员可以直接拿来使用(拿来主义).API可以理解为Sun公司已经开发好的类和方法.API文档就是我们......
  • String类型与基础类型的转换(String.valueof())
    1.由基本数据型态转换成String String类别中已经提供了将基本数据型态转换成String的static 方法 也就是String.valueOf()这个参数多载的方法 有下列几种 S......