String 构造函数
1 /**
2 * String 构造函数演示程序
3 *
4 * @author skywang
5 */
6 import java.nio.charset.Charset;
7 import java.io.UnsupportedEncodingException;
8
9 public class StringContructorTest {
10
11 public static void main(String[] args) {
12 testStringConstructors() ;
13 }
14
15 /**
16 * String 构造函数测试程序
17 */
18 private static void testStringConstructors() {
19 try {
20 System.out.println("-------------------------------- testStringConstructors -----------------------");
21
22 String str01 = new String();
23 String str02 = new String("String02");
24 String str03 = new String(new char[]{'s','t','r','0','3'});
25 String str04 = new String(new char[]{'s','t','r','0','4'}, 1, 3); // 1表示起始位置,3表示个数
26 String str05 = new String(new byte[]{0x61, 0x62, 0x63, 0x64, 0x65}); // 0x61在ASC表中,对应字符"a"; 1表示起始位置,3表示长度
27 String str06 = new String(new byte[]{0x61, 0x62, 0x63, 0x64, 0x65}, 1, 3); // 0x61在ASC表中,对应字符"a"; 1表示起始位置,3表示长度
28 String str07 = new String(new byte[]{0x61, 0x62, 0x63, 0x64, 0x65}, 0); // 0x61在ASC表中,对应字符"a";0,表示“高字节”
29 String str08 = new String(new byte[]{0x61, 0x62, 0x63, 0x64, 0x65}, 0, 1, 3); // 0x61在ASC表中,对应字符"a"; 0,表示“高字节”;1表示起始位置,3表示长度
30 String str09 = new String(new byte[]{(byte)0xe5, (byte)0xad, (byte)0x97, /* 字-对应的utf-8编码 */
31 (byte)0xe7, (byte)0xac, (byte)0xa6, /* 符-对应的utf-8编码 */
32 (byte)0xe7, (byte)0xbc, (byte)0x96, /* 编-对应的utf-8编码 */
33 (byte)0xe7, (byte)0xa0, (byte)0x81, /* 码-对应的utf-8编码 */ },
34 0, 12, "utf-8"); // 0表示起始位置,12表示长度。
35 String str10 = new String(new byte[]{(byte)0x5b, (byte)0x57, /* 字-对应的utf-16编码 */
36 (byte)0x7b, (byte)0x26, /* 符-对应的utf-16编码 */
37 (byte)0x7f, (byte)0x16, /* 编-对应的utf-16编码 */
38 (byte)0x78, (byte)0x01, /* 码-对应的utf-16编码 */ },
39 0, 8, "utf-16"); // 0表示起始位置,8表示长度。
40 String str11 = new String(new byte[]{(byte)0xd7, (byte)0xd6, /* 字-对应的gb2312编码 */
41 (byte)0xb7, (byte)0xfb, /* 符-对应的gb2312编码 */
42 (byte)0xb1, (byte)0xe0, /* 编-对应的gb2312编码 */
43 (byte)0xc2, (byte)0xeb, /* 码-对应的gb2312编码 */ },
44 Charset.forName("gb2312"));
45 String str12 = new String(new byte[]{(byte)0xd7, (byte)0xd6, /* 字-对应的gbk编码 */
46 (byte)0xb7, (byte)0xfb, /* 符-对应的gbk编码 */
47 (byte)0xb1, (byte)0xe0, /* 编-对应的gbk编码 */
48 (byte)0xc2, (byte)0xeb, /* 码-对应的gbk编码 */ },
49 0, 8, Charset.forName("gbk"));
50 String str13 = new String(new int[] {0x5b57, 0x7b26, 0x7f16, 0x7801}, 0, 4); // "字符编码"(\u5b57是‘字’的unicode编码)。0表示起始位置,4表示长度。
51 String str14 = new String(new StringBuffer("StringBuffer"));
52 String str15 = new String(new StringBuilder("StringBuilder"));
53
54 System.out.printf(" str01=%s \n str02=%s \n str03=%s \n str04=%s \n str05=%s \n str06=%s \n str07=%s \n str08=%s\n str09=%s\n str10=%s\n str11=%s\n str12=%s\n str13=%s\n str14=%s\n str15=%s\n",
55 str01, str02, str03, str04, str05, str06, str07, str08, str09, str10, str11, str12, str13, str14, str15);
56
57
58 System.out.println();
59 } catch (UnsupportedEncodingException e) {
60 e.printStackTrace();
61 }
62 }
63 }
1.理解byte类型,char类型
2.java中char类型使用的是union编码方式
3. new String(new int[]..)构造方法中传参是int类型,其编码是union方式,(union编码占用16位,所以不用8位的byte形式然后再指定编码类型)
new String(new byte[] 构造方法中传参是byte,具体编码方式需要指定