String
String是java开发中经常会用到的一类,下面对String进行一下简单的介绍。
一、结构
public final class String
implements java.io.Serializable, Comparable<String>, CharSequence {
private final char value[];
private int hash; // Default to 0
private static final long serialVersionUID = -6849794470754667710L;
//下面有一堆方法。。。。
}
从String类的大致结构可以看到两点:
1、String被final关键字修饰,代表它不能被继承,String 中的方法也不能被重写,这也就是常说String是不可变类的原因;
2、String的值本质上是char类型的数组,从而可以理解为什么String中支持charAt(int index)这样的方法,另外,String的value属性也是被final关键字修饰的,进一步说明String不可变。但是注意,这里的不可变指的是value指向的地址不可变,但是数组里面的值是可以变的,可以通过反射去改变value数组里面的值
String 的比较是平时常常遇到的问题,下面来看几个例子
String str1 = "a";
String str2 = new String("a");
System.out.println(str1==str2);
str2 = str2.intern();
System.out.println(str1==str2);
在上面的例子中第一个str1==str2为false。原因是str1指向"a"这个对象,而str2指向另一个对象,只是这个对象的值是"a"
上面例子中第二个str1==str2为true。原因我们来看jdk的解释
/**
* Returns a canonical representation for the string object.
* <p>
* A pool of strings, initially empty, is maintained privately by the
* class {@code String}.
* <p>
* 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.
* <p>
* It follows that for any two strings {@code s} and {@code t},
* {@code s.intern() == t.intern()} is {@code true}
* if and only if {@code s.equals(t)} is {@code true}.
* <p>
* All literal strings and string-valued constant expressions are
* interned. String literals are defined in section 3.10.5 of the
* <cite>The Java™ Language Specification</cite>.
*
* @return a string that has the same contents as this string, but is
* guaranteed to be from a pool of unique strings.
*/
public native String intern();
大致的意思就是,当常量池已经存在equals当前String对象的String对象得时候,将这个String对象的引用赋值给当前String对象
下面再来看一个例子:
String a = "abc";
String b = "def";
String c = "abcdef";
String d = a+b;
String e = "abc" + "def";
System.out.println(c==d);
System.out.println(c==e);
c==d为false,原因是程序在编译的时候只知道a和b是指向两个字符串对象的空间,但是不知道a和b到底是啥,所以计算出来的地址和"abcdef"的地址不一样;
c==e为true,原因是程序在编译的时候就知道"abc"+“def"就是"abcdef”,所以c和e都是指向的"abcdef"的地址