String池
java uses the String and StringBuffer classes to encapsulate strings of characters.Java uses 16-bit Unicode characters in order to supper a broader range of international alphabets than would be possible with traditional 8-bit characters. Both strings and string buffers contain sequences of 16-bit Unicode characters.The next several sections examine these two classes, as well as Java's string concatenation feature.
字符串
用字符串和stringbuffer类封装的字符的字符串。使用16位字符为晚餐范围更广的国际字母比将有可能与传统的8位字符。两个字符串和字符串缓冲区包含序列的16位字符。接下来的几节审查这些类别,以及爪哇的字符串连接特征。
The String Class
The String class contains an immutable string.Once an instance is created,the string it contains cannot be changed.There are numerous forms of
constructor,allowing you to build an instance out of an array of bytes or chars,a subset of an array of bytes or chars, another string ,or a string buffer .Many of these constructors give you the option of specifying a character
encoding,specified as a string ;however,the Certification Exam does not require you to know the details of character encodings.
Probably the most common string constructor simply takes another string as its input .This is useful when you want to specify a literal value for the new string;
String s1=new String(“immutable”); An even easier abbreviation could be: String s1=\"immutable\";
It is important to be aware of what happens when you use a String literal (\"important\" in both examples). Every string literal is represented internally by an instance of String .java classes may have a pool of such strings.When a literal is compiled,the compiler adds an appropriate string to the
pool.However,if the same literal already appeared as a literal elsewhere in the class,then it is already represented in the pool.The compiler does not create a new copy;instead,it uses the existing one from the pool.This saves on memory and can do no harm.Since strings are immutable ,there is no way that a piece of code can harm another piece of code by modifying a shareed string.
Earlier in this chapter ,you saw how the equals() method can be used to provide a deep equality check of two objects. With strings,the equals()
menthod does what you would expect:it checks the two contained collections of characters.The code below shows how this is done: 1. String s1=”Compare me”; 2. String s2=”Compare me”; 3. if(s1.equals(s2)){ 4. //whatever 5. }
Not surprisingly,the test at line 3 succeeds. Given what you know about how String literals work,you can see that if line 3 is modified to use the==comparison , as shown below ,the test still succeeds:
1. String s1=”compare me”; 2. String s2=”compare me”; 3. if(s1==s2){ 4. //whatever 5. }
The == test is true because s2 refers to the String in the pool that was created in line 1. Figure 8.1 shows this graphically
.S1“Compare me” Pool of literal strings(文字字符串池) Figure.1 Identical literals (图8.1相同的文字) S2
You can also construct a String by explicitly calling the constructor ,as shown below; however, this causes extra memory allocation for no obvious advantage.
String s2 =new String(“Constructed”);
When this line is compiled ,the String literal \"Constructed\" is placed into the pool . At runtime, the new String() statement is executed and a rfesh instance of String is constructed,duplicating the String in the literal pool . Finally, a reference to the new String is assigned to S2 .Figure 8.2 shows the chain of events.
String s2= new String();
Pool of literal strings “Constructed“ “Constructed“ Figure 8.2 Explicitly calling the String constructor
字符串类
字符串包含一个不可变字符串。一旦创建实例的字符串,它包含不能被改变。有许多形式的构造,允许你建立一个例子一个字节数组或字符,一个子集,一个字节数组或字符,另一个字符串,或一个字符串缓冲区。许多这些构造提供的选项指定一个字符编码,指定为字符串;然而,认证考试不需要你知道详情的字符编码。
也许最常见的字符串构造函数只是另一个字符串作为它的输入。这是非常有用的当你想指定一个文本值的字符串; String s1=new String(“immutable”);
更简单的缩写可以是:
字符串s1=“不可改变的”; -----String s1=\"immutable\";
重要的是要知道发生了什么,当您使用一个字符串(“重要的”,在这两个例子)。每一个字符串字面内部表示String的实例。Java类可能有一个池等strings.When文字编译,编译器增加了一个适当的字符串的pool.However,如果相同的文字作为文字在其他地方已经出现类中,那么它已经是在pool.The编译器的代表不创建一个新的副本;相反,它使用现有的pool.This节省内存,并可以做没有harm.Since字符串是不可改变的,有没有办法,一段代码可能会损害另一块代码,通过修改shareed字符串。
在本章的前面,你看到了如何equals()方法可以使用,提供了两个对象的深平等检查。字符串,等于()menthod你所期望的:检查下面的代码包含了characters.The两个集合,显示如何做到这一点:
1.String s1=”Compare me”; 2.String s2=”Compare me”; 3.if(s1.equals(s2)){ 4. //whatever 5.}
毫不奇怪,在3号线的测试成功。既然你知道字符串常量如何工作,你可以看到,如果3号线进行了修改,使用==比较,如下所示,测试成功:
1.String s1=”compare me”; 2.String s2=”compare me”; 3.if(s1==s2){ 4.//whatever
5.}
==测试是真实的,因为S2是指串在第1行中创建的池。图8.1显示了这个图形化
您也可以通过显式调用构造函数,如下所示构造一个String;然而,这将导致额外的内存分配没有明显的优势。
String s2 =new String(“Constructed”);
此行是编译时,字面的“建造的”弦乐放置入池。在运行时,新的String()语句执行rfesh一个String实例构造,复制文字池中的字符串。最后,一个新的String引用被分配到S2。图8.2显示了一连串的事件。
Public class StringTest1{
Public static void main(String[] args){ String str1=”abc”; String str2=”abc”;
System.out.println(str1==str2); }
}
Public class StringTest2{
Public static void main(String[] args){ String str1=new String(“abc”); String str2=new String(“abc”); } }
因篇幅问题不能全部显示,请点此查看更多更全内容