String

public final class String
implements java.io.Serializable, Comparable<String>, CharSequence,
Constable, ConstantDesc {
...
}

1. value数组

用于存储String的字符。

@Stable
private final byte[] value;

@Stable注解表示变量最多被修改一次,称为“稳定的”。

2. checkBoundsOffCount和checkBoundsBeginEnd方法

String的很多构造方法使用了数组或者其他一些集合来创建新的String,这些构造方法大多含有以下参数

  • 集合S
  • 取值左端点p
  • 取值长度l

那么需要一个方法来检查这几个参数是否合法,所以String类提供了checkBoundsOffCount()方法

static void checkBoundsOffCount(int offset, int count, int length) {
if (offset < 0 || count < 0 || offset > length - count) {
throw new StringIndexOutOfBoundsException(
"offset " + offset + ", count " + count + ", length " + length);
}
}

如下是一个以bytes数组初始化String的例子,使用了上述方法。

public String(byte bytes[], int offset, int length, String charsetName)
throws UnsupportedEncodingException {
if (charsetName == null)
throw new NullPointerException("charsetName");
// 检查参数边界合法性
checkBoundsOffCount(offset, length, bytes.length);
StringCoding.Result ret =
StringCoding.decode(charsetName, bytes, offset, length);
this.value = ret.value;
this.coder = ret.coder;
}

同时,还有一个checkBoundsBeginEnd()方法

static void checkBoundsBeginEnd(int begin, int end, int length) {
if (begin < 0 || begin > end || end > length) {
throw new StringIndexOutOfBoundsException(
"begin " + begin + ", end " + end + ", length " + length);
}
}

这个方法可以检查begin、end以及length是否适配

3. COMPACT_STRINGS常量

static final boolean COMPACT_STRINGS;

static {
COMPACT_STRINGS = true;
}

这个常量用于表示是否支持字符串压缩,默认为true。

coder()方法内,要求返回String的编码,它是这样实现的。

byte coder(){
return COMPACT_STRINGS ? coder : UTF16;
}

如果支持字符串压缩,就正常返回该字符串的coder,否则必须使用UTF16。

4. length方法

public int length() {
return value.length >> coder();
}

获取value的长度,再根据字符集进行处理。

coder()的返回结果如LATIN1UTF16等都是字节常量。

@Native static final byte LATIN1 = 0;
@Native static final byte UTF16 = 1;

5. getChars方法

public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin)

这个方法可以将String中的一部分转化字符存到chars数组中。下面是一个简单的使用示例。

public static void main(String[] args) {
String s = "abc";
char[] cs = new char[50];
s.getChars(0,2,cs,0);
System.out.println(cs);
}

输出

ab

6. comepareTo方法

public int compareTo(String anotherString) {
byte v1[] = value;
byte v2[] = anotherString.value;
byte coder = coder();
if (coder == anotherString.coder()) {
return coder == LATIN1 ? StringLatin1.compareTo(v1, v2)
: StringUTF16.compareTo(v1, v2);
}
return coder == LATIN1 ? StringLatin1.compareToUTF16(v1, v2)
: StringUTF16.compareToLatin1(v1, v2);
}

当调用String的compareTo方法时,会根据字符串的编码类型选择不同类的静态方法。而它们内部实现compare的算法都是类似的。下面给出一个例子。

// StringLating类
public static int compareTo(byte[] value, byte[] other, int len1, int len2) {
int lim = Math.min(len1, len2);
for (int k = 0; k < lim; k++) {
if (value[k] != other[k]) {
return getChar(value, k) - getChar(other, k);
}
}
return len1 - len2;
}

7. hashCode方法

String的哈希方法采用这个公式:$s[0]31^{(n-1)} + s[1]31^{(n-2)} + \cdots + s[n-1]$

public int hashCode() {
int h = hash;
if (h == 0 && !hashIsZero) {
h = isLatin1() ? StringLatin1.hashCode(value)
: StringUTF16.hashCode(value);
if (h == 0) {
hashIsZero = true;
} else {
hash = h;
}
}
return h;
}

下面是StringLatin.hashCode()

public static int hashCode(byte[] value) {
int h = 0;
for (byte v : value) {
h = 31 * h + (v & 0xff);
}
return h;
}

8. replaceAll和replaceFirst方法

public String replaceAll(String regex, String replacement) {
return Pattern.compile(regex).matcher(this).replaceAll(replacement);
} public String replaceFirst(String regex, String replacement) {
// 用regex表示的正则表达式去匹配当前字符串,再调用Matcher.replace
return Pattern.compile(regex).matcher(this).replaceFirst(replacement);
}

replaceAll()replaceFirst()方法接受regex参数和replacement参数。Pattern类从调用compile()方法解析正则表达式regex返回一个Mathcher,再调用它的replaceAll()replaceFirst()方法。

9. LATIN1和UTF16

String类中定义了LATIN1和UTF16两个编码格式

@Native static final byte LATIN1 = 0;
@Native static final byte UTF16 = 1;
  • ISO Latin-1是8比特的字符集,定义了256个字符。前128个字符(00000000-01111111)与ASCII完全一致。

    • 所以使用LATIN1作为编码格式时,往往要通过&0xff的方式取低八位
  • UTF-16也是采用可变长度编码,可以是一个或者两个16比特。

最新文章

  1. 解决谷歌浏览器中的input背景色默认是黄色
  2. nginx反向代理(proxy_pass)tomcat的过程中,session失效的问题解决
  3. C++ const、volatile、mutable的用法 (转)
  4. android 学习资料
  5. ajax中网页传输(二)JSON——下拉列表显示练习
  6. Android2.2 API中文文档——View
  7. C#实现文件数据库
  8. Delphi图像处理控件
  9. jsp和serverlet的差别
  10. php基础(三)超全局变量
  11. emoji图像转码解码 存入数据库
  12. swift 中使用OC第三方库(以AFNetworking为例)
  13. 【NOI复习】树链剖分
  14. [NOIp 2015]斗地主
  15. Vue中的render函数随笔
  16. django项目----函数和方法的区别
  17. 向java高级工程师和项目经理的道路进发【转】
  18. 虚拟机怎么发送ctrl+alt+delete组合键
  19. shell脚本练习【转】
  20. UNIX 家族及Linux

热门文章

  1. 哈希表(HashMap)分析及实现(JAVA)
  2. 刷题-力扣-63. 不同路径 II
  3. python3使用pycuda执行简单GPU计算任务
  4. Java集合的使用
  5. VMware虚拟机 + ubuntu16.04 Linux OpenCV打不开摄像头解决办法
  6. Error establishing a database connection!
  7. 用C++实现的Eratosthenes筛法程序
  8. C++模板简介
  9. HearthbuddyHelper已经开源
  10. 使用Python来临时启动端口,用来做安全时候的扫描用