1. 程式人生 > >JDK8中CharSequence介面原始碼分析

JDK8中CharSequence介面原始碼分析

本來是要看String類的,結果看了兩個String類的實現介面,畢竟沒有分析過嘛,所以還要看的,這回明天終於可以看String類裡面的程式碼了^_^

這個介面內容還是蠻少的,下面貼出分析。

package sourcecode.analysis;

/**
 * Created by caoxiaohong on 17/11/18 23:18.
 */

import java.lang.*;

/**
 * A <tt>CharSequence</tt> is a readable sequence of <code>char</code> values. This
 * interface provides uniform, read-only access to many different kinds of
 * <code>char</code> sequences.
 * A <code>char</code> value represents a character in the <i>Basic
 * Multilingual Plane (BMP)</i> or a surrogate. Refer to <a
 * href="Character.html#unicode">Unicode Character Representation</a> for details.
 *
 * CharSequence就是一個可讀的字元序列.對於不同型別的字元序列,這一介面都以統一且只讀的方式去讀取.
 * 一個字元值代表了BMP中的一個字元或者一個代理.(BMP是什麼?BMP包含了現代大多數語言的字符集)
 *
 * <p> This interface does not refine the general contracts of the {@link
 * java.lang.Object#equals(java.lang.Object) equals} and {@link
 * java.lang.Object#hashCode() hashCode} methods.  The result of comparing two
 * objects that implement <tt>CharSequence</tt> is therefore, in general,
 * undefined.  Each object may be implemented by a different class, and there
 * is no guarantee that each class will be capable of testing its instances
 * for equality with those of the other.  It is therefore inappropriate to use
 * arbitrary <tt>CharSequence</tt> instances as elements in a set or as keys in
 * a map. </p>
 *
 * 這一介面,並沒有提煉出Object類定義的equals()方法和hashCode()方法的通用規範(但是,像其他的介面,比如Map就有equals()方法和
 * hashCode()方法,再比如,雖然List介面沒有給出這兩個方法,但是抽象類Abstract重新定義了這兩個方法).因此,對於兩個不僅實現了
 * CharSequence介面的的物件(可能還繼承了其他的類),進行比較時,通常,其結果也是未定義的(因為對於繼承了Object的類來說,根據具體的實現,
 * 比較時,是可以有兩種選擇的,要麼比較地址,使用兩個等號==,要麼比較內容,使用equals.但是這個介面並沒有定義equals方法,也沒有用到通常我們
 * 做兩個物件比較時,用到的equals方法和hashCode方法之間的關係,也就是你重寫equals方法時,必須重寫hashCode方法,這一點我之前在Object
 * 原始碼的分析中說過了).每個物件都可以由不同的類來實現,因此,我們無法保證每個類都有能和其他類例項測試等價性的能力.因此,使用任意的
 * CharSequence例項作為set集合的元素或者map中的key,這種做法都是不合適的(為什麼不合適呢?因為CharSequence例項是沒有equals方法和
 * hashCode方法的,所以對應的例項的比較就取決於其繼承的類或者其他實現的介面,那麼兩個被比較的類如果因為繼承的類或者實現的介面
 * (並且對應繼承的類或者介面都對equals和hashCode給出了自己的定義)不同,所以一旦被比較,出現什麼結果都是不可控的,故不適合.).
 *
 * @author Mike McCloskey
 * @since 1.4
 * @spec JSR-51
 */

public interface CharSequence {
    /**
     * Returns the length of this character sequence.  The length is the number
     * of 16-bit <code>char</code>s in the sequence.</p>
     *
     * 返回字元序列的長度.
     * 長度是16bit的整數倍.(因為String類採用的是UTF-16編碼,一個字元佔用2個位元組長度)
     *
     * @return  the number of <code>char</code>s in this sequence
     */
    int length();

    /**
     * Returns the <code>char</code> value at the specified index.  An index ranges from zero
     * to <tt>length() - 1</tt>.  The first <code>char</code> value of the sequence is at
     * index zero, the next at index one, and so on, as for array
     * indexing. </p>
     *
     * 返回指定索引index位置處的字元.索引index的範圍是[0,length()-1].
     *
     * <p>If the <code>char</code> value specified by the index is a
     * <a href="{@docRoot}/java/lang/Character.html#unicode">surrogate</a>, the surrogate
     * value is returned.
     *
     * 如果指定索引位置處的字元值為代表(字元)(surrogate的出現原因:因為UTF-16採用2個位元組儲存一個字元,但是有的字元儲存只需要一個位元組,比如英文
     * 字元,那麼下一個位元組也不能繼續儲存其他的字元,而只能儲存一個代表字元,來佔用這一個位元組的位置,接下來的一個位元組處才能繼續儲存下一個字元),
     * 那麼返回的也會是這個代表(字元)值.
     *
     * @param   index   the index of the <code>char</code> value to be returned
     *
     * @return  the specified <code>char</code> value
     *
     * @throws  IndexOutOfBoundsException
     *          if the <tt>index</tt> argument is negative or not less than
     *          <tt>length()</tt>
     */
    char charAt(int index);

    /**
     * Returns a new <code>CharSequence</code> that is a subsequence of this sequence.
     * The subsequence starts with the <code>char</code> value at the specified index and
     * ends with the <code>char</code> value at index <tt>end - 1</tt>.  The length
     * (in <code>char</code>s) of the
     * returned sequence is <tt>end - start</tt>, so if <tt>start == end</tt>
     * then an empty sequence is returned. </p>
     *
     * 返回一個新的字元序列,這個序列是原字元序列的子序列.
     * 子序列的擷取開始位置為:原序列中start的位置;
     *        擷取結束位置為:原序列中(end-1)的位置.
     * 因此子字元序列的長度為(end-start)
     * 所以,如果傳入引數start=end,則返回子序列為空序列.
     *
     * @param   start   the start index, inclusive
     * @param   end     the end index, exclusive
     *
     * @return  the specified subsequence
     *
     * @throws  IndexOutOfBoundsException
     *          if <tt>start</tt> or <tt>end</tt> are negative,
     *          if <tt>end</tt> is greater than <tt>length()</tt>,
     *          or if <tt>start</tt> is greater than <tt>end</tt>
     */
    CharSequence subSequence(int start, int end);

    /**
     * Returns a string containing the characters in this sequence in the same
     * order as this sequence.  The length of the string will be the length of
     * this sequence. </p>
     *
     * 返回字元序列的字串形式,字串中字元的順序和字元序列保持一致.字串的長度和字元序列一致.
     *
     * @return  a string consisting of exactly this sequence of characters
     */
    public java.lang.String toString();
}


相關推薦

JDK8CharSequence介面原始碼分析

本來是要看String類的,結果看了兩個String類的實現介面,畢竟沒有分析過嘛,所以還要看的,這回明天終於可以看String類裡面的程式碼了^_^ 這個介面內容還是蠻少的,下面貼出分析。 package sourcecode.analysis; /** * Cre

iterator介面原始碼分析(ArrayList的實現)

iterator是一個迭代器介面,它裡面主要有:boolean hasNext();E next();這兩個方法,第一個方法表示迭代器含有更多元素則返回true;否則返回false;第二個方法是返回迭代器的下一個元素;其中還有兩個實現方法: default void r

Golangheap包原始碼分析

heap的實現使用到了小根堆,下面先對堆做個簡單說明 1. 堆概念     堆是一種經過排序的完全二叉樹,其中任一非終端節點的資料值均不大於(或不小於)其左孩子和右孩子節點的值。   最大堆和最小堆是二叉堆的兩種形式。   最大堆:根結點的鍵值是所有堆結點鍵值中最大者。   最小

JAVA的集合原始碼分析一:ArrayList的內部實現原理

作為以java為語言開發的android開發者,集合幾乎天天都要打交道,無論是使用頻率最高的ArrayList還是HashSet,都頻繁的出現在平時的工作中。但是其中的原理之前卻一直沒深入探究,接下來記錄一下這次自己學習ArrayList原始碼的過程。 一.構造方法:

String類的compareTo原始碼分析(為什麼這樣啊~~!!)

    今天看了集合,在treeset中,要自定義排序,需要實現comparable介面(比較器排序),或者自己給出compareTo方法(自然排序),但是實現comparable介面,自己寫邏輯這個還好理解一點,但是對於這個compareTo我還是有的懵逼-->因為我

Android的RecyclerView原始碼分析

RecyclerView元件 Adapter 提供資料以及資料變更通知 LayoutManager 佈局管理,負責測繪、指定item位置、item回收、 ItemAnimator Item變更動畫 關鍵實現 1、ViewHolder複

java集合之Iterable介面原始碼分析

Iterable 原始碼分析 Iterable簡介 Iterable是從jdk1.5就存在的介面,其實我們經常用到它的功能,就是for-each,要想使用for-each,就必須實現此介面。 API簡

jqueryoffset()的原始碼分析

jQuery.fn.extend({ offset: function( options ) { if ( arguments.length ) { return options === undefined ? this : this.each(function( i ) {

Set介面原始碼分析-java8

1.toArray()和toArray(T[] a) 將set例項轉為String[]的方式如下: Set<String> x=new HashSet<String>; String[] y = x.toArray(new S

Netty的ChannelPipeline原始碼分析

ChannelPipeline在Netty中是用來處理請求的責任鏈,預設實現是DefaultChannelPipeline,其構造方法如下: 1 private final Channel channel; 2 private final ChannelFuture succeededFuture;

【朝花夕拾】Android自定義View篇之(六)Android事件分發機制()從原始碼分析事件分發邏輯及經常遇到的一些“詭異”現象

前言        轉載請註明,轉自【https://www.cnblogs.com/andy-songwei/p/11039252.html】謝謝!        在上一篇文章【【朝花夕拾】Android自定義View篇之(

JDK8的HashMap實現原理及原始碼分析

本篇所述原始碼基於JDK1.8.0_121 在寫上一篇線性表的文章的時候,筆者看的是Android原始碼中support24中的Java程式碼,當時發現這個ArrayList和LinkedList的原始碼和Java官方的沒有什麼區別,然而在閱讀HashMap原

javaList介面的實現類 ArrayList,LinkedList,Vector 的區別 list實現類原始碼分析

java面試中經常被問到list常用的類以及內部實現機制,平時開發也經常用到list集合類,因此做一個原始碼級別的分析和比較之間的差異。 首先看一下List介面的的繼承關係: list介面繼承Col

Flume NG原始碼分析(四)使用ExecSource從本地日誌檔案收集日誌

常見的日誌收集方式有兩種,一種是經由本地日誌檔案做媒介,非同步地傳送到遠端日誌倉庫,一種是基於RPC方式的同步日誌收集,直接傳送到遠端日誌倉庫。這篇講講Flume NG如何從本地日誌檔案中收集日誌。 ExecSource是用來執行本地shell命令,並把本地日誌檔案中的資料封裝成Event

Flume NG原始碼分析(三)使用Event介面表示資料流

Flume NG有4個主要的元件: Event表示在Flume各個Agent之間傳遞的資料流 Source表示從外部源接收Event資料流,然後傳遞給Channel Channel表示對從Source傳遞的Event資料流的臨時儲存 Sink表示從Channel中接收儲存的Event

List去重(資料為物件的情況)及String的equals()方法和hashCode()方法原始碼分析

面試中經常被問到的list如何去重,用來考察你對list資料結構,以及相關方法的掌握,體現你的java基礎學的是否牢固。 我們大家都知道,set集合的特點就是沒有重複的元素。如果集合中的資料型別是基本資料型別,可以直接將list集合轉換成set,就會自動去除重複的元素,這個就相對比較簡單。上一篇

java排序原始碼分析(JDK1.8)

List排序 在開發過程中常用的是jdk自帶的排序 Collections.sort(List<T> list, Comparator<? super T> c); 開啟原始碼如下: @SuppressWarnings({"unchecked",

Spring裡的aop實現方式和原始碼分析 java代理,靜態代理,動態代理以及spring aop代理方式,實現原理統一彙總

使用"橫切"技術,AOP把軟體系統分為兩個部分:核心關注點和橫切關注點。業務處理的主要流程是核心關注點,與之關係不大的部分是橫切關注點。橫切關注點的一個特點是,他們經常發生在核心關注點的多處,而各處基本相似,比如許可權認證、日誌、事務。AOP的作用在於分離系統中的各種關注點,將核心關注點和橫切關注點分離開來。

Java原始碼分析——Class類、ClassLoader類解析(二) 類的識別、Modifier類、TypeVariable、GenericDeclaration介面

    在類的載入與例項化的時候,如何識別類、介面、註解以及陣列是個值得思考的問題,不僅是這些常用的引用類,還包括類、介面等的public、private、defalut、static等修飾符,以及識別一個泛型類或者介面。   

laravel的where和orwhere的原始碼分析

一、背景       博主在寫sql的時候,遇到了要用orwhere的情況,關鍵這個orwhere的條件是一個數組,就是要用orwhereIn的方法來寫。。反正在這之前博主是一直不知道, 竟然還有orWhereIn的方法,反