String.subString引起的StringIndexOutOfBoundsException

首先看两个例子,经过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

相关文章
相关标签/搜索