Java开发技术之字符串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 (&nbsp;+&nbsp;), 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是不可变的字符序列

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方法。

public boolean equals(Object anObject) {

if (this == anObject) {

return true;

}

if (anObject instanceof String) {

String anotherString = (String)anObject;

int n = value.length;

if (n == anotherString.value.length) {

char v1[] = value;

char v2[] = anotherString.value;

int i = 0;

while (n– != 0) {

if (v1[i] != v2[i])

return false;

i++;

}

return true;

}

}

return false;

}

了解java培训开发技术知识,关注我,有更多精彩内容与您分享!

文章转载链接:http://www.atguigu.com/jsfx/12148.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值