首先看两个例子,通过subString方法获得字符串t,再通过t.charAt(3)方法获得字符串t的值中的第四个字符。其中会利用反射机制,改变字符串s的值。
例子1:
public class Test {
public static void main(String[] args) throws Exception {
String s="0123456789";
String t = s.substring(1); // 注意看这里
System.out.println("t.charAt(3)为" + t.charAt(3));
Field f = s.getClass().getDeclaredField("value");
f.setAccessible(true);
f.set(s, new char[]{'a','b','c'});
System.out.println("t.charAt(3)为" + t.charAt(3));
}
}
例子1 结果:
t.charAt(3)为4
t.charAt(3)为4
例子2:
public class Test {
public static void main(String[] args) throws Exception {
String s="0123456789";
String t = s.substring(0);//注意看这里
System.out.println("t.charAt(3)为" + t.charAt(3));
Field f = s.getClass().getDeclaredField("value");
f.setAccessible(true);
f.set(s, new char[]{'a','b','c'});
System.out.println("t.charAt(3)为" + t.charAt(3));
}
}
例子2 结果:
t.charAt(3)为3
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 3
at java.lang.String.charAt(String.java:658)
at com.elong.mobile.guide.Test.main(Test.java:17)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
分析原因
首先看一下java doc中对public String substring(int beginIndex)方法的说明
Returns a new string that is a substring of this string. The substring begins with the character at the specified index and extends to the end of this string.
文档中说明subString方法会返回一个新的String对象,但上面两个例子中,只有beginIndex值不同,结果确不同,例1能正常运行,例2却抛出了StringIndexOutOfBoundsException异常。我们再看一下String当中subString方法的源码,一探究竟。
public String substring(int beginIndex) {
if (beginIndex < 0) {
throw new StringIndexOutOfBoundsException(beginIndex);
}
int subLen = value.length - beginIndex;
if (subLen < 0) {
throw new StringIndexOutOfBoundsException(subLen);
}
return (beginIndex == 0) ? this : new String(value, beginIndex, subLen);
}
注意最后一行,原因就出现在这里,当beginIndex等于0时,Java会返回当前对象的引用,不会创建新的String对象,当我们通过反射改变字符串s的值时,再通过t.charAt方法获取值的时候,就可能会抛出StringIndexOutOfBoundsException异常。
(完)