今天做了一道练习题,题目如下:public String makinStrings() {
(1) String s = "Fred";
(2) s = s + "47";
(3) s = s.substring(2, 5);
(4) s = s.toUpperCase();
(5) return s.toString();
} How many String objects will be created when this method is invoked? 给出的标准答案是:3个第(1)、(2)肯定会有两个,那么第3个是(3)(4)(5)中的哪一行创建的?String对象不是改变的时候都会new一个对象吗?请教大虾
(1) String s = "Fred";
(2) s = s + "47";
(3) s = s.substring(2, 5);
(4) s = s.toUpperCase();
(5) return s.toString();
} How many String objects will be created when this method is invoked? 给出的标准答案是:3个第(1)、(2)肯定会有两个,那么第3个是(3)(4)(5)中的哪一行创建的?String对象不是改变的时候都会new一个对象吗?请教大虾
所以题目的肯定有两个对象“Fred" "Fred47" .
toUpperCase() 方法将所以字符串 都大写,因此又创建了一个都是大写的字符串对象”FRED47"
个人感觉3,4应该是会产生新对象的
我们看String的source code,
substring(int,int)方法如下,我们可以看到索引合理的话,肯定会new一个String返回的,所以会产生一个对象 public String substring(int beginIndex, int endIndex) {
if (beginIndex < 0) {
throw new StringIndexOutOfBoundsException(beginIndex);
}
if (endIndex > count) {
throw new StringIndexOutOfBoundsException(endIndex);
}
if (beginIndex > endIndex) {
throw new StringIndexOutOfBoundsException(endIndex - beginIndex);
}
return ((beginIndex == 0) && (endIndex == count)) ? this :
new String(offset + beginIndex, endIndex - beginIndex, value);
}再看toUpperCase方法的源代码,我们可以看到,在方法最后要返回的时候也是肯定会new 一个对象的 public String toUpperCase() {
return toUpperCase(Locale.getDefault());
}
public String toUpperCase(Locale locale) {
if (locale == null) {
throw new NullPointerException();
}
........
return new String(0, result.length, result);
}
所以说这两个方法都会生成新的String对象再看String的toString方法,如下,它只是返回它本身,不会生成新的对象 public String toString() {
return this;
}
if (locale == null) {
throw new NullPointerException();
} int firstLower; /* Now check if there are any characters that need to be changed. */
scan: {
for (firstLower = 0 ; firstLower < count; ) {
int c = (int)value[offset+firstLower];
int srcCount;
if ((c >= Character.MIN_HIGH_SURROGATE) &&
(c <= Character.MAX_HIGH_SURROGATE)) {
c = codePointAt(firstLower);
srcCount = Character.charCount(c);
} else {
srcCount = 1;
}
int upperCaseChar = Character.toUpperCaseEx(c);
if ((upperCaseChar == Character.ERROR) ||
(c != upperCaseChar)) {
break scan;
}
firstLower += srcCount;
}
return this;
}
(2) s = s + "47"; 我认为(1)(2)就已经有3个对象了
substring
public String substring(int beginIndex,
int endIndex)返回一个新字符串,它是此字符串的一个子字符串。该子字符串从指定的 beginIndex 处开始,一直到索引 endIndex - 1 处的字符。因此,该子字符串的长度为 endIndex-beginIndex。
public String toUpperCase()使用默认语言环境的规则将此 String 中的所有字符都转换为大写。此方法等效于toUpperCase(Locale.getDefault())。
可重复使用。举个例子:
第一种情况:
String s = “aaa”;
String ss = “aaa”;
第二种情况:
String s = new String("aaa");
String ss = new String("aaa");第一种情况:其实s和ss引用的是同一个地址,故s == ss的结果是true
第二种情况:s和ss引用的不同的地址(但内容相同,都为“aaa”),故s==ss的结果是false所答案是对的,就是三个
(1) String s = "Fred"; //先有"Fred"第一个对象。2) s = s + "47"; //"47"第二个对象,,"Fred47"第三个对象。