一、The differences between the String, StringBuilder, and StringBuffer classes
1.Strings are immutable
String s = "abcde";
s = s.concat("more stuff");
Now there are three String Objects
abcde、more stuff、 abcde more stuff
2.To make Java more memory efficient, the JVM sets aside a special area of memory called the "String constant pool."
When the compiler encounters a String literal, it checks the pool to see if an identical String already exists. If a match is found, the reference to the new literal is directed to the existing String, and no new String literal object is created.
3.Create new strings
String s="abc" //create one String Object(in pool)
String s= new String("abc"); //create two String
objects,one in "String pool",and another in normal memory
4.StringBuilder is not safe thread because its method is not synchronize , but run faster than StringBuffer
public synchronized StringBuffer append(String s)
二.Calendar主要用于日期的操纵,它是一个抽象类,所以
Calendar c = new Calendar(); //非法
Calendar c = Calendar.getInstance() //合法
//当日期为2001年10月,此时调用
c.roll(Calendar.MONTH,9);
//此时日期为2001年7月,它只会改变日期,但不进位(即不改变年份)
三.DateFormat的用法
import java.text.*;
import java.util.*;
class Dates3 {
public static void main(String[] args) {
Date d1 = new Date(1000000000000L);
DateFormat[] dfa = new DateFormat[6];
dfa[0] = DateFormat.getInstance();
dfa[1] = DateFormat.getDateInstance();
dfa[2] = DateFormat.getDateInstance(DateFormat.SHORT);
dfa[3] = DateFormat.getDateInstance(DateFormat.MEDIUM);
dfa[4] = DateFormat.getDateInstance(DateFormat.LONG);
dfa[5] = DateFormat.getDateInstance(DateFormat.FULL);
for(DateFormat df : dfa)
System.out.println(df.format(d1));
}
}
//which on our JVM produces
9/8/01 7:46 PM
Sep 8, 2001
9/8/01
Sep 8, 2001
September 8, 2001
Saturday, September 8, 2001
如果我们把日期转化为SHORT型,然后在parse为日期,此时会丢失精度
Date d1 = new Date(1000000000000L);
System.out.println("d1 = " + d1.toString());
DateFormat df = DateFormat.getDateInstance(
DateFormat.SHORT);
String s = df.format(d1);
System.out.println(s);
try {
Date d2 = df.parse(s);
System.out.println("parsed = " + d2.toString());
} catch (ParseException pe) {
System.out.println("parse exc"); }
//which on our JVM produces
d1 = Sat Sep 08 19:46:40 MDT 2001
9/8/01
parsed = Sat Sep 08 00:00:00 MDT 2001
四.正则表达式的使用
import java.util.regex.*;
class RegexSmall {
public static void main(String [] args) {
Pattern p = Pattern.compile("ab"); // the expression
Matcher m = p.matcher("abaaaba"); // the source
boolean b = false;
while(b = m.find()) {
System.out.print(m.start() + " "+m.group());
//调用group()方法获得匹配的结果
}
}
}
五、习题中出现的错误
1.Remember, that the equals() method for the integer wrappers
will only return true if the two primitive types and the two values are equal.
class TKO {
public static void main(String[] args) {
String s = "-";
Integer x = 343;
long L343 = 343L;
if(x.equals(L343)) s += ".e1 ";
if(x.equals(343)) s += ".e2 ";
Short s1 = (short)((new Short((short)343)) / (new Short((short)49)));
if(s1 == 7) s += "=s ";
if(s1 < new Integer(7+1)) s += "fly ";
System.out.println(s);
} }
2.The %b (boolean) conversion character returns true for any
non-null or non-boolean argument.
System.out.format("%b", 123);
the %f (floating-point) conversion character won't automatically promote an integer type
System.out.printf("%f", 123); //illegal
3.Only String objects can be operated on using the overloaded "+" operator.
StringBuffer sb = new StringBuffer("hello");
sb += "xyz"; //illegal
-------------------------------------------------------
New words in this chapter
1.zillion n. 庞大的数字、无法计算的数字
2.budge v. 移动
3.devilish adj. 如恶魔般的,精力旺盛的
4.concordant adj. 协调的
5.cohesion n 结合,凝聚,内聚
6.lenient adj. 宽大的,仁慈的
7.newbie n 新手
8.guru n 领袖,头头
9.chunk (of) n. 大块,矮胖的人或物
10.maven n. 内行,专家