Java String类 源码注释阅读

来源:互联网 发布:网络金融考试 编辑:程序博客网 时间:2024/05/16 23:43

直接打开String 源码文件(jdk 1.8.0_112):

1. 首先来看引入的包:

package java.lang;import java.io.ObjectStreamField;import java.io.UnsupportedEncodingException;import java.nio.charset.Charset;import java.util.ArrayList;import java.util.Arrays;import java.util.Comparator;import java.util.Formatter;import java.util.Locale;import java.util.Objects;import java.util.StringJoiner;import java.util.regex.Matcher;import java.util.regex.Pattern;import java.util.regex.PatternSyntaxException;

由引入的包可以大致看到,这个类引用了io包的ObjectStreamField; 关于这个类,我们打开,看描述

package java.io;import java.lang.reflect.Field;import sun.reflect.CallerSensitive;import sun.reflect.Reflection;import sun.reflect.misc.ReflectUtil;/** * A description of a Serializable field from a Serializable class.  An array * of ObjectStreamFields is used to declare the Serializable fields of a class. * * @author      Mike Warres * @author      Roger Riggs * @see ObjectStreamClass * @since 1.2 */public class ObjectStreamField    implements Comparable<Object>

根据释义,了解到这个类是从可以序列化类序列化字段的描述。再回头 来看String, 剩下引入的是util包一些内容。

2. 来看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.

这个类就是代表了字符串。Java程序中例如”abc”字符串都是这个类的实例。

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: * <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: * <blockquote><pre> *     System.out.println("abc"); *     String cde = "cde"; *     System.out.println("abc" + cde); *     String c = "abc".substring(2,3); *     String d = cde.substring(1, 2); * </pre></blockquote>

字符串是常量,它们的创建后值可以改变。字符串缓冲 可以支持可变字符串。因为字符串对象不可变,所以可以共享。
例如:
String str = “abc”;
相当于: char data[] = {‘a’, ‘b’, ‘c’};
String str = new String(data);
字符串还可以这样用:
System.out.println(“abc”);
String cde = “cde”;
System.out.println(“abc” + cde);
String c = “abc”.substring(2,3);
String d = cde.substring(1, 2);

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.

这个类包含了检查序列单个字符的方法、比较字符串、检索字符串、字符串截取、复制字符串为大写或小写。这个类映射基于Unicode编码的Character类。

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 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语言提供各种支持对字符串连接操作 或把其他对象转化成字符串。字符串连接操作通过StringBuilder 或StringBuffer类及其方法实现。转化对象可以通过各个对象自己的toString方法实现。

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. * * <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). * 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). * 

接下来 看看这个类伟大的作者们:

 @author  Lee Boynton * @author  Arthur van Hoff * @author  Martin Buchholz * @author  Ulf Zibis * @since   JDK1.0
原创粉丝点击