字符串的学习,有的同学就看看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.
* 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 {@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.
* 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,
* The Java Language Specification.
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是不可变的字符序列
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)
4.字符串对象是如何存储的
字符串常量存储在字符串常量池,目的是共享
字符串非常量对象存储在堆中。
5.String的拼接
结论:
常量与常量的拼接结果在常量池
只要其中有一个是变量,结果就在堆中
如果拼接的结果调用intern()方法,就在常量池中
6.String对象的比较
==比较的是地址。
equals比较的是字符串的内容,重写了Object的equals方法。
publicbooleanequals(Object anObject) {
if(this== anObject) {
returntrue;
if(anObjectinstanceofString) {
String anotherString = (String)anObject;
intn = value.length;
if(n == anotherString·value·length) {
charv1[] = value;
charv2[] = anotherString.value;
inti = 0;
while(n– != 0) {
if(v1[i] != v2[i])
returnfalse;
i++;
returntrue;
returnfalse;
文章转载链接:http://www.atguigu.com/jsfx/12148.html
特别声明:以上内容(如有图片或视频亦包括在内)为自媒体平台“网易号”用户上传并发布,本平台仅提供信息存储服务。
Notice: The content above (including the pictures and videos if any) is uploaded and posted by a user of NetEase Hao, which is a social media platform and only provides information storage services.