JDK源码阅读(1.8) —— String

String类有两个私有的变量,字符数组value,和整型变量hash(默认为0)。

1     /** The value is used for character storage. */
2     private final char value[];
3 
4     /** Cache the hash code for the string */
5     private int hash; // Default to 0

String的构造器会对value进行赋值,有时也对hash进行赋值。

 1 /**
 2      * Initializes a newly created {@code String} object so that it represents
 3      * an empty character sequence.  Note that use of this constructor is
 4      * unnecessary since Strings are immutable.
 5      */
 6     public String() {
 7         this.value = "".value;
 8     }
 9 
10     /**
11      * Initializes a newly created {@code String} object so that it represents
12      * the same sequence of characters as the argument; in other words, the
13      * newly created string is a copy of the argument string. Unless an
14      * explicit copy of {@code original} is needed, use of this constructor is
15      * unnecessary since Strings are immutable.
16      *
17      * @param  original
18      *         A {@code String}
19      */
20     public String(String original) {
21         this.value = original.value;
22         this.hash = original.hash;
23     }
24 
25     /**
26      * Allocates a new {@code String} so that it represents the sequence of
27      * characters currently contained in the character array argument. The
28      * contents of the character array are copied; subsequent modification of
29      * the character array does not affect the newly created string.
30      *
31      * @param  value
32      *         The initial value of the string
33      */
34     public String(char value[]) {
35         this.value = Arrays.copyOf(value, value.length);
36     }
View Code

下面对一些常用方法进行分析。

length()

 1  /**
 2      * Returns the length of this string.
 3      * The length is equal to the number of <a href="Character.html#unicode">Unicode
 4      * code units</a> in the string.
 5      *
 6      * @return  the length of the sequence of characters represented by this
 7      *          object.
 8      */
 9     public int length() {
10         return value.length;
11     }
View Code

 length()通过value数组的长度表示字符串的长度。

 

isEmpty()

 1 /**
 2      * Returns {@code true} if, and only if, {@link #length()} is {@code 0}.
 3      *
 4      * @return {@code true} if {@link #length()} is {@code 0}, otherwise
 5      * {@code false}
 6      *
 7      * @since 1.6
 8      */
 9     public boolean isEmpty() {
10         return value.length == 0;
11     }
View Code

通过value.length == 0,判断字符串是否为空

 

charAt(int index)

1  public char charAt(int index) {
2         if ((index < 0) || (index >= value.length)) {
3             throw new StringIndexOutOfBoundsException(index);
4         }
5         return value[index];
6     }
View Code

通过数组的方式去获取字符串对应位置的字符,基0。

 

equals(Object anObject)

 1 public boolean equals(Object anObject) {
 2         if (this == anObject) {
 3             return true;
 4         }
 5         if (anObject instanceof String) {
 6             String anotherString = (String)anObject;
 7             int n = value.length;
 8             if (n == anotherString.value.length) {
 9                 char v1[] = value;
10                 char v2[] = anotherString.value;
11                 int i = 0;
12                 while (n-- != 0) {
13                     if (v1[i] != v2[i])
14                         return false;
15                     i++;
16                 }
17                 return true;
18             }
19         }
20         return false;
21     }
View Code

equals(Object obj)重写了Object的equals(Object obj).会先比较引用(String属于引用数据类型,不是基本数据类型)。当引用不同时,会进行 instanceof 判断

(如 if( str instanceof String)),判断该对象是否是字符串或其子类的一个实例。如果是,再比较value数组长度,相同的话,通过while循环,逐位比较字符是否相同,相同返回true,反之返回false.

Java 中的instanceof 运算符是用来在运行时指出对象是否是特定类的一个实例。instanceof通过返回一个布尔值来指出,这个对象是否是这个特定类或者是它的子类的一个实例。 

 

hashCode()、

 

indexOf(int ch)、substring(int beginIndex)、concat(String str)、

 replace(char oldChar, char newChar) 、contains(CharSequence s)、

 

转载于:https://www.cnblogs.com/DamonGeng/p/10480239.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值