目录标题
1. 借助JDK, 选取几个String、StringBuffer 、StringBuilder的常用API,并编写实例测试API的功能。
String类:
Interface CharSequence——charAt(int index):Returns the char value at the specified index.
package homework.second;
public class TestCharAt {
public static void main(String[] args) {
String str="0123456789";
char c=str.charAt(5);
System.out.println(c);
}
}
StringBuffer类:
Interface Appendable—— append(char c):Appends the specified character to this Appendable.
package homework.second;
public class TestAppend {
public static void main(String[] args) {
StringBuffer strb=new StringBuffer("01234");
strb.append("56789");
System.out.println(strb);
}
}
StringBuilder类:
Interface CharSequence—— length():Returns the length of this character sequence.
package homework.second;
public class TestLength {
public static void main(String[] args) {
StringBuilder Strbd=new StringBuilder("0123456789");
System.out.println(Strbd.length());
}
}
2. 请简述String,StringBuffer,StringBuilder三者之间的共同点与区别,应该分别在何种场景下使用?
●共同点:都是处理字符串的类,具有类似的处理方法;内部属性都有字符数组。
●不同点:
String:对象内储存的字符串为常量(见下图 private final char[] value),一经赋值就不能改变,进行字符串内容的改变操作时返回的结果是一个新的对象。
jdk中部分属性定义:
应用场景:用在一些字符串内容不改变的场景,确保程序的数据安全,如:作为参数传递(网络、数据库,安全性)、多线程程序。
StringBuffer:对象内储存的是字符串变量(见下图 char[] value),进行字符串内容的改变操作时返回的结果是同一个对象,比String高效(不需多次创建新的对象);线程安全。
jdk中部分属性定义:
应用场景:用于字符串内容经常改变的场景。
StringBuilder:和StringBuffer相似(都是继承的AbstractStringBuilder)对象内容可以改变,兼容StringBuffer的API,比StringBuffer更高效,但不线程安全。
jdk中部分属性定义:
应用场景:字符串经常改变的场景,但要注意线程安全。
3. 为什么不建议在for循环中使用“+”进行字符串拼接?
因为对于String类,每次用“+”进行字符串都会返回一个新对象,导致程序执行效率低,资源损耗大。推荐只用StringBuffer类和StringBuilder类的append()方法。
4. 什么是字符串的编码与解码?请举例说明。
在Java中将Unicode字符集转为本地字符集(如GB2312或GBK)的过程叫编码,反之叫解码。
例:
编码:
package homework.second;
import java.io.*;
public class CharCode {
public static void printByteArray(String msg,byte[] t) {
System.out.println(msg+"**************************");
for(int i=0;i<t.length;i++) {
System.out.println(Integer.toHexString(t[i]));
}
}
public static void printCharArray(String msg,char[] c) {
System.out.println(msg+"**************************");
for(int i=0;i<c.length;i++) {
System.out.println(Integer.toHexString(c[i]));
}
}
public static void main(String[] args) {
try {
String str="中文";
System.out.println(str);
printCharArray("unicode:",str.toCharArray());
byte[] b=str.getBytes("GB2312");
printByteArray("GB2312",b);
byte[] m=str.getBytes("ISO8859-1");
printByteArray("ISO8859-1",m);
}
catch(UnsupportedEncodingException e) {
System.out.println("没有相应的字符集!");
}
}
}
解码:
package homework.second;
import java.io.*;
public class Decode {
public static void printByteArray(String msg,byte[] t) {
System.out.println(msg+"**************************");
for(int i=0;i<t.length;i++) {
System.out.println(Integer.toHexString(t[i]));
}
}
public static void printCharArray(String msg,char[] c) {
System.out.println(msg+"**************************");
for(int i=0;i<c.length;i++) {
System.out.println(Integer.toHexString(c[i]));
}
}
public static void main(String[] args) {
byte[] b=new byte[6];
int t=0,pos=0;
String s;
try {
while(t!='\n') {
t=System.in.read();
b[pos]=(byte)t;
pos++;
}
printByteArray("本地码:",b);
s=new String(b,"GBK");
System.out.println(s);
printCharArray("unicode码:",s.toCharArray());
}
catch(Exception e) {
System.out.println(e.getMessage());
}
}
}