About Question enthuware.ocajp.i.v7.2.1155 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
ETS User

About Question enthuware.ocajp.i.v7.2.1155 :

Post by ETS User »

I see that one is correct but I don't understand how it works.
s = "MINIMUM" has starting index of 0 and ending in 6
How does it accept s.substring(4,7);

Thanks.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1155 :

Post by admin »

Because the rule is that endIndex must not be larger than the length. Here, length is 7 so endIndex can be 7.

You might want to take a look at the this JavaDoc API description for substring

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

enthunoob
Posts: 60
Joined: Thu Apr 15, 2021 12:21 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1155 :

Post by enthunoob »

For others who are also confused with startindex or endindex, and inclusive or exclusive, I think these two rules will help to remember:

- Arrays are always zero based, so the first character of a string (which is an array of characters) always has an index of 0.
- a startindex is always inclusive and an endindex is always exclusive. This applies to all methods in Java, for example also to List.subList(int startindex, int endIndex).

This means that the index number of a first position is never 1.

Post Reply

Who is online

Users browsing this forum: No registered users and 74 guests