publicbooleanequals(Object anObject){if(this== anObject){returntrue;}if(anObject instanceofString){
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])returnfalse;
i++;}returntrue;}}returnfalse;}
2. Long
2.1 LongCache
privatestaticclassLongCache{privateLongCache(){}staticfinal Long cache[]=newLong[-(-128)+127+1];static{for(int i =0; i < cache.length; i++)
cache[i]=newLong(i -128);}}
2.2 Long.valueOf(Long l)
publicstatic Long valueOf(long l){finalint offset =128;if(l >=-128&& l <=127){// will cachereturn LongCache.cache[(int)l + offset];}returnnewLong(l);}
2.3 Long.parseLong(Stirng s, int radix)
publicstaticlongparseLong(String s,int radix)throws NumberFormatException {if(s == null){thrownewNumberFormatException("null");}if(radix < Character.MIN_RADIX){thrownewNumberFormatException("radix "+ radix +" less than Character.MIN_RADIX");}if(radix > Character.MAX_RADIX){thrownewNumberFormatException("radix "+ radix +" greater than Character.MAX_RADIX");}long result =0;boolean negative =false;int i =0, len = s.length();long limit =-Long.MAX_VALUE;long multmin;int digit;if(len >0){char firstChar = s.charAt(0);if(firstChar <'0'){// Possible leading "+" or "-"if(firstChar =='-'){
negative =true;
limit = Long.MIN_VALUE;}elseif(firstChar !='+')throw NumberFormatException.forInputString(s);if(len ==1)// Cannot have lone "+" or "-"throw NumberFormatException.forInputString(s);
i++;}
multmin = limit / radix;while(i < len){// Accumulating negatively avoids surprises near MAX_VALUE
digit = Character.digit(s.charAt(i++),radix);if(digit <0){throw NumberFormatException.forInputString(s);}if(result < multmin){throw NumberFormatException.forInputString(s);}
result *= radix;if(result < limit + digit){throw NumberFormatException.forInputString(s);}
result -= digit;}}else{throw NumberFormatException.forInputString(s);}return negative ? result :-result;}
privatestaticclassIntegerCache{staticfinalint low =-128;staticfinalint high;staticfinal Integer cache[];static{// high value may be configured by propertyint h =127;
String integerCacheHighPropValue =
sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");if(integerCacheHighPropValue != null){try{int i =parseInt(integerCacheHighPropValue);
i = Math.max(i,127);// Maximum array size is Integer.MAX_VALUE
h = Math.min(i, Integer.MAX_VALUE -(-low)-1);}catch( NumberFormatException nfe){// If the property cannot be parsed into an int, ignore it.}}
high = h;
cache =newInteger[(high - low)+1];int j = low;for(int k =0; k < cache.length; k++)
cache[k]=newInteger(j++);// range [-128, 127] must be interned (JLS7 5.1.7)assert IntegerCache.high >=127;}privateIntegerCache(){}}