One, the String class
1. A string is a special object.
2. Once the string is initialized, it cannot be changed.
The String class represents a string and cannot be overridden because it has no subclasses.
Demo:
1 package com.hpioneer.Demo; 2 3 /** 4 * @Description: String类 5 * @Author: HPioneer 6 * @CreateTime: 2018/5/7 11:13 7 * @File: Java_String of JavaProject in com.hpioneer.Demo 8 * @FullFileName: com.hpioneer.Demo.Java_String 9 * @Create By IntelliJ 10 * @Version: 1.0 11 */ 12 public class Demo_String{ 13 public static void main(String[] args) { 14 15 //字符数组转字符串 16 char[] Array = { 'r', 'u', 'n', 'o', 'o', 'b' }; 17 String string = new String(Array); 18 System.out.println( string ); 19 System.out.println(string.length()); // Length 20 System.out.println("1, hello,".concat(string)); // Splicing 21 System.out.println(2+ " 、hello," + string); // Splice 22 System.out.printf("The value of the floating-point variable is "+"%f, the value of the integer variable is "+"%d, the value of the string variable is " +"is %s" , 1.0, 1, "1.0"); // Format 23 System.out.println(); 24 25 String q = "a"; 26 String w = "b" ; 27 String e = q + w; 28 String r = new StringBuffer().append(q).append(w).toString(); // The implementation of string addition is Buffer After append tostring 29 30 String a = "saff" ; 31 String b = "saff" ; 32 String c = new String("saff" ); 33 System.out.println(a.equals(b)); // true 34 System.out.println(a.equals(c)); // true 35 } 36 37}
Second, the StringBuffer class
Demo:
1 package com.hpioneer.Demo; 2 3 /** 4 * @Description: 5 * @Author: HPioneer 6 * @CreateTime: 2018/5/7 11:58 7 * @File: Demo_StringBuffer of JavaProject in com.hpioneer.Demo 8 * @FullFileName: com.hpioneer.Demo.Demo_StringBuffer 9 * @Create By IntelliJ 10 * @Version: 1.0 11 */ 12 public class Demo_StringBuffer { 13 public static void main(String[] args) { 14 15 /* StringBuffer s = new StringBuffer(); 16 System.out.println(s.length());//0 17 System.out.println(s.capacity());//16 初始容量 18 19 StringBuffer s1 = new StringBuffer(10); 20 System.out.println(s1.length());//0 21 System.out.println(s1.capacity());//10 22 23 StringBuffer s2 = new StringBuffer("asas"); 24 System.out.println(s2.length());//8 25 System.out.println(s2.capacity());//24 = 8+16 26 27 System.out.println(s2.append(true));//asastrue 28 s2.deleteCharAt(0); 29 s2.delete(0,3); 30 System.out.println(s2.append("dfdfdf"));//truedfdfdf 31 System.out.println(s2.append(100));//truedfdfdf100 32 System.out.println(s2.insert(4,"KK"));//trueKKdfdfdf100 33 System.out.println(s2.reverse());//001fdfdfdKKeurt 34 System.out.println(s2.replace(0,2,"111"));//1111fdfdfdKKeurt 35 System.out.println(s2.substring(2,5));//11f*/ 36 37 /*StringBuffer sb = new StringBuffer("heima"); 38 39 String s1 = new String(sb); //通过构造将StringBuffer转换为String 40 System.out.println(s1); 41 42 String s2 = sb.toString(); //Convert StringBuffer to String by toString method 43 System.out.println(s2); 44 45 String s3 = sb.substring(0, sb.length()); //Pass Convert StringBuffer to String by intercepting substring 46 System.out.println(s3); */ 47 48 String s = "heima" ; 49 System.out.println(s); 50 change(s); 51 System.out .println(s); 52 // System.out.println(s+="itca"); 53 System.out.println("-------------------- -" ); 54 StringBuffer sb =new StringBuffer(); 55 sb.append("heima"); 56 System.out.println(sb); 57 change(sb); 58 System.out.println(sb); 59 60 61 } 62 63 public static void change(StringBuffer sb) { 64 sb.append("itcast"); 65 } 66 67 public static void change(String s) { 68 s += "itcast"; 69 } 70 71 }