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的更多相关文章

  1. think in java 读书笔记 3 —— 数据报

    目录 think in java 读书笔记 1 ——移位 think in java 读书笔记 2 —— 套接字 think in java 读书笔记 3 —— 数据报 概要 1. 数据报基本知识 2 ...

  2. think in java 读书笔记 2 —— 套接字

    目录 think in java 读书笔记 1 ——移位 think in java 读书笔记 2 —— 套接字 think in java 读书笔记 3 —— 数据报 概要 1. 套接字基本知识 2 ...

  3. think in java 读书笔记 1 ——移位

    目录 think in java 读书笔记 1 ——移位 think in java 读书笔记 2 —— 套接字 think in java 读书笔记 3 —— 数据报 在Think in Java中 ...

  4. head first java读书笔记

    head first java读书笔记 1. 基本信息 页数:689 阅读起止日期:20170104-20170215 2. 标签 Java入门 3. 价值 8分 4. 主题 使用面向对象的思路介绍J ...

  5. Thinking In Java读书笔记--对象导论

    Thinking In Java读书笔记--对象导论[对象]服务提供者==>将对象看做一个服务提供者[程序员分类][类创造者]/[客户端程序员] [访问控制存在的原因?][1]客户端程序员无法触 ...

  6. Java读书笔记1

    Java逍遥游记读书笔记 前言 必须先来一句,这是入门级别,高手勿喷~ 写Android的时候总有一些语句不是很理解,其实大部分是Java的内容,所以想系统的学下Java. 这本书——<Java ...

  7. java读书笔记二

    这是我的一些读书笔记: 我研究了一下面向对象: 面向对象符合人类看待事物的一般规律,对象的方法的实现细节是包装的,只有对象方法的实现者了解细节 我觉得面向过程是由过程.步骤.函数组成,过程是核心,面向 ...

  8. Effective Java读书笔记完结啦

    Effective Java是一本经典的书, 很实用的Java进阶读物, 提供了各个方面的best practices. 最近终于做完了Effective Java的读书笔记, 发布出来与大家共享. ...

  9. Core Java 学习笔记——1.术语/环境配置/Eclipse汉化字体快捷键/API文档

    今天起开始学习Java,学习用书为Core Java.之前有过C的经验.准备把自己学习这一本书时的各种想法,不易理解的,重要的都记录下来.希望以后回顾起来能温故知新吧.也希望自己能够坚持把自己学习这本 ...

随机推荐

  1. contentProvider内容提供者

    contentProvider内容提供者 15. 四 / android基础 / 没有评论   步骤 权限在application中注册 Source code     <provider an ...

  2. __m128i的理解[转]

    __m128i被称为128bits的整数,当我们对其赋值时,调用 __m128i _mm_set1_epi32(int i) Sets the four signed 32-bit integer v ...

  3. 【0】python核心编程,第二章

    1.print语句也支持将输入重定向到文件,示例: logfile = open('/tmp/mylog.txt', 'a') print >> logfile, 'Fatal error ...

  4. javascript跨域获取json数据

    项目在开发过程中,用到了天气预报的功能,所以需要调用天气预报的api,一开始以为直接用ajax调用url就可以获取天气数据,结果涉及到了跨域的问题,这里做一个记录. 说到跨域,就得知道同源策略. 同源 ...

  5. HDU 4939 Stupid Tower Defense

    dp:枚举red,dp前i 个塔中有j 个蓝塔的最大伤害. 机智的地方:dp前i 个塔的时候可以同时处理n-i 个红塔,这样就少了个循环...(枚举红塔的循环) #include <iostre ...

  6. html中的特殊符号表示法

    html中的特殊符号 符号 说明 编码   符号 说明 编码   符号 说明 编码 " 双引号 " × 乘号 × ← 向左箭头 ← & AND符号 & ÷ 除号 ÷ ...

  7. TENX_ASM.uew

    /L14"TENX ASM" Nocase Line Comment = ; File Extensions = INC ASM LST H /Colors = ,,,,, /Co ...

  8. Asp.net web服务处理程序(第六篇)

    四.Web服务处理程序 对于Web服务来说,标准的方式是使用SOAP协议,在SOAP中,请求和回应的数据通过XML格式进行描述.在Asp.net 4.0下,对于Web服务来说,还可以选择支持Ajax访 ...

  9. C# 将字节流转换为图片的实例方法

    usingSystem;  usingSystem.Collections.Generic;  usingSystem.Linq;  usingSystem.Text;  usingSystem.Dr ...

  10. Linux系统编程(6)——文件系统

    计算机的文件系统是一种存储和组织计算机数据的方法,它使得对其访问和查找变得容易,文件系统使用文件和树形目录的抽象逻辑概念代替了硬盘和光盘等物理设备使用数据块的概念,用户使用文件系统来保存数据不必关心数 ...