[HD-OCP17/21-Fundamentals Pg 265, Sec. 11.1.6 - manipulating-strings]

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

Moderator: admin

Post Reply
raphaelzintec
Posts: 167
Joined: Sun Apr 21, 2024 10:43 am
Contact:

[HD-OCP17/21-Fundamentals Pg 265, Sec. 11.1.6 - manipulating-strings]

Post by raphaelzintec »

Furthermore, any method that attempts to access an index that is beyond the range of this array throws IndexOutOfBoundsException.

so basically all String methods throw IndexOutOfBoundsException except substring() which throws StringIndexOutOfBoundsException ?

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

Re: [HD-OCP17/21-Fundamentals Pg 265, Sec. 11.1.6 - manipulating-strings]

Post by admin »

For most of the methods (that throw IOOBE) of the String class, the JavaDoc say these methods throws IOOBE but the implementation of some of these methods (including substring) throw SIOOBE. Since SIOOBE extends IOOBE, the implementation is technically within the agreed contract.

The JavaDoc for indexOf method says it throws SIOOBE.

dtruebin
Posts: 3
Joined: Wed Jun 18, 2025 4:39 am
Contact:

Re: [HD-OCP17/21-Fundamentals Pg 265, Sec. 11.1.6 - manipulating-strings]

Post by dtruebin »

21st Jun 2025 Build 2.20: the summary table at the beginning of the section mentions method firstIndexOf which exists neither in Java 17's nor in Java 21's String.

dtruebin
Posts: 3
Joined: Wed Jun 18, 2025 4:39 am
Contact:

Re: [HD-OCP17/21-Fundamentals Pg 265, Sec. 11.1.6 - manipulating-strings]

Post by dtruebin »

21st Jun 2025 Build 2.20:

Code: Select all

//the following lines print false
System.out.println(s1.indent(0) == s1); //prints false even though there is no change
-- "there is no change" is clearly wrong, as demonstrated by the following JShell snippet:

Code: Select all

jshell> "a".stripIndent()
$86 ==> "a"
Even in the book itself, this is supported by this sentence a couple of paragraphs earlier:
More interestingly, if the last line is not terminated using a line terminator, a \n character is appended.

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

Re: [HD-OCP17/21-Fundamentals Pg 265, Sec. 11.1.6 - manipulating-strings]

Post by admin »

dtruebin wrote:
Sun Jul 13, 2025 6:15 am
21st Jun 2025 Build 2.20: the summary table at the beginning of the section mentions method firstIndexOf which exists neither in Java 17's nor in Java 21's String.
Correct. indexOf is actually returns the first index so there is no need for firstIndexOf. There is just lastIndexOf.

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

Re: [HD-OCP17/21-Fundamentals Pg 265, Sec. 11.1.6 - manipulating-strings]

Post by admin »

dtruebin wrote:
Sun Jul 13, 2025 6:29 am
21st Jun 2025 Build 2.20:

Code: Select all

//the following lines print false
System.out.println(s1.indent(0) == s1); //prints false even though there is no change
-- "there is no change" is clearly wrong, as demonstrated by the following JShell snippet:

Code: Select all

jshell> "a".stripIndent()
$86 ==> "a"
Even in the book itself, this is supported by this sentence a couple of paragraphs earlier:
More interestingly, if the last line is not terminated using a line terminator, a \n character is appended.
You are right.s1.indent(0) adds a new line character and so false is expected. Although "a".stripIndent() produces a different string with the same content.

Thank you for your feedback. Please let me know your name so that it can be added to the acknowledgements pages. You may email it to support@enthu... if you don't want to reveal it here.

Post Reply

Who is online

Users browsing this forum: No registered users and 232 guests