网站首页 > 精选教程 正文
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()的返回结果如LATIN1、UTF16等都是字节常量。
@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比特
原文链接:https://www.cnblogs.com/WangXianSCU/p/15365978.html
猜你喜欢
- 2024-11-10 每日分享- jvm 如何压缩 java 项目的可执行文件
- 2024-11-10 15个最好用的JavaScript代码压缩工具
- 2024-11-10 Redis 6.2配置文件全解析:轻松优化缓存性能!
- 2024-11-10 Linux中常用的打包,压缩,解压 tar指令 zip指令
- 2024-11-10 Go发起HTTP2.0请求流程分析(后篇)——标头压缩
- 2024-11-10 hash碰撞的概率和可能性比你直觉中大得多
- 2024-11-10 2020上半年Java面试题总结,20多类1100道面试题...
- 2024-11-10 Facebook 发布最新数据压缩技术:可将 Android App 大小减少 20%
- 2024-11-10 一个简单的字符串,为什么 Redis 要设计的如此特别
- 2024-11-10 Android如何进行资源压缩 android资源文件路径
你 发表评论:
欢迎- 最近发表
-
- Python 列表(List)详解
- spring boot Mybatis Mapper.xml使用总结
- Python list列表详解
- Python中获取列表元素数量的方法
- Java List结构转Tree树形结构_非递归_简单优化版
- JAVA进阶知识学习-day02 Collection集合&Iterator迭代器&泛型
- Python列表(List)一文全掌握:核心知识点+20实战练习题
- 踩坑!Java集合必学技能:Collection.size()方法深度解析与避坑
- 深入理解ThreadLocal:线程安全的“独享空间”
- 构建无锁的线程安全架构:掌握Java中ThreadLocal的原理灵活应用
- 标签列表
-
- nginx反向代理 (57)
- nginx日志 (56)
- nginx限制ip访问 (62)
- mac安装nginx (55)
- java和mysql (59)
- java中final (62)
- win10安装java (72)
- java启动参数 (64)
- java链表反转 (64)
- 字符串反转java (72)
- java逻辑运算符 (59)
- java 请求url (65)
- java信号量 (57)
- java定义枚举 (59)
- java字符串压缩 (56)
- java中的反射 (59)
- java 三维数组 (55)
- java插入排序 (68)
- java线程的状态 (62)
- java异步调用 (55)
- java中的异常处理 (62)
- java锁机制 (54)
- java静态内部类 (55)
- java怎么添加图片 (60)
- java 权限框架 (55)
本文暂时没有评论,来添加一个吧(●'◡'●)