StringBuilder和StringBuffer非常类似, 均代表可变的字符串序列. 这两个类都是抽线类AbstractStringBuilder的子类, 方法几乎一样

/******String修改引用, StringBuilder/StringBuffer修改源字符串*******/
public static void main(String[] args) {
String s1 = new String("abcdef");
System.out.println(Integer.toHexString(s1.hashCode()));
System.out.println(s1); s1 = "hdfa"; // 新建一个字符串, 而不是改变原来字符串的内容
System.out.println(Integer.toHexString(s1.hashCode()));
System.out.println(s1); System.out.println("###############################"); StringBuilder sb = new StringBuilder("hahahaha"); System.out.println(Integer.toHexString(sb.hashCode()));
System.out.println(sb); sb.setCharAt(2, 'Z'); // 改变原来字符串的内容
System.out.println(Integer.toHexString(sb.hashCode()));
System.out.println(sb);
}
/*
ab199863
abcdef
30cab7
hdfa
###############################
3b192d32
hahahaha
3b192d32
haZahaha
*/

StringBuilder

abstract class AbstractStringBuilder implements Appendable, CharSequence {
/**
* The value is used for character storage.
*/
byte[] value
// 以下代码shenglue
}

显然, 内部也是一个字符数组, 但这个数组没有像String类中用final修饰

StringBuilder与StringBuffer区别:

1.StringBuffer是JDK1.0版本提供的类, 线程安全, 做线程同步检查, 效率较低

2.StringBuilder是JDK1.5版本提供的类, 线程不安全, 不做线程同步检查, 因此效率较高, 建议使用此种方法

常用方法

注意链式调用

重载public StringBuilder append(...)方法可以为该StringBuilder对象添加字符串序列, 仍然返回对象自身

public StringBuilder delet(int start, int end)可以删除从start开始到end-1为止的一段字符序列, 仍然返回自身对象

public StringBuilder deleteCharAt(int dex)移除此序列指定位置上的字符, 仍然返回自身对象

重载public StringBuilder insert(...)方法可以为该StringBuilder对象在指定位置插入字符序列, 仍然返回自身对象

public StringBuilder reverse()用于将字符序列逆序, 仍然返回自身对象

public StringBuilder toString()返回此序列中数据的字符串表示形式, StringBuilder对象转换成String对象

和Sting类含义类似的方法:

  public int indexOf(String str)

  public int indexOf(String str, int fromIndex)

  public String substring(int start)

  public String substring(int start, int end)

  public int length()

  char charAt(int index)

public static void main(String[] args) {
StringBuilder sb = new StringBuilder(); for(int i=0; i<26; i++) {
// 数字转换字母方法
char tmp = (char)('a' + i);
sb.append(tmp);
} System.out.println(sb);
System.out.println("##########################"); // StringBuilder对象转换String对象
String str = sb.toString();
System.out.println(str);
System.out.println("##########################"); sb.reverse();
System.out.println(sb);
System.out.println("##########################"); sb.setCharAt(0, '哈');
System.out.println(sb);
System.out.println("##########################");
// 链式调用. 核心就是该方法调用return this, 把自己返回了
sb.insert(1, 'Z').insert(2, 'Y').insert(3, 'B').insert(4, "哈哈");
System.out.println(sb);
System.out.println("##########################"); System.out.println(sb.length());
sb.delete(5, 31).deleteCharAt(0).deleteCharAt(3);
System.out.println(sb); } /*
abcdefghijklmnopqrstuvwxyz
##########################
abcdefghijklmnopqrstuvwxyz
##########################
zyxwvutsrqponmlkjihgfedcba
##########################
哈yxwvutsrqponmlkjihgfedcba
##########################
哈ZYB哈哈yxwvutsrqponmlkjihgfedcba
##########################
31
ZYB
*/

使用陷阱

StringBuilder和StringBuffer类是对原始字符串本身操作, 可以对字符串进行修改而不产生副本拷贝或产生少量的副本, 因此可以在循环中使用

/*************String和StringBuilder频繁修改字符串时效率测试***************/
public static void main(String[] args) {
/****************使用String进行字符串的拼接*******************************/
String str1 = ""; long num1 = Runtime.getRuntime().freeMemory(); // 获取系统剩余内存空间
long time1 = System.currentTimeMillis(); // 获取系统当前时间 for(int i=0; i<5000; i++) {
str1 = str1 + i;
} long num2 = Runtime.getRuntime().freeMemory();
long time2 = System.currentTimeMillis(); System.out.println("String占用内存: " + (num1 - num2));
System.out.println("String占用时间: " + (time2 - time1)); /**************使用StringBuilder进行字符串的拼接*************************/
StringBuilder sb1 = new StringBuilder(""); long num3 = Runtime.getRuntime().freeMemory();
long time3 = System.currentTimeMillis(); for(int i=0; i<5000; i++) {
sb1.append(i);
} long num4 = Runtime.getRuntime().freeMemory();
long time4 = System.currentTimeMillis(); System.out.println("StringBuilder占用内存: " + (num3 - num4));
System.out.println("StringBuilder占用时间: " + (time4 - time3));
} /*
String占用内存: 77520136
String占用时间: 61
StringBuilder占用内存: 524288
StringBuilder占用时间: 1
*/

最新文章

  1. Aptana+spket搭建EXTJS开发环境
  2. PhpStorm 10 破解方法
  3. C语言新文法
  4. 实现ASP.NET无刷新下载并提示下载完成
  5. WinMain与wWinMain,win32的字符集问题
  6. JQuery.Ajax()的data参数类型
  7. 【USACO】beads
  8. C# Redis Server分布式缓存编程(二)
  9. LC-检索
  10. Linux设备驱动程序:中断处理之顶半部和底半部
  11. go语言细节
  12. cocos2dx-lua绑定自定义c++类(一)
  13. 【LeetCode练习题】Partition List
  14. hdu 1535 Invitation Cards(SPFA)
  15. zookeeer client 通信协议
  16. 详解ASP.NET MVC 控制器
  17. java poi 导入日期为空
  18. 《高级软件测试》Windows平台Jira的配置
  19. 第四次C语言作业
  20. 安卓开发笔记(十):升级ListView为RecylerView的使用

热门文章

  1. helm部署mysql
  2. Eclipse 重命名工程、包、类
  3. springCloud项目搭建
  4. [源码阅读] 阿里SOFA服务注册中心MetaServer(2)
  5. 2-Java面试-面向对象
  6. 2014年 实验一  C2C个人拍卖
  7. 【C语言/C++编程学习笔记】:通俗易懂讲解 - 链表!学不会?不存在的!
  8. go mod模式,引入自己的包,goland飘红
  9. centos8使用systemctl管理运行级别
  10. 如何使用FastCGI处理自定义HTTP头