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. 关于LayoutParams

    每一个布局均有一个叫LayoutParams的内部类,如: LinearLayout.LayoutParams  RelativeLayout.LayoutParams  AbsoluteLayout ...

  2. JavaScript开发规范

    作为一名开发人员(WEB前端JavaScript开发),不规范的开发不仅使日后代码维护变的困难,同时也不利于团队的合作,通常还会带来代码安全以 及执行效率上的问题.本人在开发工作中就曾与不按规范来开发 ...

  3. Equations(hdu 1496 二分查找+各种剪枝)

    Equations Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  4. C# 内存泄露

    一.事件引起的内存泄露 1.不手动注销事件也不发生内存泄露的情况 我们经常会写EventHandler += AFunction; 如果没有手动注销这个Event handler类似:EventHan ...

  5. zend studio使用入门

    使用zend studio8建立项目 使用PHP开发工具zend studio8进行PHP网站开发,就需要建立(导入)相应的项目,方法如下:右键左侧Workspace,选择New | PHP Proj ...

  6. 汉子英文同行 连续英文不折行断行 的问题 兼容FIREFOX浏览器CSS

    #intro {white-space: normal;word-break: break-all;overflow: hidden;} --------------------- 案例2

  7. UESTC_传输数据 2015 UESTC Training for Graph Theory<Problem F>

    F - 传输数据 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit  ...

  8. UESTC_魔法少女小蟹 CDOJ 710

    小蟹是一名魔法少女,能熟练的施放很多魔法. 有一天魔法学院上课的时候出现了这样一道题,给一个6位数,让大家用自己的魔法,把这个6位数变成另一个给定的6位数. 小蟹翻了下魔法书,发现她有以下6种魔法: ...

  9. LeeCode-Remove Element

    Given an array and a value, remove all instances of that value in place and return the new length. T ...

  10. 2.8 Classes of Restricted Estimators

    根据所加限制的不同,可以将模型分为以下几类 RSS+Roughness penalty $PRSS(f;\lambda)=RSS(f)+\lambda J(f)$ 其中$J(f)$为对函数$f$的pe ...