what is StringIndexOutOfBoundsException in java code example

StringIndexOutOfBoundsException :-

StringIndexOutOfBoundsException is thrown by the String class,
if index in the string is passing greater then the string size
or negative value of index in string.

Example:-
public class Test {
        public static void main(String[] args) {       
           
          
String str = "hello my friends";
           
            String str1 = str.substring(20);
           
        }
}

/*
 * Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -4
    at java.lang.String.substring(String.java:1938)
    at java.lang.String.substring(String.java:1905)
    at com.Test.main(Test.java:11)
 */

Comments

Recent Post

Recent Posts Widget

Popular posts from this blog

Capture image from webcam java code, examples

Use of req.query, req.params and req.body in NODE JS

How to capture finger prints in java