????public class StringHashCode {
????public static void main(String[] args) {
????\?????????
????String[] hellos = "Hello Hello".split(" " );
????System.out.println(""+hellos[0].hashCode());
????System.out.println(""+hellos[1].hashCode());
????\?????????
????String a = new String("hello");
????String b = new String("hello");
????System.out.println(""+a.hashCode());
????System.out.println(""+b.hashCode());
????}
????}
????????
????String ????final?????????С???String?????????÷??????? ???????С?
????String ??length????????????????length????
????String s = new String(“xyz”); ??????????????????
???????????????????洢??“xyz”?? ?????new?????????????
????String ?? StringBuffer??String Builder????
?????????????? StringBuffer > String
????Java.lang.StringBuffer ?????????????????С?????????? String ??????????????????????????????????????????????????????????????У???????Щ?????????????????е????????????????п???????????????????????????????????????????Щ????????????????????????????????в????????????????????????????????漰??????????е???????????????
????StringBuffer ???????????? append ?? insert ??????????????Щ??????????????????????????????????????Ч???????????????????????????????????????????????????????????С?append ??????????Щ????????????????????? insert ??????????????????????
???????磬??? z ???????????????? “start”??????????????????????????? z.append(“le”) ?????????????????? “startle”( ???); ?? z.insert(4?? “le”) ????????????????????????? “starlet”??
?????????????? StringBuilder > StringBuffer
????java.lang.StringBuilder ???????????????? JAVA 5.0 ?????????????????? StringBuffer ????? API????????????????????ó????????????????????? StringBuffer ??????????滻????????????????????????????????????????????飩?????????????????????????????????????У????? StringBuffer ??????????÷????????????
???????
????String??StringBuffer??StringBuilder???????CharSequence????
????public class StringHashCode {
????public static void main(String[] args) {
????\?????????
????String[] hellos = "Hello Hello".split(" " );
????System.out.println(""+hellos[0].hashCode());
????System.out.println(""+hellos[1].hashCode());
????\?????????
????String a = new String("hello");
????String b = new String("hello");
????System.out.println(""+a.hashCode());
????System.out.println(""+b.hashCode());
????}
????}
????String?????
????public final class String{
????private final char value[]; // used for character storage
????private int the hash; // cache the hash code for the string
????}
????????????????????
????final??char????????
????int?????hashcode
??????????
????public String()
????public String(String original){
????this.value = original.value;
????this.hash = original.hash;
????}
????public String(char value[]){
????this.value = Arrays.copyOf(value?? value.length);
????}
????public String(char value[]?? int offset?? int count){
????// ?ж?offset??count??offset+count?????????
????this.value = Arrays.copyOfRange(value?? offset?? offset+count);
????}
??????????????Щ???????
????copyOf(source[]??length); ????????0λ?????length????
???????????????System.arraycopy(original?? 0?? copy?? 0?? Math.min(original.length?? newLength))?????
????copyOfRange(T[] original?? int from?? int to)??
??????????????????StringBuffer/StringBuilder????????String??
????public String(StringBuffer buffer) {
????synchronized(buffer) {
????this.value = Arrays.copyOf(buffer.getValue()?? buffer.length());
????}
????}
????public String(StringBuilder builder) {
????this.value = Arrays.copyOf(builder.getValue()?? builder.length());
????}
???????????????String???????к??
????length??isEmpty?????????????value.length??????
????charAt(int index):
???????????value??????????????ж?index????????
????public char charAt(int index) {
????if ((index < 0) || (index >= value.length)) {
????throw new StringIndexOutOfBoundsException(index);
????}
????return value[index];
????}
????getChars????
????public void getChars(int srcBegin?? int srcEnd??
????char dst[]?? int dstBegin)
????{
????\?????
????System.arraycopy(value?? srcBegin?? dst?? dstBegin?? srcEnd - srcBegin);
????}
????equals???????????????????????????????????????棩???????????equals
????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;
????}
???????????????????????棬????????????==?????
?????????????????????????????£?
????????anObject????String??????ù????instanceof??
?????????????????????
????????????????????????б???????????????????true.
?????????????????StringBuffer??????
????contentEquals(StringBuffer sb)?????????sb??????????
????compareTo????:
???????A????B?????????0??????
????AС??B??????С??0??????
????A=B??????0
????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;
????}
????regionMatches:?????????????????????????
public boolean regionMatches(int toffset?? String other?? int ooffset??
int len)
{
//?ж???????
while (len-- > 0) {
if (ta[to++] != pa[po++]) {
return false;
}
}
}
public boolean regionMatches(boolean ignoreCase?? int toffset??
String other?? int ooffset?? int len)
{
while (len-- > 0) {
char c1 = ta[to++];
char c2 = pa[po++];
if (c1 == c2) {
continue;
}
if (ignoreCase) {
// If characters don't match but case may be ignored??
// try converting both characters to uppercase.
// If the results match?? then the comparison scan should
// continue.
char u1 = Character.toUpperCase(c1);
char u2 = Character.toUpperCase(c2);
if (u1 == u2) {
continue;
}
// Unfortunately?? conversion to uppercase does not work properly
// for the Georgian alphabet?? which has strange rules about case
// conversion.  So we need to make one last check before
// exiting.
if (Character.toLowerCase(u1) == Character.toLowerCase(u2)) {
continue;
}
}
return false;
}
return true;
}
startsWith(String prefix?? int toffset)
startsWith(String prefix)
endsWith(String suffix)
{
return startsWith(suffix?? value.length
- suffix.value.length);
}
substring(int beginIndex??int endIndex)
?????????????ж??
????return (beginIndex == 0) ? this : new String(value?? beginIndex?? subLen);
?????????????concat(String str)
????int otherLen = str.length();
????if (otherLen == 0) {
????return this;
????}
????int len = value.length;
????char buf[] = Arrays.copyOf(value?? len + otherLen);
????str.getChars(buf?? len);
????return new String(buf?? true);
????????StringBuffer??StringBuilder
????StringBuffer ?? StringBuilder ???????? AbstractStringBuilder?? ?????????????append????????????????С?
????public AbstractStringBuilder append(String str) {
????if (str == null) str = "null";
????int len = str.length();
????ensureCapacityInternal(count + len);//???????????????????????
????str.getChars(0?? len?? value?? count);//getChars??????native??array copy???????Ч
????count += len;
????return this;
????}
????StringBuffer ?????? char[]?? ??????????????????С?? ???????? append ????г????????С?????????????????????????????????飬??????????? ArrayList ???????????????????? append ???????????????????????????????????????????????????????С?