1. 理解Java API 的概念;
2. String类中的内容一旦被初始化就不能再改变;
3. StringBuffer类用于封装内容可以改变的字符串;用它的ToString方法转换成Stirng;
4. 比较一下两种情况的差异:
String s1 = "hello";
String s2 = "hello";
System.out.println(s1 == s2);
String s3 = new String("hello");
String s4 = new String("hello");
System.out.println(s3 == s4);
以上的代码的执行结果是:true false
5. 练习的小程序
/**
* 从命令行中读取字符串,并且显示出来,直到遇到bye字符串
* @author lcq
*
*/
publicclass ReadLine {
publicstaticvoid main(String[] args) {
byte[] buf = newbyte[1024];
String strInfo;
int pos = 0;
int ch = 0;
System.out.println(" enter info :");
while (true) {
try {
ch = System.in.read();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
switch (ch) {
case'\r':
break;
case'\n':
strInfo = new String(buf, 0, pos);
if (strInfo.equals("bye")) {
return;
} else {
System.out.println(strInfo);
pos = 0;
break;
}
default:
buf[pos++] = (byte) ch;
}
}
}
}
6. String类中的常用的方法:indexOf()返回指定字符的索引;subString(int beginIndex)返回子字符串;
7. 基本数据类型的对象包装类
8. 根据用户的输入数据打印星图,同时比较下图中代码的效率,在第一段代码中每一次外层的循环都要创建一个String对象,并且要转换成StirngBuffer对象进行字符串的拼接然后调用ToString方法再转换成String对象进行打印;而直接使用StringBuffer对象则大大提高了执行效率;
public class TestInteger {
public static void main(String[] args) {
int w = new Integer(args[0]).intValue();// 得到星图的宽度
int h = Integer.valueOf(args[1]).intValue();//得到星图的高度
for (int i = 0; i < h; i++) {
StringBuffer sbf = new StringBuffer();
for (int j = 0; j < w; j++) {
sbf.append('*');
}
System.out.println(sbf.toString());
}
}
}
代码效率的提高: