String类
一、String
介绍
数组为final修饰 ,所以不可变
public final class String
extends Object
implements Serializable, Comparable<String>, CharSequence{
/** The value is used for character storage. */
private final char value[];
The , are implemented as instances of this class.
String str = "abc";
is equivalent to:
char data[] = {'a', 'b', 'c'}; String str = new String(data);
一些方法
String()
/**
* 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.
*/
public String() {
this.value = "".value;
}
String(String original)
/**
* 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.
*
* @param original
* A {@code String}
*/
public String(String original) {
this.value = original.value;
this.hash = original.hash;
}
compareTo(String anotherString)
对两个字符串从首位判断是否相等,不相等则根据ascii值相减;
public int compareTo(String anotherString) {
int len1 = value.length;
int len2 = anotherString.value.length;
int lim = Math.min(len1, len2);
char v1[] = value;
char v2[] = anotherString.value;
int k = 0;
while (k < lim) {
char c1 = v1[k];
char c2 = v2[k];
if (c1 != c2) {
return c1 - c2;
}
k++;
}
return len1 - len2;
}
hashCode()
//hash值默认为0 所有hashCode算出来的值是根据value算出来的 字符串值一样hashcode就一样
public int hashCode() {
int h = hash;
if (h == 0 && value.length > 0) {
char val[] = value;
for (int i = 0; i < value.length; i++) {
h = 31 * h + val[i];
}
hash = h;
}
return h;
}
equals(Object anObject)
//在类型相同长度相同的情况下比较value数组中每个char
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
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) {
if (v1[i] != v2[i])
return false;
i++;
}
return true;
}
}
return false;
}
s.concat(s2); 在s尾部加入s2
s=s.trim() 清空尾部的空格
String的==和equals和hascode
String s = "aaa";
String s1 = "aaa" ;
String s2 = new String(); s2=s;
String s3 = new String(s);
String s4 = new String("aaa");
输出:
//hash值默认为0 所有hashCode算出来的值是根据value算出来的 字符串值一样hashcode就一样
s.hashCode()96321
s1.hashCode()96321
s2.hashCode()96321
s3.hashCode()96321
s4.hashCode()96321
//但是地址不一定一样 ==比较的是地址
s的地址23934342
s1的地址23934342
s2的地址23934342
s3的地址22307196
s4的地址22307196
s1==s?true
s2==s?true
s3==s?false
s4==s?false
equals true
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-CA4y7H38-1569662986162)(C:\Users\Administrator\Desktop\draw\String.png)]
二、Stringbuider
public final class StringBuilder
extends AbstractStringBuilder
implements java.io.Serializable, CharSequence
{
abstract class AbstractStringBuilder implements Appendable, CharSequence {
char[] value;
int count;
AbstractStringBuilder() {
}
AbstractStringBuilder(int capacity) {
value = new char[capacity];
}
value数组没有final修饰符,所以可变。
三、StringBuffer
public final class StringBuffer
extends AbstractStringBuilder
implements java.io.Serializable, CharSequence
{
[capacity];
}
value数组没有final修饰符,所以可变。
## 三、StringBuffer
~~~java
public final class StringBuffer
extends AbstractStringBuilder
implements java.io.Serializable, CharSequence
{
Stringbuff大多数函数增加了synchronized标识符