Core Java读书笔记之String
Java里面的String
Conceptually, Java Strings are sequences of Unicode characters.
Java里面的String都是Unicode字符串
Java does not have a built-in string type.
java没有内建的字符串类型。
Each quoted string is an instance of the String class.
每个引用的字符串都是一个String类的实例。
字符串是永恒的:
Strings Are Immutable
The String class gives no methods that let you change a character in an existing string.
Just as the number 3 is always number 3,the string "hello" will always contain the code unit sequence describing the characters h,e,l,l,o.You cannot change these values.You can change the contents of the String variable and make it refer to a different string,just as you can make a numeric variable currently holding the value 3 hold the value 4.
这段话说得非常明白,就像数字3总是3一样,也就是Java里面的所有字符串都是不可更改的,你只能更改引用它的那个变量,就像让一个原来存储值为3的变量存储4一样。
关于效率的论述:
Isn't that a lot less efficient?It would be simpler to change the code units than to build up a whole new string from scratch.Well,yes and no.Indeed,it isn't efficient to generate a new string that holds the concatenation.But immutable strings have one great advantage:the compiler can arrange that strings are shared.
Overall,the designers of Java decided that the efficiency of sharing outweighs the efficiency of string editing by extracting substrings and concatening.
这段话意思是:虽然有人说改变原来的编码会变得简单一点,但是让字符串不能变化有一个优点:编译器可以安排字符串共享。Java 的发明者自有自己的考虑,他认为共享的效率要胜过通过截取拼接来编辑字符串的效率。
而且我们大部分时间只拿字符串来作比较。
和C的区别
关于字符串,作者拿来与C和C++做了区别:
C程序员眼中的字符串:
char greeting[] = "Hello";//字符数组
在Java中可不如此,他认为,"A Java string is roughly analogous to a char* pointer."即,在Java里面字符串更像一个char型的常量指针,如下:
char* greeting = "Hello";
然后他用C的方式来模拟了改变Java里面的字符串变量的引用的过程:
char *temp = malloc(6);
strncpy(temp, greeting, 3);
strncpy(temp+3,"p!",3);
greeting = temp;
即改变字符串变量的内容只是改变了变量指向的地址。而原来地址里面的内容,将会被JVM自动回收。
怎么测试字符串的相等?
To test whether two strings are equal, use equals method. Never use == to compare strings lest you end up with a program with the worst kind of bug.
“==”号只是比较两个字符串变量指向的地址是否相等。注意区分。
charAt()方法的雷区
看下面的一个例子:
这是一句话:
@ is the set of integers.
执行以下代码:
char ch = setence.charAt(1);
猜一猜输出结果。。。。
一般情况下肯定是空格。
但是如果句首的字符是一个UTF-16编码的字母,则结果就不一样了。用charAt()方法只能得到其编码的后半部分。要注意这一点。
String API
列出了常用的方法:
char charAt(int index);
int compareTo(String other);
boolean endWith(String other);
boolean equals(Object other);
boolean equalsIgnoreCase(String other);
int lastIndexOf(String str);
int length();
String replace(CharSequence oldString, CharSequence newString);
boolean startWith(String prefix);
String toLowerCase();
String toUpperCase();
String trim();
构造字符串
Occasionally, you need to build up strings from shorter strings,such as keystrokes or words from files.It would be iniefficient to use string concatenation for this purpose.
因为每次拼接字符串都会产生一个废弃的就字符串对象。所以需要用到StringBuilder对象,直接在原来的字符串上进行操作。
每次拼接的时候,用append方法即可。
常用的API
int length();
StringBuilder append(String str);
StringBuilder insert(int offset, String str);
StringBuilder delete(int startIndex, int endIndex);
StringBuilder 的前任是 StringBuffer,效率较低,但是支持多线程。通常,如果只是单线程进行编辑的话,还是使用StringBuilder较好。
说点废话
Core Java上确实有很多值得细细品味啊,看了这一篇关于字符串的介绍,感觉基础还是不牢固啊,还得加强,后面还会有系列文章,讲Core Java。
Core Java读书笔记之String的更多相关文章
- think in java 读书笔记 3 —— 数据报
目录 think in java 读书笔记 1 ——移位 think in java 读书笔记 2 —— 套接字 think in java 读书笔记 3 —— 数据报 概要 1. 数据报基本知识 2 ...
- think in java 读书笔记 2 —— 套接字
目录 think in java 读书笔记 1 ——移位 think in java 读书笔记 2 —— 套接字 think in java 读书笔记 3 —— 数据报 概要 1. 套接字基本知识 2 ...
- think in java 读书笔记 1 ——移位
目录 think in java 读书笔记 1 ——移位 think in java 读书笔记 2 —— 套接字 think in java 读书笔记 3 —— 数据报 在Think in Java中 ...
- head first java读书笔记
head first java读书笔记 1. 基本信息 页数:689 阅读起止日期:20170104-20170215 2. 标签 Java入门 3. 价值 8分 4. 主题 使用面向对象的思路介绍J ...
- Thinking In Java读书笔记--对象导论
Thinking In Java读书笔记--对象导论[对象]服务提供者==>将对象看做一个服务提供者[程序员分类][类创造者]/[客户端程序员] [访问控制存在的原因?][1]客户端程序员无法触 ...
- Java读书笔记1
Java逍遥游记读书笔记 前言 必须先来一句,这是入门级别,高手勿喷~ 写Android的时候总有一些语句不是很理解,其实大部分是Java的内容,所以想系统的学下Java. 这本书——<Java ...
- java读书笔记二
这是我的一些读书笔记: 我研究了一下面向对象: 面向对象符合人类看待事物的一般规律,对象的方法的实现细节是包装的,只有对象方法的实现者了解细节 我觉得面向过程是由过程.步骤.函数组成,过程是核心,面向 ...
- Effective Java读书笔记完结啦
Effective Java是一本经典的书, 很实用的Java进阶读物, 提供了各个方面的best practices. 最近终于做完了Effective Java的读书笔记, 发布出来与大家共享. ...
- Core Java 学习笔记——1.术语/环境配置/Eclipse汉化字体快捷键/API文档
今天起开始学习Java,学习用书为Core Java.之前有过C的经验.准备把自己学习这一本书时的各种想法,不易理解的,重要的都记录下来.希望以后回顾起来能温故知新吧.也希望自己能够坚持把自己学习这本 ...
随机推荐
- OpenCV——常用函数查询
1.cvLoadImage:将图像文件加载至内存: 2.cvNamedWindow:在屏幕上创建一个窗口: 3.cvShowImage:在一个已创建好的窗口中显示图像: 4.cvWaitKey:使程序 ...
- uva 10763 Foreign Exchange <"map" ,vector>
Foreign Exchange Your non-profit organization (iCORE - international Confederation of Revolver Enthu ...
- shell中的数学运算
shell中要进行数学运算通常有3中方法: expr命令 比如 expr 1 + 6就会返回7,使用expr的缺点就是碰到乘法运算,或者加括号(因为它们在shell中有其他意义),需要使用转义,比如: ...
- Linq的基础2
var 构建匿名类型1 = from c in ctx.GetTable<Customers>() select new ...
- js戳和php戳时间换算
问题:剩余多少时间,如果只用php来输出,却看不到动态效果.解决办法,利用获取的时间减去当前时间js 时间格式转换php时间商品距离秒杀时间的天数时分秒<input name="tes ...
- EF 请求数据是缓存 求大神解释
// //AliexpressEntities MyaliexpressEntities 为了事物一致性 在别的方法里面传过来的 实质还是 ( AliexpressEntities aliexpre ...
- 利用SQL Profiler处理开销较大的查询
当SQL Server的性能变差时,最可能发生的是以下两件事: 首先,某些查询产生了系统资源上很大的压力.这些查询影响整个系统的性能,因为服务器无法足够快速地服务其他SQL查询. 另外,开销较大的查询 ...
- C# 文件/文件夹压缩
一.ZipFile ZipFile类用于选择文件或文件夹进行压缩生成压缩包. 常用属性: 属性 说明 Count 文件数目(注意是在ComitUpdat之后才有) Password 压缩包密码 Siz ...
- 【Xamarin挖墙脚系列:代码手写UI,xib和StoryBoard间的博弈,以及Interface Builder的一些小技巧(转)】
正愁如何选择构建项目中的视图呢,现在官方推荐画板 Storybord...但是好像 xib貌似更胜一筹.以前的老棒子总喜欢装吊,用代码写....用代码堆一个HTML页面不知道你们尝试过没有.等页面做出 ...
- MIT-scheme安装
下载地址: http://www.gnu.org/software/mit-scheme/ 下载windows版本,安装. The MIT-Scheme can be installed by jus ...