Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6815257
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:47:08+00:00 2026-05-26T20:47:08+00:00

Why does the following Java code snippet throw a StringIndexOutOfBoundsException on the third line

  • 0

Why does the following Java code snippet throw a StringIndexOutOfBoundsException on the third line of code?

String str = "1234567890";
String sub1 = str.substring(0, 3);
String sub2 = str.substring(4, 1);

I’d have expected the result of the above code to be that sub1 contains “123” and sub2 contains “5”, but instead I get the exception mentioned above. Does the first substring call have have side-effect on the string being operated on?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T20:47:09+00:00Added an answer on May 26, 2026 at 8:47 pm

    Because beginIndex is larger than endIndex

    See the doc:

    Throws:
    IndexOutOfBoundsException – if the beginIndex is negative, or endIndex is larger than the length of this String object, or beginIndex is larger than endIndex.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following java code does exactly what is expected: 1 String s = ♪♬♪♪♬♪♪♬♪♪♬♪♪♬♪♪♬♪;
does the following snippet throw NPE when argument is null? public void doSomething(String string)
Why does the following code sometimes causes an Exception with the contents CLIPBRD_E_CANT_OPEN: Clipboard.SetText(str);
Does the following Java code guarantee an exclusive lock on an unopened file in
I have the following Java code: String query = Select 1 from myTable where
I came across some Java code that had the following structure: public MyParameterizedFunction(String param1,
From an online notes, I read the following java code snippet for reversing a
I have the following Java code: String p = seven; String q = teen;
Let's see the following code snippet in Java. public class Main { public static
Let's look at the simple Java code in the following snippet: public class Main

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.