有两个final类用于处理字符:String类和StringBuffer类。
1、String类实现不可变字符型字符串,不可变字符串在创建后和初始化之后能被读取.
2、StringBuffer类实现动态的字符型字符串。
3、使用这两个类实现的字符型字符串是真正的对象,这种字符串的每个字符都是用16位的字符表示的。
创建并初始化字符串
1、创建String对象的最简单的办法就是使用字符串常量:
String str1="You cannot change me!";
/*
可以从某种意义上说:
A a=new A();A b=new A();a=b; */
/*str1
和
”You cannot change me!”
都是对象。
*/
字符串常量是String对象的引用。String对象中的值就是双引号中字符串常量的字符序列。由于字符串常量是引用,因此可以像操作其他String引用一样操作它。
2、字符串常量可以用来在其String对象上调用方法:
int len="You cannot change me!".length();
3、
interned
字符串
<1>如果常量表达式的值位字符串,而且常量表达式的字符串序列都相同,那么这些共享同一个String对象。字符串为interned时,String类将护同一个私有池。
String str2="You cannot change me!";
<2>字符串引用str1和str2都代表同一个String对象,而且被初始化为字符型字符串:"You cannot change me!"。
<3>编译时在推导这个表达式时,将产生一个interned字符串。
<4>在下面的代码里,can1引用和can2引用表示同一个含有字符串“7UP”的String对象。
String can1=7+"UP";
String can2="7UP";
<5>但是,在下面的代码里,引用can4表示一个新的String对象,在编译时,其值为“7UP”。
String word="UP";
String can4=7+word;
值为字符串的常量表达式共享同一个String对象不会产生问题,因为String对象是不可变的。表达式(7+其他字符)不是常量比表达式,因此产生了一个新的String对象。
String构造函数
String类有许多构造函数,可以使用不同类型的参数来创建和初始化String对象。下面是其中的两个构造函数:
1、String (String s)
此构造函数创建一个新的String对象,其内容与参数String对象的内容是一样的。
2、String( )
<1>此构造函数创建一个新String对象,其内容为空字符串” ”。
<2>
请注意:使用构造函数将创建一个全新的
String
对象,而不是共享字符串。
<3>调用String类中intern( )方法可以获得inerned字符串的引用——但实际不需要这样做。
3、 其他构造函数。还可以使用字节数组、字符数组和字符串缓冲来创建String对象:
byte[ ] bytes={97,98,98,97};
char[ ] characters={‘a’,’b’,’b’,’a’};
StringBuffer strBuf=new StringBuffer(“abba”);
//……
String byteStr=new String(bytes); //Using array of bytes:”abba”
String charStr=new String(character); //Using array of chars:”abba”
String buffStr=new String(strBuf); //Using string of buffer:”abba”
实例
1、在下面的例子中,引用str1表示的String对象与str4和str5表示的都不一样。
2、使用new运算符和String构造函数总是创建新String对象。
3、表达式("You cannot”+其他字符)不是常量表达式,因此产生了一个新Stiring对象。
4、main( )方法中的本地引用str2和str3以及Auxiliary类中的静态引用str1都表示同一个interned字符串。
代码:
package
stringdemo;
public
class
StringConstruction {
/**
*
@param
args
*/
static
String
str1
=
"You cannot change me!"
;
//Interned
public
static
void
main(String[] args) {
//
TODO
Auto-generated method stub
String emptyStr=
new
String();
//""
System.
out
.println(
"emptyStr"
+emptyStr);
String str2=
"You cannot change me!"
;
//Interned
String str3=
"You cannot"
+
"change me!"
;
//Interned
String str4=
new
String(
"You cannot change me!"
);
// New String object
String words=
"change me"
;
String str5=
"You cannot"
+words;
// New String object
System.
out
.println(
"str1==str2:"
+(
str1
==str2));
//(1)true
System.
out
.println(
"str1.equals(str2)"
+
str1
.equals(str2));
//(2)true
System.
out
.println(
"str1==str3:"
+(
str1
==str3));
//(3)true
System.
out
.println(
"str1.equals(str3)"
+
str1
.equals(str3));
//(4)true
System.
out
.println(
"str1==str4:"
+(
str1
==str4));
//(5)false
System.
out
.println(
"str1.equals(str4)"
+
str1
.equals(str4));
//(6)true
System.
out
.println(
"str1==str5:"
+(
str1
==str5));
//(7)false
System.
out
.println(
"str1.equals(str5)"
+
str1
.equals(str5));
//(8)true
System.
out
.println(
"str1==Auxiliary.str1:"
+(
str1
==Auxiliary.
str1
));
//(9)true
System.
out
.println(
"str1.equals(Auxiliary.str1)"
+
str1
.equals(Auxiliary.
str1
));
//(10)true
System.
out
.println(
"/"You cannot change me!/".length():"
+
"You cannot change me!"
.length());
}
}
class
Auxiliary{
static
String
str1
=
"You cannot change me!"
;
}