class HelloWorld
{
public static void main(String[] args)
{
System.out.println("Hello, World!");
String a="hi hello";
a = a.substring(8);
System.out.println(a);
}
}
In this program I entered the value 8 in substring but that should give an error but in output it doesn’t give error and doesn’t even print anything except "Hello, World!"
>Solution :
String.substring(int begin) throws IndexOutOfBoundsException – if beginIndex is negative or larger than the length of this String object.
String a="hi hello";
That String has a length of 8. So when you call it, the first index is the 8’th index. As you have noticed it would be an index out of bounds exception if you did charAt(8). For substring you get a zero length string returned.