10.2 字符串String类
10.2.1 字符串String类的特点
字符串的学习,有的同学就看看API,记下方法,有的同学看看源代码,还有的同学画画图,自然学的深度是不一样的。
/** * The {@code String} class represents character strings. All * string literals in Java programs, such as {@code "abc"}, are * implemented as instances of this class. * <p> * 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: * <blockquote><pre> * String str = "abc"; * </pre></blockquote><p> * is equivalent to: * <blockquote><pre> * char data[] = {'a', 'b', 'c'}; * String str = new String(data); * </pre></blockquote><p> * Here are some more examples of how strings can be used: * <blockquote><pre> * System.out.println("abc"); * String cde = "cde"; * System.out.println("abc" + cde); * String c = "abc".substring(2,3); * String d = cde.substring(1, 2); * </pre></blockquote> * <p> * The class {@code 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 {@link java.lang.Character Character} class. * <p> * 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 {@code StringBuilder}(or {@code StringBuffer}) * class and its {@code append} method. * String conversions are implemented through the method * {@code toString}, defined by {@code Object} and * inherited by all classes in Java. For additional information on * string concatenation and conversion, see Gosling, Joy, and Steele, * <i>The Java Language Specification</i>. |
String 类代表字符串。Java 程序中的所有字符串字面值(如 "abc" )都作为此类的实例实现。 字符串是常量;它们的值在创建之后不能更改。字符串缓冲区支持可变的字符串。因为 String 对象是不可变的,所以可以共享。例如: String str = "abc"; 等效于: char data[] = {'a', 'b', 'c'}; String str = new String(data); 下面给出了一些如何使用字符串的更多示例: System.out.println("abc"); String cde = "cde"; System.out.println("abc" + cde); String c = "abc".substring(2,3); String d = cde.substring(1, 2); String 类包括的方法可用于检查序列的单个字符、比较字符串、搜索字符串、提取子字符串、创建字符串副本并将所有字符全部转换为大写或小写。大小写映射基于 Character 类指定的 Unicode 标准版。
Java 语言提供对字符串串联符号("+")以及将其他对象转换为字符串的特殊支持。字符串串联是通过 StringBuilder(或 StringBuffer)类及其 append 方法实现的。字符串转换是通过 toString 方法实现的,该方法由 Object 类定义,并可被 Java 中的所有类继承。有关字符串串联和转换的更多信息,请参阅 Gosling、Joy 和 Steele 合著的 The Java Language Specification。 |
1、String是个final类
2、String是不可变的字符序列
public final class String implements java.io.Serializable, Comparable<String>, CharSequence { /** The value is used for character storage. */ private final char value[];
/** Cache the hash code for the string */ private int hash; // Default to 0 |
String对象的字符内容是存储在一个字符数组中的。
private意味着外面无法直接获取字符数组,而且String没有提供value的get和set方法,
final意味着字符数组的引用不可改变,即通过让value指向新的数组对象来实现修改String对象,
而且String也没有提供方法来修改value数组某个元素值,因此字符串的字符数组内容也不可变。
疑问?那么字符串的拼接、字符串的截取、字符串的替换等操作是如何实现的呢?
每次修改都创建一个新的char数组表示修改结果。
3、String对象的创建
String str = “hello”;
String s1 = new String(); // 本质上 this.value = new char[0];
String s2 = new String(String original); //this.value = original.value;
String s3 = new String(char[] a); //this.value = Arrays.copyOf(value, value.length);
String s4 = new String(char[] a,int startIndex,int count)
.......
本教程由尚硅谷教育大数据研究院出品,如需转载请注明来源。