stringbuffer java API_JAVA_API -- 2 String+StringBuilder+StringBuffer

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 字符串是一个常量不可变

5f8d547ac838fafec22c8affab7c8cd9.png

1.2 运用

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

7373bab74e4406d5abec79ff58918d45.png

① 如果是第一次使用字符串,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);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值