/** * The {@code String} class represents character strings. All * string literals in Java programs, such as {@code "abc"}, are * implemented as instances of this class. * 這個String類代表字符串。java編程中的所有字符串常量。 * 比如說:"abc"就是這個String類的實例 * <p> * Strings are constant; their values cannot be changed after they * are created. * 字符串是常量,他們一旦被創(chuàng)建后,他們的值是不能被修改。(重點) * String buffers support mutable strings. * String緩存池支持可變的字符串, * Because String objects are immutable they can be shared. For example: * 因為String字符串不可變,但他們可以被共享。比如: * <blockquote><pre> * String str = "abc"; * </pre></blockquote><p> * is equivalent to: * <blockquote><pre> * char data[] = {'a', 'b', 'c'}; * String str = new String(data); * </pre></blockquote><p> * Here are some more examples of how strings can be used: * String使用案例 * System.out.println("abc"); * String cde = "cde"; * System.out.println("abc" + cde); * String c = "abc".substring(2,3); * String d = cde.substring(1, 2); * <p> * 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. * 這個String類包含了一些測評單個字符序列的方法,比如字符串比較,查找字符串, * 提取字符串,和拷貝一個字符串的大小寫副本。 * 大小寫映射的是基于Character類支持的Unicode的字符集標(biāo)準(zhǔn)版本。 * <p> * The Java language provides special support for the string * concatenation operator ( + ), and for conversion of * other objects to strings. * java語言提供了對字符串的特殊支持,如:可以通過"+"號來進(jìn)行字符串的拼接操作, * 為其他類提供了與字符串轉(zhuǎn)換的操作 * String concatenation is implemented * through the {@code StringBuilder}(or {@code StringBuffer}) * class and its {@code append} method. * 字符串的+號拼接操作是通過StringBuilder或者StringBuffer類的append()方法 * 來實現(xiàn)的 * String conversions are implemented through the method * {@code toString}, defined by {@code Object} and * inherited by all classes in Java. * 對象與字符串的轉(zhuǎn)換操作是通過所有類的父類Object中定義的toString()方法來實現(xiàn)的 * For additional information on * string concatenation and conversion, see Gosling, Joy, and Steele, * <i>The Java Language Specification</i>. * * <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給String的構(gòu)造方法或者put方法,會報空指針異常的 * <p>A {@code String} represents a string in the UTF-16 format * in which <em>supplementary characters</em> are represented by <em>surrogate * pairs</em> (see the section <a href="Character.html#unicode">Unicode * Character Representations</a> in the {@code Character} class for * more information). * 一個String 對象代表了一個UTF-16編碼語法組成的字符串 * Index values refer to {@code char} code units, so a supplementary * character uses two positions in a {@code String}. * <p>The {@code String} class provides methods for dealing with * Unicode code points (i.e., characters), in addition to those for * dealing with Unicode code units (i.e., {@code char} values). * 索引值指向字符碼單元,所以一個字符在一個字符串中使用兩個位置, * String 類提供了一些方法區(qū)處理單個Unicode編碼,除了那些處理Unicode代碼單元。 * @since JDK1.0 */
/** The value is used for character storage. */ // 來用存儲String內(nèi)容的 privatefinalchar value[]; // 存儲字符串哈希值,默認(rèn)值為0 privateint hash; // Default to 0 // 實現(xiàn)序列化的標(biāo)識 privatestaticfinallong serialVersionUID = -6849794470754667710L;
char value[]被final修飾,說明value[]數(shù)組是不可變的。
構(gòu)造方法
/** * Initializes a newly created {@code String} object so that it represents * an empty character sequence. Note that use of this constructor is * unnecessary since Strings are immutable. * 初始化新創(chuàng)建的String對象,時期表示空字符串序列。 * 注意:這個構(gòu)造方法的用法是沒必要的,因為字符串是不可變的 */ publicString(){ this.value = "".value; }
無參構(gòu)造方法中是將一個空字符串的value值賦給當(dāng)前value。
/** * Initializes a newly created {@code String} object so that it represents * the same sequence of characters as the argument; in other words, the * newly created string is a copy of the argument string. Unless an * explicit copy of {@code original} is needed, use of this constructor is * unnecessary since Strings are immutable. * 初始化創(chuàng)建的String對象,時期表示與參數(shù)相同的字符串序列。 * 換句話說:新創(chuàng)建的字符串是參數(shù)自粗糙的副本。 * 除非,如果需要original的顯示副本,否則也是沒有必要使用此構(gòu)造方法的 * 因為字符串是不可變的 * @param original * A {@code String} */ publicString(String original){ this.value = original.value; this.hash = original.hash; } //案例: String str=new String("abc");
/** * Allocates a new {@code String} so that it represents the sequence of * characters currently contained in the character array argument. The * contents of the character array are copied; subsequent modification of * the character array does not affect the newly created string. * 分配一個新的{@code String},以便它表示字符數(shù)組參數(shù)中當(dāng)前包含的字符。這個 * 復(fù)制字符數(shù)組的內(nèi)容;隨后修改字符數(shù)組不影響新創(chuàng)建的字符串。 * @param value * The initial value of the string */ publicString(char value[]){ //注:將傳過來的char數(shù)組copy到value數(shù)組里 this.value = Arrays.copyOf(value, value.length); } //Arrays類中的copyOf方法 publicstaticchar[] copyOf(char[] original, int newLength) { //創(chuàng)建一個新的char數(shù)組 char[] copy = newchar[newLength]; //把original數(shù)組中內(nèi)容拷貝到新建的char數(shù)組中 System.arraycopy(original, 0, copy, 0, Math.min(original.length, newLength)); //返回新建的char數(shù)組 return copy; }
substring方法在工作使用的也是相當(dāng)?shù)亩?,作用就是截取一段字符串?/p>public String substring(int beginIndex){ if (beginIndex < 0) { thrownew StringIndexOutOfBoundsException(beginIndex); } int subLen = value.length - beginIndex; if (subLen < 0) { thrownew StringIndexOutOfBoundsException(subLen); } //如果beginIndex==0,返回的是當(dāng)前對象, //否則這里是new的一個新對象,其實String中的很多函數(shù)都是這樣的操作 return (beginIndex == 0) ? this : new String(value, beginIndex, subLen); }
intern()方法
intern()方法是native修飾的方法,表示該方法為本地方法。
/* * When the intern method is invoked, if the pool already contains a * string equal to this {@code String} object as determined by * the {@link #equals(Object)} method, then the string from the pool is * returned. Otherwise, this {@code String} object is added to the * pool and a reference to this {@code String} object is returned. */ publicnative String intern();