上一节主要介绍了String类的一些构造方法,主要分为四类

  • 无参构造器:String(),创建一个空字符串"",区别于null字符串,""已经初始化,null并未初始化
  • 副本构造器:String(String s),简单的赋值,得到的是一个副本,俩个引用指向的是常量池中的同一个String,但是String是不可变的,所有用意不大
  • byte类构造器:String(byte[] b),将byte数组转换为String,注意并不是简单的赋值,而是整体copy一份
  • char类构造器:String(char[] c),将char数组转换成String,注意并不是简单的赋值,而是整体copy一份
  • codepoints构构器:String(int[] i),将超过char表示范围的字符转换为为String
  • String缓存流的转换:String(StringBuffer),将字符换缓冲流转换为String
    /*
* 获得字符串的长度,也就是属性value数组的长度
*/
public int length() {
return value.length;
} /*
* 判断字符串是否为空,也就是判断属性value数组的长度是否等于0
*/
public boolean isEmpty() {
return value.length == 0;
} /*
* charAt方法可以直接获取当前下标对应的字符,这对于判断某个位置的字符值很方便
* 区别于
*/
public char charAt(int index) {
if ((index < 0) || (index >= value.length)) {
throw new StringIndexOutOfBoundsException(index);
}
return value[index];
} /*
* 返回指定索引处的字符(Unicode代码点)。 codePoint在上一章已经介绍过了,具体就是那些超出byte和char表示方位的字符
*/
public int codePointAt(int index) {
if ((index < 0) || (index >= value.length)) {
throw new StringIndexOutOfBoundsException(index);
}
return Character.codePointAtImpl(value, index, value.length);
} /*
* 返回指定索引之前的字符(Unicode代码点)。
*/
public int codePointBefore(int index) {
int i = index - 1;
if ((i < 0) || (i >= value.length)) {
throw new StringIndexOutOfBoundsException(index);
}
return Character.codePointBeforeImpl(value, index, 0);
} /*
* 返回此 String指定文本范围内的Unicode代码点数。
*/
public int codePointCount(int beginIndex, int endIndex) {
if (beginIndex < 0 || endIndex > value.length || beginIndex > endIndex) {
throw new IndexOutOfBoundsException();
}
return Character.codePointCountImpl(value, beginIndex, endIndex - beginIndex);
} /*
* 返回此String中的索引,该索引通过codePointOffset代码点从给定的index偏移。
* index和codePointOffset给出的文本范围内的未配对代理计为每个代码点。
*
*/
public int offsetByCodePoints(int index, int codePointOffset) {
if (index < 0 || index > value.length) {
throw new IndexOutOfBoundsException();
}
return Character.offsetByCodePointsImpl(value, 0, value.length, index, codePointOffset);
} /*
* 将此字符串复制给dst字节数组,默认为整体复制,其中dstBegin为目标数组dst中的开始位置
* 此方法为包访问权限,所以我们并没有调用的权限
*/
void getChars(char dst[], int dstBegin) {
System.arraycopy(value, 0, dst, dstBegin, value.length);
} /*
* 将此字符串的额指定位置开始到指定位置结束复制给dst字节数组,其中dstBegin为目标数组dst的开始位置,
* srcBegin和srcEnd分别为本字符串的开始位置和结束位置
*/
public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) {
if (srcBegin < 0) {
throw new StringIndexOutOfBoundsException(srcBegin);
}
if (srcEnd > value.length) {
throw new StringIndexOutOfBoundsException(srcEnd);
}
if (srcBegin > srcEnd) {
throw new StringIndexOutOfBoundsException(srcEnd - srcBegin);
}
System.arraycopy(value, srcBegin, dst, dstBegin, srcEnd - srcBegin);
} /*
* 方法已过时
* 使用平台的默认字符集将此 String编码为字节序列,将结果存储到新的字节数组中,可以指定字符串的开始位置和结束位置,
* 但结果往往是不正确的,在源码中我们可以看到,仅仅是将字符串的每一位字符强转成字节类型,实际用处很少。
*
*/
@Deprecated
public void getBytes(int srcBegin, int srcEnd, byte dst[], int dstBegin) {
if (srcBegin < 0) {
throw new StringIndexOutOfBoundsException(srcBegin);
}
if (srcEnd > value.length) {
throw new StringIndexOutOfBoundsException(srcEnd);
}
if (srcBegin > srcEnd) {
throw new StringIndexOutOfBoundsException(srcEnd - srcBegin);
}
Objects.requireNonNull(dst); int j = dstBegin;
int n = srcEnd;
int i = srcBegin;
char[] val = value; /* avoid getfield opcode */ while (i < n) {
dst[j++] = (byte) val[i++];
}
} /*
* 指定编码字符集将此 String编码为字节序列,将结果存储到新的字节数组中
*/
public byte[] getBytes(String charsetName) throws UnsupportedEncodingException {
if (charsetName == null)
throw new NullPointerException();
return StringCoding.encode(charsetName, value, 0, value.length);
} /*
* 指定编码字符集将此 String编码为字节序列,将结果存储到新的字节数组中
*/
public byte[] getBytes(Charset charset) {
if (charset == null)
throw new NullPointerException();
return StringCoding.encode(charset, value, 0, value.length);
} /*
* 使用平台的默认字符集将此 String编码为字节序列,将结果存储到新的字节数组中
*/
public byte[] getBytes() {
return StringCoding.encode(value, 0, value.length);
} /*
* String重写Object类的equals方法,详细阅读源码,我们发现形参是Object类型,那么在实际传参的过程当中
* 除了String类型,别的引用类型都会返回false,重点注意的是StringBuffer和StringBuild,在实际比较的时候一定要先进行转换
* equals实际比较的就是字符串的每一位依次进行比较,直到全部比较完毕都没有不一样的才返回true
*/
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String) anObject;
int n = value.length;
if (n == anotherString.value.length) {
char v1[] = value;
char v2[] = anotherString.value;
int i = 0;
while (n-- != 0) {
if (v1[i] != v2[i])
return false;
i++;
}
return true;
}
}
return false;
} /*
* 将此字符串与指定的StringBuffer进行 比较内容是否相等
*/
public boolean contentEquals(StringBuffer sb) {
return contentEquals((CharSequence) sb);
} private boolean nonSyncContentEquals(AbstractStringBuilder sb) {
char v1[] = value;
char v2[] = sb.getValue();
int n = v1.length;
if (n != sb.length()) {
return false;
}
for (int i = 0; i < n; i++) {
if (v1[i] != v2[i]) {
return false;
}
}
return true;
} /*
* 将此字符串与指定的CharSequence进行内容比较是否相等
*/
public boolean contentEquals(CharSequence cs) {
// Argument is a StringBuffer, StringBuilder
if (cs instanceof AbstractStringBuilder) {
if (cs instanceof StringBuffer) {
synchronized (cs) {
return nonSyncContentEquals((AbstractStringBuilder) cs);
}
} else {
return nonSyncContentEquals((AbstractStringBuilder) cs);
}
}
// Argument is a String
if (cs instanceof String) {
return equals(cs);
}
// Argument is a generic CharSequence
char v1[] = value;
int n = v1.length;
if (n != cs.length()) {
return false;
}
for (int i = 0; i < n; i++) {
if (v1[i] != cs.charAt(i)) {
return false;
}
}
return true;
} /*
* 能用一行代价解决的事就不用俩行,三元运算符?:直接解决。其中regionMatches(true, 0, anotherString, 0, value.length)是测试是否相等的函数
* 第一个参数true代表忽略大小写
*/
public boolean equalsIgnoreCase(String anotherString) {
return (this == anotherString) ? true
: (anotherString != null) && (anotherString.value.length == value.length)
&& regionMatches(true, 0, anotherString, 0, value.length);
}

java源码解析之String类(二)的更多相关文章

  1. java源码解析之String类(一)

    String是我们接触最多的类,无论是学习中还是工作中,基本每天都会和字符串打交道,从字符串本身的各种拼接.切片.变形,再到和其他基本数据类型的转换,几乎无时无刻都在使用它,今天就让我们揭开Strin ...

  2. java源码解析之String类(三)

    上一节我们主要讲了String类的一些不是很常用的方法,其中需要掌握的如下,我就不再赘述了 public int length() public boolean isEmpty() public by ...

  3. java源码解析之String类(四)

    /* * 返回指定字符第一次出现的字符串内的索引 */ public int indexOf(int ch) { return indexOf(ch, 0); } /* * 返回指定字符第一次出现的字 ...

  4. java源码解析之String类(五)

    /* * 切片函数,非常重要,这里一定要牢记beginIndex是开始位置,endIndex是结束位置,区别于以前学的offset是开始位置,而count或length是个数和长度 * 比如说,new ...

  5. Java源码解析——集合框架(二)——ArrayBlockingQueue

    ArrayBlockingQueue源码解析 ArrayBlockingQueue是一个阻塞式的队列,继承自AbstractBlockingQueue,间接的实现了Queue接口和Collection ...

  6. java源码解析之Object类

    一.Object类概述   Object类是java中类层次的根,是所有类的基类.在编译时会自动导入.Object中的方法如下: 二.方法详解   Object的方法可以分成两类,一类是被关键字fin ...

  7. Spring源码解析之ConfigurationClassPostProcessor(二)

    上一个章节,笔者向大家介绍了spring是如何来过滤配置类的,下面我们来看看在过滤出配置类后,spring是如何来解析配置类的.首先过滤出来的配置类会存放在configCandidates列表, 在代 ...

  8. [Java源码解析] -- String类的compareTo(String otherString)方法的源码解析

    String类下的compareTo(String otherString)方法的源码解析 一. 前言 近日研究了一下String类的一些方法, 通过查看源码, 对一些常用的方法也有了更透彻的认识,  ...

  9. [java源码解析]对HashMap源码的分析(二)

    上文我们讲了HashMap那骚骚的逻辑结构,这一篇我们来吹吹它的实现思想,也就是算法层面.有兴趣看下或者回顾上一篇HashMap逻辑层面的,可以看下HashMap源码解析(一).使用了哈希表得“拉链法 ...

随机推荐

  1. OpenGL(十二) 纹理映射(贴图)

    OpenGL绘制纹理的步骤: 1. 开启纹理功能 使用glEnable(GL_TEXTURE_2D)开启2D纹理功能,使用glDisable(GL_TEXTURE_2D)关闭纹理,默认情况下纹理是关闭 ...

  2. sql server & .net core 使用空间数据

    使用的库 Microsoft.EntityFrameworkCore.SqlServer Microsoft.EntityFrameworkCore.SqlServer.NetTopologySuit ...

  3. WPF - 本质:数据和行为

    原文:WPF - 本质:数据和行为 如果自己来做一个UI框架,我们会首先关注哪些方面?我想UI框架主要处理的一定包括两个主要层次的内容,一个是数据展现,另一个就是数据操作,所以UI框架必须能够接收各种 ...

  4. Emgu-WPF学习使用-阈值化

    原文:Emgu-WPF学习使用-阈值化 环境:Win8 64位 Vs2015 Emgu 版本:emgucv-windesktop 3.2.0.2682 上图为常用阈值化处理效果.不同阈值设置可呈现不同 ...

  5. 【C++】【TinyXml】xml文件的读写功能使用——写xml文件

    TinyXml工具是常用比较简单的C++中xml读写的工具 需要加载 #include "TinyXml\tinyxml.h" 在TinyXML中,根据XML的各种元素来定义了一些 ...

  6. Install Oracle 12c R2 on CentOS 7 silently

    准备工作 VMware 虚拟机 CentOS 7 17.08 系统安装包镜像 Oracle 12c R2 软件安装包 配置 yum 库并安装如下包 binutils-2.23.52.0.1-12.el ...

  7. Delphi7下安装TMS component控件

    1.启动delphi7.2.File-->Open Project ...打开TMS component 源目录下的“tmsd7.bpg”. 3.在打开的窗口列表中,依次在各选项上点击鼠标右键, ...

  8. Win10《芒果TV》商店版更新v3.2.1:优化手机版卡顿,修复推送故障

    此版本是小版本更新,主要是修复上一版本发布后暴露的部分体验问题,以免进一步扩大影响,小幅修复后更新上线. 芒果TV UWP V3.2.1更新内容清单: 1.优化和修复列表预加载机制的本地保存丢失导致的 ...

  9. C#管理服务停止启动

    由于机器性能问题,把许多服务关闭了,需要用的时候再开启,这样每次都打开服务管理或cmd命令比较麻烦.就自己写了工具显示在桌面上; 声明:ServiceController myController = ...

  10. JAVA 与 PHP 的不同和相同

    Java语言与PHP语言因为都可以做web应用开发,所以总有入门学习这2种语言的入门者会问Java和PHP哪一个好.讨论语言的好坏是一件不太明智的事情,我认为只能去说那一种编程语言更加适合干什么,人与 ...