Given:
public class Case{
public static void main(String[] args){
String product = "Pen";
product.toLowerCase();
product.concat(" BOX".toLowerCase());
System.out.print(product.substring(4, 6));
}
}
What is the result?
A.
box
B.
nbo
C.
bo
D.
nb
E. An exception is thrown at runtime
题解
字符串对象的 toLowerCase、concat 和 substring 方法都不会影响到字符串对象本身的内容,而是会回传出新的字符串对象。
substring 方法可以取得字符串中某一段子字符串,在这题中要取 product 字符串变量所参考到的「Pen」字符串中,索引 4 到索引 6(不包含索引 6)的子字符串,由于「Pen」字符串的长度没那么长,因此会抛出 StringIndexOutOfBoundsException 例外。
