java基礎源碼 (1)--String類

來源:互聯網
上載者:User

標籤:訪問   多語言   統一   class   sem   pre   .so   create   result   

這個是String類上面的注釋,我用Google翻譯翻譯的,雖然有點文法上的問題,但是大概都可以翻譯出來

 

/** * The {@code String} class represents character strings. All * string literals in Java programs, such as {@code "abc"}, are * implemented as instances of this class. * <p> * Strings are constant; their values cannot be changed after they * are created. String buffers support mutable strings. * Because String objects are immutable they can be shared. For example:
{@code String}類表示字串。所有java程式中的字元文字,例如{@code "abc"},是作為此類的執行個體實現。
The {@code String} class represents character strings. All * string literals in Java programs, such as {@code "abc"}, are * implemented as instances of this class. * <p> * Strings are constant; their values cannot be changed after they * are created. String buffers support mutable strings. * Because String objects are immutable they can be shared. For example:
字串是不變的;他們的價值觀無法改變已建立(這句話的意思我個人覺得應該是:已經建立就無法修改)。
字串緩衝區支援可變字串。因為String對象是不可變的。所以可以共用他們。
* The class {@code String} includes methods for examining * individual characters of the sequence, for comparing strings, for * searching strings, for extracting substrings, and for creating a * copy of a string with all characters translated to uppercase or to * lowercase. Case mapping is based on the Unicode Standard version * specified by the {@link java.lang.Character Character} class.
類{@code String}包括檢查方法
*序列的各個字元,用於比較字串
*搜尋字串,提取子字串,以及建立
*一個字串的副本,所有字元都翻譯成大寫或
*小寫。案例映射基於Unicode標準版本
*由{@link java.lang.Character Character}類指定。
介紹了String類裡麵包括的一些內容(檢查方法,序列的各個字元,比較字串,搜尋字串,
* 提取子字串,建立一個字串的副本,所有字元翻譯大,小寫,)
 *  * The Java language provides special support for the string * concatenation operator (&nbsp;+&nbsp;), and for conversion of * other objects to strings. String concatenation is implemented * through the {@code StringBuilder}(or {@code StringBuffer}) * class and its {@code append} method. * String的轉換是通過由java中的所有類繼承的Object裡面定義的toString()方法實現 * String conversions are implemented through the method * {@code toString}, defined by {@code Object} and * inherited by all classes in Java. For additional information on * string concatenation and conversion, see Gosling, Joy, and Steele, *  * <i>The Java Language Specification</i>. *   * Java語言為字串提供特殊支援 *串連運算子(&nbsp; +&nbsp;),用於轉換 *其他對象到字串。字串串連已實現 *通過{@code StringBuilder}(或{@code StringBuffer}) *類及其{@code append}方法。 *字串轉換通過該方法實現 * {@code toString},由{@code Object}定義 *由Java中的所有類繼承。有關其他資訊 *字串串連和轉換,請參閱Gosling,Joy和Steele, * <i> Java語言規範</ i>。&nbsp;代表不換行空格的意思
 * <p> Unless otherwise noted, passing a <tt>null</tt> argument to a constructor * or method in this class will cause a {@link NullPointerException} to be * thrown.

除非另有說明,否者將NULL參數傳遞給建構函式或此類中的方法將導致NullPointerException(null 指標異常)

現在開始學習:

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

首先他是final類型的,是不可修改,然後實現了Serializable,Comparable,CharSequence這個3個介面

Serializable:這個是Serializablelei類源碼上的注釋

* Serializability of a class is enabled by the class implementing the * java.io.Serializable interface. Classes that do not implement this * interface will not have any of their state serialized or * deserialized.  All subtypes of a serializable class are themselves * serializable.  The serialization interface has no methods or fields * and serves only to identify the semantics of being serializable. 

 翻譯的大概意思是,類的序列化是由java.io.Serializable介面的類啟用。不實現此介面的類將不會使用任何狀態序列化或還原序列化,可序列化的所有子類型都是可序列化的,序列化介面沒有方法或欄位,僅用於標識可串列話的語義。

Comparable:

  JDK1.8文檔的意思是該介面對實現它的每個類的對象加強一個整體排序。這個排序被稱為類的自然排序,類的compareTo方法被稱為自然比較方法。

  例子(借鑒別人的)

public class Persion implements Comparable<Persion> {     String name;     int age;    public Persion(String name, int age) {        this.name = name;        this.age = age;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }    public int compareTo(Persion o) {        /*        * 比較此對象與指定對象的順序,如果對象小於        * 等於或大於指定對象,則分別返回整數,零,或負數        * */        return compare(this.age,o.age);    }    public static int compare(long age1,long age2){        return (age1>age2?1:(age1==age2?0:-1));    }    @Override    public String toString() {        return "Persion{" +                "name=‘" + name + ‘\‘‘ +                ", age=" + age +                ‘}‘;    }}   public static void main(String[] args){        Persion persion2 = new Persion("世界", 23);        Persion persion1 = new Persion("科紀", 21);        Persion persion3 = new Persion("偉克", 25);        List<Persion>persionList=new ArrayList<Persion>();        persionList.add(persion2);        persionList.add(persion1);        persionList.add(persion3);        for (Persion person1 : persionList) {            System.out.println(person1.toString());        }        System.out.println("排序之前........................");        Collections.sort(persionList);        for (Persion person1 : persionList) {            System.out.println(person1.toString());        }        System.out.println("排序之後........................");    }

 CharSequence:

   

/** * 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是char值的可讀序列,該介面提供統一的,唯讀訪問許多不同類型的char序列,char值代表基本多語言平面(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>
* * @author Mike McCloskey * @since 1.4 * @spec JSR-51 */
此介面不會完善equals和hashCode方法的一般合約。因此,比較兩個對象實現CharSequence其結果是,一般情況下,不確定的,每個對象可以由不同的類實現,並且不能保證每個類都能夠測試其執行個體以與另一個類相同,因此,使用任意的CharSequence
執行個體作為集合中的元素或映射中的鍵是不合適的。

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.
*返回此字元序列的長度,這個數字序列是長度為16位char的

* @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.
*返回char指定索引處的值。索引範圍從0到length()-1.序列的第一個char值在索引為零,下一個索引為1,
以此類推,就像數組索引一樣
* <p>If the <code>char</code> value specified by the index is a
如果char由索引指定的值是surrogate,則返回所述替代值
* <a href="{@docRoot}/java/lang/Character.html#unicode">surrogate</a>, the surrogate
* value is returned.
*
* @param index the index of the <code>char</code> value to be returned
*參數 index,要返回的char值的索引
* @return the specified <code>char</code> value
*返回 指定值為char
* @throws IndexOutOfBoundsException
* if the <tt>index</tt> argument is negative or not less than
* <tt>length()</tt>
  異常 IndexOutOfBuoundsException 如果index參數為負數或不低於length()
*/
char charAt(int index);
 * Returns a <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.
* 返回一個CharSequence,這是這個序列的一個子序列。子char以指定索引的char值開始,以索引end-1的char
值結束,返回序列的長度(chars)為end-start,因此如果start==end則返回一個空序列
* @param start the start index, inclusive
  參數 start 包含起始索引
* @param end the end index, exclusive
* 參數 end 結束索引,獨佔
* @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>
*/異常 indexOutOfBoundsException 如果start或end為負數,如果end大於length()
      ,或者如果start大於end
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.
*以與此順序相同的順序返回包含此序列中的字元的字串,字串的長度將是此序列的長度
* @return a string consisting of exactly this sequence of characters
*/ 返回 一個由這個字元序列組成的字串
public String toString();
 * Returns a stream of {@code int} zero-extending the {@code char} values
* from this sequence. Any char which maps to a <a
* href="{@docRoot}/java/lang/Character.html#unicode">surrogate code
* point</a> is passed through uninterpreted.
* 返回Int的流,從這個序列零擴充char值,映射到surrogate code point的任何字元通過未
解釋的方式傳遞
* <p>If the sequence is mutated while the stream is being read, the
* result is undefined.
*如果序列在流被讀取時被突變,則結果是未定義的
 * @return an IntStream of char values from this sequence
  返回 這個序列中的char值的intStream
* @since 1.8
*/
public default IntStream chars() {

java基礎源碼 (1)--String類

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.