TIJ——Chapter Thirteen:Strings
- String 对象是不可修改的,对于被String 重载的'+' 和'+=' 运算符来说,当你用它们来连接两个String 对象的时候,它在底层并不会对于每一次连接均生成一个String 对象,取而代之,在底层会有一个非线程安全的可修改的StringBuilder 对象被构造出来,并且调用其append()方法来生成我们需要拼接的字符串。关于这一点,我们可以随便编写一个字符串拼接语句,然后使用JDK自带的javap 工具,通过javap -c 编译后的类文件名 命令来查看其对应的JVM字节码(相当于汇编语言)来得到一些启示。不过,应当注意的一点是,在循环语句块中拼接字符串,在每次循环的过程中都会生成一个新的StringBuilder 对象(即该对象的构造发生在循环的内部),因此,如果你关注程序的性能,那么这种情况下显式地使用StringBuilder 对象的append 方法是很有必要的。不过,像append(a + ":" + c) 这种语句,对于内部的连接语句,编译器又会帮你生成一个新的StringBuilder 对象,这种情况下可以分步append:append(a).append(":").append(c)。
- StringBuilder was introduced in Java SE5. Prior to this, Java used StringBuffer, which ensured thread safety and so was significantly more expensive. Thus, string operations in Java SE5/6 should be faster.
- 如果重写了某个类的toString 方法,那么不要在其中单纯地使用this 拼接字符串,因为这会导致程序无限递归调用该类的toString 方法,最终导致栈溢出。如果想要获取该对象的地址,可以调用super.toString() 或者Integer.toHexString(this.hashCode()) 来获得。
- Formatter provides powerful control over spacing and alignment with fairly concise notation.
Regular expressions
- In general, you'll compile regular expression objects rather than using the fairly limited String utilities. To do this, you import java.util.regex, then compile a regular expression by using the static Pattern.compile() method. This produces Pattern object based on its String argument. You use the Pattern by calling the matcher() method, passing the string that you want to search. The matcher() method produces a Matcher object, which has a set of operations to choose from(you can see all of these in the JDK documentation for java.util.regex.Matcher). For example, the replaceAll() method replaces all the matches with its argument.
- find() is like an iterator, moving forward through the input string.
- Groups are regular expressions set off by parentheses that can be called up later with their group number. Group 0 indicates the whole expression match, group 1 is the first parenthesized group, etc. Thus in A(B(C))D, There are three groups: Group 0 is ABCD, group 1 is BC, and group 2 is C.
TIJ——Chapter Thirteen:Strings的更多相关文章
- TIJ——Chapter Two:Everything Is an Object
If we spoke a different language, we would perceive a somewhat different world. Ludwig Wittgenstein( ...
- TIJ——Chapter Eleven:Holding Your Objects
Java Provides a number of ways to hold objects: An array associates numerical indexes to objects. It ...
- TIJ——Chapter One:Introduction to Objects
///:~容我对这个系列美其名曰"读书笔记",其实shi在练习英文哈:-) Introduction to Objects Object-oriented programming( ...
- Think Python - Chapter 8 - Strings
8.1 A string is a sequenceA string is a sequence of characters. You can access the characters one at ...
- TIJ——Chapter Twelve:Error Handling with Exception
Exception guidelines Use exceptions to: Handle problems at the appropriate level.(Avoid catching exc ...
- TIJ——Chapter Eight:Polymorphism
The twist |_Method-call binding Connecting a method call to a method body is called binding. When bi ...
- TIJ——Chapter Seven:Reusing Classes
Reusing Classes 有两种常用方式实现类的重用,组件(在新类中创建存在类的对象)和继承. Composition syntax Every non-primitive object has ...
- TIJ——Chapter Five:Initialization & Cleanup
Method overloading |_Distinguishing overloaded methods If the methods hava the same name, how can Ja ...
- TIJ——Chapter Fourteen:Type Information
Runtime type information(RTTI) allows you to discover and use type information while a program is ru ...
随机推荐
- hbase过滤器(1)
最近在公司做hbase就打算复习下它的过滤器以便不时之需,RowFilter根据行键(rowkey)筛选数据 public void filter() throws IOException { Fil ...
- pytorch 加载训练好的模型做inference
前提: 模型参数和结构是分别保存的 1. 构建模型(# load model graph) model = MODEL() 2.加载模型参数(# load model state_dict) mode ...
- JavaScript内容回顾
<!DOCTYPE html> <!--JavaScript内容回顾--> <html lang="en"> <head> < ...
- Spring MVC中获取当前项目的路径
Spring MVC中获取当前项目的路径 在web.xml中加入以下内容 <!--获取项目路径--> <context-param> <param-name>web ...
- ES6--反引号的使用
/*动态初始退出登出框话模态框*/ /*动态的初始化退出登陆模态框 反引号ES6语法 * 为什么在使用字符串格式直接创建模态框 * 1.不能在html页面中创建模板,因为如果换一个页面就没有对应的模板 ...
- Linq 之 Where操作
适用场景:实现过滤,查询等功能. 说明:与SQL命令中的Where作用相似,都是起到范围限定也就是过滤作用的,而判断条件就是它后面所接的子句. Where操作包括3种形式,分别为简单形式.关系条件形式 ...
- 下载android4.4.2源代码全过程(附已下载的源代码)
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/maosidiaoxian/article/details/36625727 今天在下载andriod ...
- webpack学习之—— Plugins
Plugins are the backbone of webpack! webpack 自身也是构建于你在 webpack 配置中用到的相同的插件系统之上! 插件目的在于解决 loader 无法实现 ...
- jquery中的index方法和eq方法
jquery的index()方法 搜索匹配的元素,并返回相应元素的索引值,从0开始计数,如果不给 .index() 方法传递参数,那么返回值就是这个jQuery对象集合中第一个元素相对于其同辈元素的位 ...
- 使用php实现单点登录
1.准备两个虚拟域名 127.0.0.1 www.openpoor.com 127.0.0.1 www.myspace.com 2.在openpoor的根目录下创建以下文件 index.PHP [ ...