1、String类注释说明

  1. /**
  2. * The {@code String} class represents character strings. All
  3. * string literals in Java programs, such as {@code "abc"}, are
  4. * implemented as instances of this class.
  5. * <p>
  6. * Strings are constant; their values cannot be changed after they
  7. * are created. String buffers support mutable strings.
  8. */

注释说明String是不变的类,即String类的值一旦被生成后就不能被改变了。

2、String类声明

  1. public final class String implements java.io.Serializable, Comparable<String>, CharSequence

final关键字修饰String类表示String类不可以被其他类继承。

3、String类的属性

  1. /** The value is used for character storage. */
  2. private final char value[]; // final关键字修饰属性,可以看出String对象确实是不可改变的
  3.  
  4. /** Cache the hash code for the string */
  5. private int hash; // Default to 0
  6.  
  7. /** use serialVersionUID from JDK 1.0.2 for interoperability */
  8. private static final long serialVersionUID = -6849794470754667710L;
  9.  
  10. /**
  11. * Class String is special cased within the Serialization Stream Protocol.
  12. *
  13. * A String instance is written into an ObjectOutputStream according to
  14. * <a href="{@docRoot}/../platform/serialization/spec/output.html">
  15. * Object Serialization Specification, Section 6.2, "Stream Elements"</a>
  16. */
  17. private static final ObjectStreamField[] serialPersistentFields =
  18. new ObjectStreamField[0];

4、String类构造方法

  1. // 无参构造方法一般不用,因为值为空
  2. public String() {
  3. this.value = "".value;
  4. }
  5.  
  6. public String(String original) {
  7. this.value = original.value;
  8. this.hash = original.hash;
  9. }
  10.  
  11. public String(char value[]) {
  12. this.value = Arrays.copyOf(value, value.length);
  13. }
  14.  
  15. public String(char value[], int offset, int count) {
  16. if (offset < 0) {
  17. throw new StringIndexOutOfBoundsException(offset);
  18. }
  19. if (count <= 0) {
  20. if (count < 0) {
  21. throw new StringIndexOutOfBoundsException(count);
  22. }
  23. if (offset <= value.length) {
  24. this.value = "".value;
  25. return;
  26. }
  27. }
  28. // Note: offset or count might be near -1>>>1.
  29. if (offset > value.length - count) {
  30. throw new StringIndexOutOfBoundsException(offset + count);
  31. }
  32. this.value = Arrays.copyOfRange(value, offset, offset+count);
  33. }
  34.  
  35. public String(int[] codePoints, int offset, int count) {
  36. if (offset < 0) {
  37. throw new StringIndexOutOfBoundsException(offset);
  38. }
  39. if (count <= 0) {
  40. if (count < 0) {
  41. throw new StringIndexOutOfBoundsException(count);
  42. }
  43. if (offset <= codePoints.length) {
  44. this.value = "".value;
  45. return;
  46. }
  47. }
  48. // Note: offset or count might be near -1>>>1.
  49. if (offset > codePoints.length - count) {
  50. throw new StringIndexOutOfBoundsException(offset + count);
  51. }
  52.  
  53. final int end = offset + count;
  54.  
  55. // Pass 1: Compute precise size of char[]
  56. int n = count;
  57. for (int i = offset; i < end; i++) {
  58. int c = codePoints[i];
  59. if (Character.isBmpCodePoint(c))
  60. continue;
  61. else if (Character.isValidCodePoint(c))
  62. n++;
  63. else throw new IllegalArgumentException(Integer.toString(c));
  64. }
  65.  
  66. // Pass 2: Allocate and fill in char[]
  67. final char[] v = new char[n];
  68.  
  69. for (int i = offset, j = 0; i < end; i++, j++) {
  70. int c = codePoints[i];
  71. if (Character.isBmpCodePoint(c))
  72. v[j] = (char)c;
  73. else
  74. Character.toSurrogates(c, v, j++);
  75. }
  76.  
  77. this.value = v;
  78. }
  79.  
  80. /* Common private utility method used to bounds check the byte array
  81. * and requested offset & length values used by the String(byte[],..)
  82. * constructors.
  83. */
  84. private static void checkBounds(byte[] bytes, int offset, int length) {
  85. if (length < 0)
  86. throw new StringIndexOutOfBoundsException(length);
  87. if (offset < 0)
  88. throw new StringIndexOutOfBoundsException(offset);
  89. if (offset > bytes.length - length)
  90. throw new StringIndexOutOfBoundsException(offset + length);
  91. }
  92.  
  93. public String(byte bytes[], int offset, int length, String charsetName)
  94. throws UnsupportedEncodingException {
  95. if (charsetName == null)
  96. throw new NullPointerException("charsetName");
  97. checkBounds(bytes, offset, length);
  98. this.value = StringCoding.decode(charsetName, bytes, offset, length);
  99. }
  100.  
  101. public String(byte bytes[], int offset, int length, Charset charset) {
  102. if (charset == null)
  103. throw new NullPointerException("charset");
  104. checkBounds(bytes, offset, length);
  105. this.value = StringCoding.decode(charset, bytes, offset, length);
  106. }
  107.  
  108. public String(byte bytes[], String charsetName)
  109. throws UnsupportedEncodingException {
  110. this(bytes, 0, bytes.length, charsetName);
  111. }
  112.  
  113. public String(byte bytes[], Charset charset) {
  114. this(bytes, 0, bytes.length, charset);
  115. }
  116.  
  117. public String(byte bytes[], int offset, int length) {
  118. checkBounds(bytes, offset, length);
  119. this.value = StringCoding.decode(bytes, offset, length);
  120. }
  121.  
  122. public String(byte bytes[]) {
  123. this(bytes, 0, bytes.length);
  124. }
  125.  
  126. public String(StringBuffer buffer) {
  127. synchronized(buffer) {
  128. this.value = Arrays.copyOf(buffer.getValue(), buffer.length());
  129. }
  130. }
  131.  
  132. public String(StringBuilder builder) {
  133. this.value = Arrays.copyOf(builder.getValue(), builder.length());
  134. }
  135.  
  136. String(char[] value, boolean share) {
  137. // assert share : "unshared not supported";
  138. this.value = value;
  139. }

5、String类常用方法

  1. // 得到字符串长度,即字符数组value的大小
  2. public int length() {
  3. return value.length;
  4. }
  5.  
  6. // 判断字符串是否为空
  7. public boolean isEmpty() {
  8. return value.length == 0;
  9. }
  10.  
  11. // 得到索引位置的字符
  12. public char charAt(int index) {
  13. if ((index < 0) || (index >= value.length)) {
  14. throw new StringIndexOutOfBoundsException(index);
  15. }
  16. return value[index];
  17. }
  18.  
  19. // 判断两个字符串是否相等,equals是Object类的方法,String覆写了该方法
  20. public boolean equals(Object anObject) {
  21. if (this == anObject) {
  22. return true;
  23. }
  24. if (anObject instanceof String) {
  25. String anotherString = (String)anObject;
  26. int n = value.length;
  27. if (n == anotherString.value.length) {
  28. char v1[] = value;
  29. char v2[] = anotherString.value;
  30. int i = 0;
  31. while (n-- != 0) {
  32. if (v1[i] != v2[i])
  33. return false;
  34. i++;
  35. }
  36. return true;
  37. }
  38. }
  39. return false;
  40. }
  41.  
  42. // 字符串比较操作,compareTo是Object类的方法,String覆写了该方法
  43. public int compareTo(String anotherString) {
  44. int len1 = value.length;
  45. int len2 = anotherString.value.length;
  46. int lim = Math.min(len1, len2);
  47. char v1[] = value;
  48. char v2[] = anotherString.value;
  49.  
  50. int k = 0;
  51. while (k < lim) {
  52. char c1 = v1[k];
  53. char c2 = v2[k];
  54. if (c1 != c2) {
  55. return c1 - c2;
  56. }
  57. k++;
  58. }
  59. return len1 - len2;
  60. }
  61.  
  62. // hashCode是Object类的方法,String类覆写了该方法
  63. public int hashCode() {
  64. int h = hash;
  65. if (h == 0 && value.length > 0) {
  66. char val[] = value;
  67.  
  68. for (int i = 0; i < value.length; i++) {
  69. h = 31 * h + val[i];
  70. }
  71. hash = h;
  72. }
  73. return h;
  74. }
  75.  
  76. // 判断字符串是否以另一个字符串开头
  77. public boolean startsWith(String prefix, int toffset) {
  78. char ta[] = value;
  79. int to = toffset;
  80. char pa[] = prefix.value;
  81. int po = 0;
  82. int pc = prefix.value.length;
  83. // Note: toffset might be near -1>>>1.
  84. if ((toffset < 0) || (toffset > value.length - pc)) {
  85. return false;
  86. }
  87. while (--pc >= 0) {
  88. if (ta[to++] != pa[po++]) {
  89. return false;
  90. }
  91. }
  92. return true;
  93. }
  94. // 重载上一个方法
  95. public boolean startsWith(String prefix) {
  96. return startsWith(prefix, 0);
  97. }
  98. // 判断字符串是否以另一个字符串结尾
  99. public boolean endsWith(String suffix) {
  100. return startsWith(suffix, value.length - suffix.value.length);
  101. }
  102.  
  103. // 得到子字符串
  104. public String substring(int beginIndex) {
  105. if (beginIndex < 0) {
  106. throw new StringIndexOutOfBoundsException(beginIndex);
  107. }
  108. int subLen = value.length - beginIndex;
  109. if (subLen < 0) {
  110. throw new StringIndexOutOfBoundsException(subLen);
  111. }
  112. return (beginIndex == 0) ? this : new String(value, beginIndex, subLen);
  113. }
  114. // 上个方法的重载
  115. public String substring(int beginIndex, int endIndex) {
  116. if (beginIndex < 0) {
  117. throw new StringIndexOutOfBoundsException(beginIndex);
  118. }
  119. if (endIndex > value.length) {
  120. throw new StringIndexOutOfBoundsException(endIndex);
  121. }
  122. int subLen = endIndex - beginIndex;
  123. if (subLen < 0) {
  124. throw new StringIndexOutOfBoundsException(subLen);
  125. }
  126. return ((beginIndex == 0) && (endIndex == value.length)) ? this
  127. : new String(value, beginIndex, subLen);
  128. }
  129.  
  130. // 判断字符串是否包含某个字符串
  131. public boolean contains(CharSequence s) {
  132. return indexOf(s.toString()) > -1;
  133. }
  134.  
  135. // 将其他类型数据转换为字符串的操作
  136. public static String valueOf(Object obj) {
  137. return (obj == null) ? "null" : obj.toString();
  138. }
  139. public static String valueOf(char data[]) {
  140. return new String(data);
  141. }
  142. public static String valueOf(char data[], int offset, int count) {
  143. return new String(data, offset, count);
  144. }
  145. public static String copyValueOf(char data[], int offset, int count) {
  146. return new String(data, offset, count);
  147. }
  148. public static String copyValueOf(char data[]) {
  149. return new String(data);
  150. }
  151. public static String valueOf(boolean b) {
  152. return b ? "true" : "false";
  153. }
  154. public static String valueOf(char c) {
  155. char data[] = {c};
  156. return new String(data, true);
  157. }
  158. public static String valueOf(int i) {
  159. return Integer.toString(i);
  160. }
  161. public static String valueOf(long l) {
  162. return Long.toString(l);
  163. }
  164. public static String valueOf(float f) {
  165. return Float.toString(f);
  166. }
  167. public static String valueOf(double d) {
  168. return Double.toString(d);
  169. }
  170.  
  171. // 本地方法,将字符串对象放入字符串常量池
  172. public native String intern();

String类源码分析的更多相关文章

  1. String 类源码分析

    String 源码分析 String 类代表字符序列,Java 中所有的字符串字面量都作为此类的实例. String 对象是不可变的,它们的值在创建之后就不能改变,因此 String 是线程安全的. ...

  2. String类源码分析(JDK1.7)

    以下学习根据JDK1.7String类源代码做注释 public final class String implements java.io.Serializable, Comparable<S ...

  3. List 接口以及实现类和相关类源码分析

    List 接口以及实现类和相关类源码分析 List接口分析 接口描述 用户可以对列表进行随机的读取(get),插入(add),删除(remove),修改(set),也可批量增加(addAll),删除( ...

  4. Cocos2d-X3.0 刨根问底(六)----- 调度器Scheduler类源码分析

    上一章,我们分析Node类的源码,在Node类里面耦合了一个 Scheduler 类的对象,这章我们就来剖析Cocos2d-x的调度器 Scheduler 类的源码,从源码中去了解它的实现与应用方法. ...

  5. Java Properties类源码分析

    一.Properties类介绍 java.util.Properties继承自java.util.Hashtable,从jdk1.1版本开始,Properties的实现基本上就没有什么大的变动.从ht ...

  6. java中List接口的实现类 ArrayList,LinkedList,Vector 的区别 list实现类源码分析

    java面试中经常被问到list常用的类以及内部实现机制,平时开发也经常用到list集合类,因此做一个源码级别的分析和比较之间的差异. 首先看一下List接口的的继承关系: list接口继承Colle ...

  7. Java并发编程笔记之Unsafe类和LockSupport类源码分析

    一.Unsafe类的源码分析 JDK的rt.jar包中的Unsafe类提供了硬件级别的原子操作,Unsafe里面的方法都是native方法,通过使用JNI的方式来访问本地C++实现库. rt.jar ...

  8. Cocos2d-X3.0 刨根问底(三)----- Director类源码分析

    上一章我们完整的跟了一遍HelloWorld的源码,了解了Cocos2d-x的启动流程.其中Director这个类贯穿了整个Application程序,这章随小鱼一起把这个类分析透彻. 小鱼的阅读源码 ...

  9. java.lang.String 类源码解读

    String类定义实现了java.io.Serializable, Comparable<String>, CharSequence 三个接口:并且为final修饰. public fin ...

随机推荐

  1. bat批处理命令及解释

    相关原文链接 一.批处理概念 批处理文件:包含DOS命令的可编辑可执行文件 批处理:可以对某一对象批量操作的文件 二.批处理命令简介 命令1~10 1 echo 和 @ 回显命令 @ #关闭单行回显 ...

  2. Java包装类,以及Integer与int之间的比较

    一.Java的基本类型 Java语言中提供了八种基本类型,包括六种数字类型(四个整数型,两个浮点型),一种字符类型,还有一种布尔型. 整数型,包括byte.short.int.long,默认初始值是0 ...

  3. [bzoj5295]染色

    将这张图化简,不断删掉度为1的点(类似于拓扑排序),构成了一张由环组成的图考虑一个连通块中,设点数为n,边数为m(已经删掉了度为1的点),那么一共只有三种情况:1.一个环($n=m$),一定为YES2 ...

  4. Centos8上安装Nginx

    一.Nginx下载 官网:http://nginx.org/ 选择稳定版下载:直接右键复制下载地址即可 命令: wget http://nginx.org/download/nginx-1.20.2. ...

  5. nginx得请求转发代码-将请求转发到网关

    首先:本地主机host更改成 192.168.111.1 gulimail.com 这样一访问网址就能映射到本地. 然后修改nginx得conf worker_processes 1; events ...

  6. C/C++ Qt Tree与Tab组件实现分页菜单

    虽然TreeWidget组件可以实现多节点的增删改查,但多节点操作显然很麻烦,在一般的应用场景中基本上只使用一层结构即可解决大部分开发问题,TreeWidget组件通常可配合TabWidget组件,实 ...

  7. vue3 高阶 API 大汇总,强到离谱

    高阶函数是什么呢? 高阶函数英文名叫:Higher Order function ,一个函数可以接收一个或多个函数作为输入,或者输出一个函数,至少满足上述条件之一的函数,叫做高阶函数. 前言 本篇内容 ...

  8. Codeforces 436D - Pudding Monsters(dp)

    Codeforces 题目传送门 & 洛谷题目传送门 u1s1 这题数据范围有点迷惑啊--乍一看 \(\mathcal O(nm)\) 过不去,还以为是正解是 \(\mathcal O(n+m ...

  9. [Linux]非root的R环境被conda破坏后如何恢复?

    记录说明 这篇文章本来是用来记录Linux非root环境下安装PMCMRplus包折腾过程,但后来试过了各种方法安装不上这个R包后,我换上了Miniconda来安装.经前人提醒,一开始安装Minico ...

  10. 利用vcftools比较两个vcf文件

    因为最近有一项工作是比较填充准确性的,中间有用到vcftools比较两个vcf文件. 使用命令也很简单: 1 vcftools --vcf file1.snp.vcf --diff file2.snp ...