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 ...
随机推荐
- MySQL用户权限详细汇总
1,MySQL权限体系 mysql 的权限体系大致分为5个层级:全局层级:全局权限适用于一个给定服务器中的所有数据库.这些权限存储在mysql.user表中.GRANT ALL ON .和REVOKE ...
- Hadoop中map数的计算
转载▼ Hadoop中在计算一个JOB需要的map数之前首先要计算分片的大小.计算分片大小的公式是: goalSize = totalSize / mapred.map.tasks minSize = ...
- 5+App 基于HTML、JS、CSS编写的运行于手机端的App(DCloud开发)
HTML5 Plus移动App(5+App) 工具: 开发工具HBuilder下载下载地址 开发工具HBuilderX下载下载地址(替代HBuilder) 框架: uni-app :是一个使用 Vue ...
- vue 图片上传功能
这次做了vue页面的图片上传功能,不带裁剪功能的! 首先是html代码,在input框上添加change事件,如下: <ul class="clearfix"> ...
- Codeforces 467D
题目链接 D. Fedor and Essay time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- Spring Bean 作用域
Bean 的作用域 当在 Spring 中定义一个 bean 时,你必须声明该 bean 的作用域的选项.例如,为了强制 Spring 在每次需要时都产生一个新的 bean 实例,你应该声明 bean ...
- 代码规范:在Keil5中使用代码格式化工具Astyle(插件)
r如果日志提示没有被格式化,那么检查你的文件是不是有错误,编译一下,没错误的时候才能格式化 keil mdk都啥年代了每次更新还是都无法解决补全bug也不添加格式化工具 所以这里要用一个第三方插件ht ...
- NOIP模拟17.10.12
T1 临江仙 旧梦 题目背景 闻道故园花陌,今年奼紫嫣红.扬帆直渡水千重.东君何解意,送我一江风. 还是昔时庭院,终得醉卧花丛.残更惊醒月明中.流光如旧岁,多少梦成空. 题目描述 #define go ...
- Leetcode46. Permutations全排列
给定一个没有重复数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1 ...
- fileinput URL携带参数的问题,uploadExtraData,Bootstrap
因为无法在URL中执行其他代码,通过阅读文档可知可以由uploadExtraData参数携带数据.于是在这里携带,如果uploadExtraData 无法接收到数据,一般是格式有误, 先尝试这个简单的 ...