首先看兩個例子,經過subString方法得到字符串t,再經過t.charAt(3)方法得到字符串t的值中的第四個字符。其中會利用反射機制,改變字符串s的值。java
例子1:app
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 結果:ide
t.charAt(3)爲4 t.charAt(3)爲4
例子2:ui
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 結果:this
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)
分析緣由.net
首先看一下java doc中對public String substring(int beginIndex)方法的說明code
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異常。blog
(完)ci