1 String

String 类代表字符串。Java 程序中的所有字符串字面值(如 "abc" )都作为此类的实例实现。

1.1 特点

1.1.1 是一个封装char[]数组的对象
package com.mtingcat.javabasis.api;/** * String 底层是以个char[]数组对象 * String所示意的字符串是一个常量是不可变的 * @author MTing * */public class apiString {    public static void main(String[] args) {        char data[] = {'a', 'b', 'c'};        String str = new String(data);        System.out.println(str);    }}
1.1.2 字符串是一个常量不可变

1.2 使用

1.2.1 创立一个对象
String str = "Hello";


① 如果是第一次应用字符串,java会在字符串常量池中创立一个对象
② 在次应用时java当初常量池中寻找,如果有间接拜访,如果没有那么放入常量池

package com.mtingcat.javabasis.api;/** * String 底层是以个char[]数组对象 * String所示意的字符串是一个常量是不可变的 * @author MTing * */public class apiString {    public static void main(String[] args) {                String str = "Hello";        String str1 = "Hello";        System.out.println(str.equals(str1));//true                char data[] = {'a', 'b', 'c'};        String str2 = new String(data);        System.out.println(str2);        }}
new String(char[])

底层保护了一个char[]

char data[] = {'a', 'b', 'c'};String str2 = new String(data);System.out.println(str2);
间接创立

到常量池中拿地址,再指向不会创立新的

String str = "Hello";String str1 = "Hello";System.out.println(str.equals(str1));//true
1.2.2 字符串的拼接

字符串是一个常量是不可变的这一个性质导致字符串的拼接操作每一次都会创立一个新对象,所以比拟耗时间。

String s1 = "字符串的拼接操作,";String s2 = "每一次都会创立一个对象,";String s3 = "因而效率较低";String s4 = "";long star = System.currentTimeMillis();s4 += s1;s4 += s2;s4 += s3;long end = System.currentTimeMillis();System.out.println(s4);System.out.println("拼接耗时:" + (end - star));

1.3 罕用办法案例

办法含意
length()返回此字符串的长度
charAt()返回指定索引处的 char
lastIndexOf()返回指定字符在此字符串中最初一次呈现处的索引
substring()返回一个新的字符串,它是此字符串的一个子字符串
equals()将此字符串与指定的对象比拟
startsWith()测试此字符串从指定索引开始的子字符串是否以指定前缀开始
endsWith()测试此字符串是否以指定的后缀完结
trim()去除字符串两端的空格
package com.mtingcat.javabasis.api;/** *| length() | 返回此字符串的长度 | *| charAt() | 返回指定索引处的 `char` 值 | *| lastIndexOf() | 返回指定字符在此字符串中最初一次呈现处的索引 | *| substring() | 返回一个新的字符串,它是此字符串的一个子字符串 | *| equals() | 将此字符串与指定的对象比拟 | *| startsWith() | 测试此字符串从指定索引开始的子字符串是否以指定前缀开始 | *| endsWith() | 测试此字符串是否以指定的后缀完结 | *| trim() | 去除字符串两端的空格 | * @author MTing * */public class apiString02 {    public static void main(String[] args) {        String I = " I am a student   ";        String She = "She is a student";        System.out.println(I);        System.out.println(She);        System.out.println("length():"+I.length());//18        System.out.println("charAt10:"+I.charAt(10));//u        System.out.println("lastIndexOf:"+I.lastIndexOf("a"));//6        System.out.println("substring:"+I.substring(5));// a student         System.out.println("equals:"+I.equals(She));//false        System.out.println("startwith:"+I.startsWith(I));//true        System.out.println("endWith:"+I.endsWith("student"));//false        System.out.println("trim:"+I.trim());//I am a student    }}

2 StringBuilder/StringBuffer

2.1 StringBuilder

① 是一个可变的字符串序列,是一个相似于String的字符串缓冲区,是一个容器
② StringBuilder底层封装了一个可变的char[]
③ 能够被屡次批改并且不会产生新的对象
④ 外面封装了一套对字符串操作的办法
⑤ 字符串数组默认的初始容量是16 initial capacity of 16 characters
⑥ 如果大于16会尝试将扩容,新数组大小原来的变成2倍+2,容量如果还不够,间接裁减到须要的容量大小。int newCapacity = value.length * 2 + 2;
⑦ StringBuffer 1.0出道线程平安,StringBuilder1.5出道线程不平安
⑧ 罕用append代替字符串来做字符串的连贯。

2.1.2 构造方法和罕用的办法

构造方法
//1.结构一个空的StringBuilder对象/** * Constructs a string builder with no characters in it and an * initial capacity of 16 characters. */ public StringBuilder() {    super(16); }//2.结构一个StringBuilder容器,并将字符串增加进去 /**  * Constructs a string builder initialized to the contents of the  * specified string. The initial capacity of the string builder is  * {@code 16} plus the length of the string argument.  *  * @param   str   the initial contents of the buffer.  */  public StringBuilder(String str) {      super(str.length() + 16);      append(str);  }//3.结构一个StringBuilder容器,并且自定义容器大小 /**  * Constructs a string builder with no characters in it and an  * initial capacity specified by the {@code capacity} argument.  *  * @param      capacity  the initial capacity.  * @throws     NegativeArraySizeException  if the {@code capacity}  *               argument is less than {@code 0}.  */ public StringBuilder(int capacity) {     super(capacity); }
罕用办法
//1.增加任意类型数据的字符串模式,并返回以后对象本身public StringBuilder append()//2.将以后StringBuilder对象转化为String对象public String toString()

2.1.2 字符串的连贯操作

package com.mtingcat.javabasis.api;public class objectStringBuilder01 {    public static void main(String[] args) {        StringBuilder str = new StringBuilder();        String str2 = "jkjkjkjk";        long start = System.currentTimeMillis();        for (int i = 0; i < 10; i++) {            str.append(str2);            }        long end = System.currentTimeMillis();        System.out.println(str.toString());        System.out.println(str.charAt(10));        System.out.println(end-start);    }        }

2.2 StringBuffer

① 线程平安的可变字符序列,相似于String字符串缓冲区
② 在任意的工夫点上都蕴含某些特定的字符序列

package com.mtingcat.javabasis.api;public class objectStringBuffer01 {    public static void main(String[] args) {        StringBuffer str = new StringBuffer();        String str2 = "jkjkjkjk";        long start = System.currentTimeMillis();        for (int i = 0; i < 10; i++) {            str.append(str2);                    }        long end = System.currentTimeMillis();        System.out.println(str.toString());        System.out.println(str.charAt(10));        System.out.println(end-start);            }        }