`
Lstoryc
  • 浏览: 75792 次
文章分类
社区版块
存档分类
最新评论

Java 中 StringBuilder 说明

 
阅读更多
java.lang.StringBuilder

 

A mutable sequence of characters. This class provides an API compatible with StringBuffer, but with no guarantee of synchronization. This class is designed for use as a drop-in replacement for StringBuffer in places where the string buffer was being used by a single thread (as is generally the case). Where possible, it is recommended that this class be used in preference to StringBuffer as it will be faster under most implementations.

The principal operations on a StringBuilder are the append and insert methods, which are overloaded so as to accept data of any type. Each effectively converts a given datum to a string and then appends or inserts the characters of that string to the string builder. The append method always adds these characters at the end of the builder; the insert method adds the characters at a specified point.

For example, if z refers to a string builder object whose current contents are "start", then the method call z.append("le") would cause the string builder to contain "startle", whereas z.insert(4, "le") would alter the string builder to contain "starlet".

In general, if sb refers to an instance of a StringBuilder, then sb.append(x) has the same effect as sb.insert(sb.length(), x). Every string builder has a capacity. As long as the length of the character sequence contained in the string builder does not exceed the capacity, it is not necessary to allocate a new internal buffer. If the internal buffer overflows, it is automatically made larger.

Instances of StringBuilder are not safe for use by multiple threads. If such synchronization is required then it is recommended that java.lang.StringBuffer be used.

Since:
1.5
Author:
Michael McCloskey
See Also:
java.lang.StringBuffer
java.lang.String
如果用.append 来更新String对象,效率比用 +("xxx") 高很多。。。。
分享到:
评论

相关推荐

    java中关于栈的使用

    当遍历完成时栈为空说明字符串是合法的. public boolean isMatch(String str) { MyStack<Character> myStack = new MyArrayStack(); char[] arr = str.toCharArray(); for (char c : arr) { Character temp = ...

    Java实现自己的Json解析器

    碰到 \",说明是字符串,在下一个\"出现之前,把扫描出来的字符都当成字符串中的字符,放到一个StringBuilder中去。 碰到 [ 符号,说明是数组了,就需要new一个JsonArray,在下一个 ] 符号出现之前,调用nextObject...

    Java String、StringBuffer与StringBuilder的区别

    本文主要介绍Java String、StringBuffer与StringBuilder的区别的资料,这里整理了相关资料及详细说明其作用和利弊点,有需要的小伙伴可以参考下

    java面试宝典

    42、一个“.java”源文件中是否可以包含多个类(不是内部类)?有什么限制? 12 43、说出一些常用的类,包,接口,请各举5 个。 12 44、Anonymous Inner Class (匿名内部类) 是否可以extends(继承)其它类?是否可以...

    Java入门教程(微学苑)-part1

    2.2 对布尔型的说明 25 2.3 Java数据类型转换 26 2.4 自动数据类型转换 26 2.5 强制数据类型转换 26 2.6 Java运算符 27 2.6.1.1 数学运算符 27 2.6.1.2 关系运算符 27 2.6.1.3 位运算符 27 2.6.1.4 条件运算符 28 ...

    JavaLearning:我对Java的学习

    IDEA_Projet春天的一个临时主题需要重新编辑 JDBC在Java中使用JDBC Java进阶注解退货虚拟机 Java基础根据日历推荐jdk1.7 LocalDate之后的日期数学随机的字符串StringBuilder馆藏了解结构arraylist链表hashmap哈希集...

    java学习笔记 初学者必读

    2.2. Java中基本概念 2-5 3. 二•定义,关键字和类型 3-5 3.1. 注释的三种形式 3-5 3.2. Java代码中的“;”、“{}”、“ ” 3-5 3.3. 标识符 3-5 3.4. 数据类型 3-6 3.5. 命名规则 3-6 4. 三•表达式和控制流 4-6 ...

    JAVA面试资料大全-整理.zip

    3、 Java 中实现多态的机制是什么? 4、 说出 ArrayList,Vector, LinkedList 的存储性能和特性 5、 Collection 和 Collections 的区别。 6、HashMap 和 Hashtable 的区别。 7、 线程的基本概念、线程的基本状态以及...

    corejava培训文档

    2.2. Java中基本概念 3. 二 定义,关键字和类型 3.1. 注释的三种形式 3.2. Java代码中的“;”、“{}”、“ ” 3.3. 标识符 3.4. 数据类型 3.5. 命名规则 4. 三 表达式和控制流 4.1. 变量和作用域 4.2. 操作...

    Java语言程序设计(第3版)第06章-字符串.pptx

    6.1.1 1 String类 2 3 主要内容 Java语言程序设计(第3版) 格式化输出 StringBuilder类 Java语言程序设计(第3版)第06章-字符串全文共31页,当前为第2页。 6.1.1 String类 Java语言程序设计(第3版) Java语言程序设计...

    corejavaNoteBook

    2.2. Java中基本概念 2-5 3. 二•定义,关键字和类型 3-5 3.1. 注释的三种形式 3-5 3.2. Java代码中的“;”、“{}”、“ ” 3-5 3.3. 标识符 3-5 3.4. 数据类型 3-6 3.5. 命名规则 3-6 4. 三•表达式和控制流 4-6 ...

    Java常用API

    2.String类/StringBuffer类/StringBuilder类 3.数组高级和Arrays类 4.基本类型包装类(Integer,Character) 5.Math类/Random类/System类 6.BigInteger类/BigDecimal类 7.Date类/DateFormat类/Calendar类

    Java工程师必学系列课程之5--《字符串及正则表达式》视频课程

    一、String、StringBuffer和StringBuilder类基本常识、基本原理和使用技巧二、字符编码常识三、Java语言正则表达式的详细语法和使用技巧四、实战案例课程说明:在开发Java程序的过程中,最常用的类莫过于字符串相关...

    java面试题,180多页,绝对良心制作,欢迎点评,涵盖各种知识点,排版优美,阅读舒心

    【基础】java中String、StringBuffer、StringBuilder的区别 21 【基础】运行时异常和非运行时异常 参见 21 运行时异常 21 非运行时异常 22 【基础】java引用类型 23 强引用(StrongReference) 23 软引用...

    wgu-c195-software-2:WGU C195的Java项目-软件II高级Java概念

    单击应用程序中的按钮也会自动打开示例-在Windows机器上,我的Java应用程序按钮默认情况下使用记事本打开我的log.txt文件创建模型并将对应的数据映射到TableViews 使用StringBuilder和TextArea基于数据库数据创建...

    MD5加密算法(Java语言描述)

    MD5加密算法(Java版) 可以运行 原理  对MD5算法简要的叙述可以为:MD5以512位分组来处理输入的信息,且每一分组又被划分为16个32位子分组,经过了一系列的处理后,算法的输出由四个32位分组组成,将这四个32位...

    javalruleetcode-LeetCode:leetcode的解决方案(Java或Python解决)

    这道题的解法是双指针,当然我是用StringBuilder,但是效果也一样,只要维护StringBuilder中最后三位和最后四位数组满足编辑关系即可 牛客-字节跳动 这道题需要注意的地方其实就是精度,需要提前取模是我这里错的...

    最新JAVA编程题全集_50题及答案

    // 中位数变量 System.out.println("数据长度:"+len); left = 0; right = len - 1; while (left ) { //由于源数据不是顺序的,需先进行排序 int temp; for(int i=0;i;++i) { ...

Global site tag (gtag.js) - Google Analytics