1.
Review question 6.14: If all the data fields in a class are private primitive, and the class contains no set methods, is the class immutable?

Chapter 7 Strings

2.
String 类共有13种构造方法,另外还可以简单地通过赋予初值来创建一个String对象。
String message = "Welcome to Java";
该方法称为 shorthand initializer

3.
String 类是不可变动的,听起来似乎并非如此。事实上,当试图修改String的内容时,如
String s = "Java";
s = "HTML"
此时并没有修改原来 s 的内容,而是新建了一个内容为"HTML"的字符串,然后s指向了那个新字符串。

4.
JVM 为了提高效率同时节省内存空间,会自动让两个(或以上)内容相同的字符串reference指向同一个字符串,而该字符串可以通过任意一个字符串的intern方法得到,如
String s = "Welcome to Java";
String s1 = new String("Welcome to Java");
String s2 = s1.intern();
String s3 = "Welcome to Java";

System.out.println("s1 == s is ", (s1 == s));
System.out.println("s2 == s is ", (s2 == s));
System.out.println("s == s3 is ", (s == s3));

则显示
s1 == s is false
s2 == s is true
s == s3 if true

由此可见,通过shorthand initializer创建的几个内容相同的字符串reference最终指向同一个字符串。

posts - 403, comments - 310, trackbacks - 0, articles - 7
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

1.26 Java notes

Posted on 2007-04-22 20:23 ZelluX 阅读(215) 评论(0)  编辑  收藏 所属分类: OOP
2007-01-26 22:29:04
只有注册用户登录后才能发表评论。


网站导航: