String.subString引发的StringIndexOutOfBoundsException

首先看两个例子,通过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异常。

(完)

转载于:https://my.oschina.net/u/167082/blog/337098

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值