1.string.valueof("11"), "11"是字符串;String s =“” +11,11是整型;
2. string.valueof("11")调用的是:
public static String valueOf(Object obj) {
return (obj == null) ? "null" : obj.toString();
}
String s =“” +11,编译器会优化成,String s =new StringBuilder.append(“”) .append(11); 而.append(11)调用的是:
public StringBuilder append(int i) {
super.append(i);
return this;
}
super.append(i); 调用的是:
public AbstractStringBuilder append(int i) {
if (i == Integer.MIN_VALUE) {
append("-2147483648");
return this;
}
int appendedLength = (i < 0) ? stringSizeOfInt(-i) + 1
: stringSizeOfInt(i);
int spaceNeeded = count + appendedLength;
if (spaceNeeded > value.length)
expandCapacity(spaceNeeded);
Integer.getChars(i, spaceNeeded, value);
count = spaceNeeded;
return this;
}
|
|