/**
* 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类的实例
*
* Strings are constant; their values cannot be changed after they
* are created.
* 字符串是常量,他们一旦被创建后,他们的值是不能被修改。(重点)
* String buffers support mutable strings.
* String缓存池支持可变的字符串,
* Because String objects are immutable they can be shared. For example:
* 因为String字符串不可变,但他们可以被共享。比如:
*
* 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);
*
* 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的字符集标准版本。
*
* The Java language provides special support for the string
* concatenation operator ( + ), and for conversion of
* other objects to strings.
* java语言提供了对字符串的特殊支持,如:可以通过"+"号来进行字符串的拼接操作,
* 为其他类提供了与字符串转换的操作
* String concatenation is implemented
* through the {@code StringBuilder}(or {@code StringBuffer})
* class and its {@code append} method.
* 字符串的+号拼接操作是通过StringBuilder或者StringBuffer类的append()方法
* 来实现的
* String conversions are implemented through the method
* {@code toString}, defined by {@code Object} and
* inherited by all classes in Java.
* 对象与字符串的转换操作是通过所有类的父类Object中定义的toString()方法来实现的
* For additional information on
* string concatenation and conversion, see Gosling, Joy, and Steele,
* The Java Language Specification.
*
*
Unless otherwise noted, passing a null argument to a constructor
* or method in this class will cause a {@link NullPointerException} to be
* thrown.
* 除非有特殊说明,否则传一个null给String的构造方法或者put方法,会报空指针异常的
*
A {@code String} represents a string in the UTF-16 format
* in which supplementary characters are represented by surrogate
* pairs (see the section Unicode
* Character Representations 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}.
*
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 类提供了一些方法区处理单个Unicode编码,除了那些处理Unicode代码单元。
* @since JDK1.0
*/
/** The value is used for character storage. */
// 来用存储String内容的
private final char value[];
// 存储字符串哈希值,默认值为0
private int hash; // Default to 0
// 实现序列化的标识
private static final long serialVersionUID = -6849794470754667710L;
char value[]被final修饰,说明value[]数组是不可变的。
构造方法
/**
* 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.
* 初始化新创建的String对象,时期表示空字符串序列。
* 注意:这个构造方法的用法是没必要的,因为字符串是不可变的
*/
public String() {
this.value = "".value;
}
无参构造方法中是将一个空字符串的value值赋给当前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.
* 初始化创建的String对象,时期表示与参数相同的字符串序列。
* 换句话说:新创建的字符串是参数自粗糙的副本。
* 除非,如果需要original的显示副本,否则也是没有必要使用此构造方法的
* 因为字符串是不可变的
* @param original
* A {@code String}
*/
public String(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},以便它表示字符数组参数中当前包含的字符。这个
* 复制字符数组的内容;随后修改字符数组不影响新创建的字符串。
* @param value
* The initial value of the string
*/
public String(char value[]) {
//注:将传过来的char数组copy到value数组里
this.value = Arrays.copyOf(value, value.length);
}
//Arrays类中的copyOf方法
public static char[] copyOf(char[] original, int newLength) {
//创建一个新的char数组
char[] copy = new char[newLength];
//把original数组中内容拷贝到新建的char数组中
System.arraycopy(original, 0, copy, 0, Math.min(original.length, newLength));
//返回新建的char数组
return copy;
}
public boolean equals(Object anObject) {
//首先会判断是否是同一个对象
if (this == anObject) {
return true;
}
//判断是否为String类型
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = value.length;
//长度是否相同
if (n == anotherString.value.length) {
char v1[] = value;
char v2[] = anotherString.value;
int i = 0;
//逐个遍历判断是否相等
//从后往前单个字符判断,如果有不相等,返回假
while (n-- != 0) {
//不相等,直接返回false
if (v1[i] != v2[i])
return false;
i++;
}
return true;
}
}
return false;
}
补充:==比较
==比较基本数据类型,比较的是值
==比较引用数据类型,比较的是地址值
substring()方法
substring方法在工作使用的也是相当的多,作用就是截取一段字符串。
public String substring(int beginIndex) {
if (beginIndex < 0) {
throw new StringIndexOutOfBoundsException(beginIndex);
}
int subLen = value.length - beginIndex;
if (subLen < 0) {
throw new StringIndexOutOfBoundsException(subLen);
}
//如果beginIndex==0,返回的是当前对象,
//否则这里是new的一个新对象,其实String中的很多函数都是这样的操作
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.
*/
public native String intern();