java基础源码 --String类
Posted 李科技
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java基础源码 --String类相关的知识,希望对你有一定的参考价值。
这个是String类上面的注释,我用谷歌翻译翻译的,虽然有点语法上的问题,但是大概都可以翻译出来
/** * 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 ( + ), 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(空指针异常)
现在开始学习:
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基础源码 --String类的主要内容,如果未能解决你的问题,请参考以下文章