System.out.println("----"+"".substring(1).length());//question

System.out.println("----"+"1".substring(1).length());第一个出现错误是肯定的,我想了解为什么第二个不报错呢输出来的时候什么都没有,为什么,原因

解决方案 »

  1.   

    lz可以查下API文档,对于String的subString方法给了几个事例:示例: "emptiness".substring(9) returns "" (an empty string)抛出: 
    IndexOutOfBoundsException - 如果 beginIndex 为负或大于此 String 对象的长度
    如果参数大于字符串的长度,就抛出异常,但是如果参数等于字符串的长度,则像给出的实例一样是返回空字符串。
    这里"1"的长度是1,因此"1".subString(1)返回空字符串。
      

  2.   

    System.out.println("----"+"".substring(1).length()); 先执行的应该是"".substring(1).length()
    你的""长度为0 自然会告诉你越界了