【问题排查】StringIndexOutOfBoundsException

工作中遇到 java.lang.StringIndexOutOfBoundsException ,查看网上资料,总结如下

1、异常定义:

Java API指出StringIndexOutOfBoundsException异常
Thrown by String methods to indicate that an index is either negative or greater than the size of the string. For some methods such as the charAt method。

以 java.lang.StringIndexOutOfBoundsException: String index out of range: 10 异常举例, 当String.length()<10,而你又要取index>=10 的字符时,会抛出上面的异常

2、关于String使用

String是不可变类,每一步操作都会返回新的String变量,占用时间和内存空间;

如 : String = String + " b"; 

实际上是新创建了个String变量 

参考资料:

http://blog.chinaunix.net/uid-11361941-id-2880422.html

原文地址:https://www.cnblogs.com/bukudekong/p/5610817.html