1 String s1 = "abc";//字面量的定义方式
2 String s2 = "abc";
3 s1 = "hello";
4
5 System.out.println(s1 == s2);//比较s1和s2的地址值
6
7 System.out.println(s1);//hello
8 System.out.println(s2);//abc
9
10 System.out.println("*****************");
11
12 String s3 = "abc";
13 s3 += "def";
14 System.out.println(s3);//abcdef
15 System.out.println(s2);
16
17 System.out.println("*****************");
18
19 String s4 = "abc";
20 String s5 = s4.replace(‘a‘, ‘m‘);
21 System.out.println(s4);//abc
22 System.out.println(s5);//mbc
1 String s1 = "javaEE";
2 String s2 = "hadoop";
3
4 String s3 = "javaEEhadoop";
5 String s4 = "javaEE" + "hadoop";
6 String s5 = s1 + "hadoop";
7 String s6 = "javaEE" + s2;
8 String s7 = s1 + s2;
9
10 System.out.println(s3 == s4);//true
11 System.out.println(s3 == s5);//false
12 System.out.println(s3 == s6);//false
13 System.out.println(s3 == s7);//false
14 System.out.println(s5 == s6);//false
15 System.out.println(s5 == s7);//false
16 System.out.println(s6 == s7);//false
17
18 String s8 = s6.intern();//返回值得到的s8使用的常量值中已经存在的“javaEEhadoop”
19 System.out.println(s3 == s8);//true
20 ****************************
21 String s1 = "javaEEhadoop";
22 String s2 = "javaEE";
23 String s3 = s2 + "hadoop";
24 System.out.println(s1 == s3);//false
25
26 final String s4 = "javaEE";//s4:常量
27 String s5 = s4 + "hadoop";
28 System.out.println(s1 == s5);//true
1 @Test
2 public void test1(){
3 String str1 = "123";
4 // int num = (int)str1;//错误的
5 int num = Integer.parseInt(str1);
6
7 String str2 = String.valueOf(num);//"123"
8 String str3 = num + "";
9
10 System.out.println(str1 == str3);
11 }
1 @Test
2 public void test2(){
3 String str1 = "abc123"; //题目: a21cb3
4
5 char[] charArray = str1.toCharArray();
6 for (int i = 0; i < charArray.length; i++) {
7 System.out.println(charArray[i]);
8 }
9
10 char[] arr = new char[]{‘h‘,‘e‘,‘l‘,‘l‘,‘o‘};
11 String str2 = new String(arr);
12 System.out.println(str2);
13 }
1 @Test
2 public void test3() throws UnsupportedEncodingException {
3 String str1 = "abc123中国";
4 byte[] bytes = str1.getBytes();//使用默认的字符集,进行编码。
5 System.out.println(Arrays.toString(bytes));
6
7 byte[] gbks = str1.getBytes("gbk");//使用gbk字符集进行编码。
8 System.out.println(Arrays.toString(gbks));
9
10 System.out.println("******************");
11
12 String str2 = new String(bytes);//使用默认的字符集,进行解码。
13 System.out.println(str2);
14
15 String str3 = new String(gbks);
16 System.out.println(str3);//出现乱码。原因:编码集和解码集不一致!
17
18
19 String str4 = new String(gbks, "gbk");
20 System.out.println(str4);//没出现乱码。原因:编码集和解码集一致!
21
22
23 }
原文:https://www.cnblogs.com/shici/p/14869284.html