Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Please help me understand a line in this piece of code (K&B7)

 
Sama Willson
Greenhorn
Posts: 22
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

I am having a little bit of confusion here. I have attached a snippet of code from one of the mock tests. I am confused about line #5, it calls the insert method to add a char sequence at str.length(21) which is last index +1, when the index ends at 20. Why does it not throw an exception since the index is out of bounds? Please help me understand this. Thanks! 
2016-08-24.png
[Thumbnail for 2016-08-24.png]
code snippet
 
Henry Wong
author
Marshal
Pie
Posts: 22111
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sama Willson wrote:I have attached a snippet of code from one of the mock tests.


Please QuoteYourSources.

Henry
 
Sama Willson
Greenhorn
Posts: 22
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Henry,

Its from one of  the mock tests included in the Kathy Sierra and Bert Bates OCA/OCP Java 7 guide CD.

Thanks!
 
Paul Clapham
Sheriff
Posts: 21567
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And why should it throw an exception? After all, it doesn't attempt to access a character at position 21 -- if it did, an exception would certainly be thrown.

Imagine a StringBuilder containing a single character, suppose it's "X". You might want to insert a character before that, producing "AX", or you might want to insert one after that, producing "XA". To insert before the beginning you would use str.insert(0, "A"), correct? And to insert after the end, how would you do that? I suggest that str.insert(1, "A") would be the right way to do that. And you wouldn't want an exception to be thrown just because position 1 is after the end of the buffer.
 
Henry Wong
author
Marshal
Pie
Posts: 22111
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sama Willson wrote:I am confused about line #5, it calls the insert method to add a char sequence at str.length(21) which is last index +1, when the index ends at 20. Why does it not throw an exception since the index is out of bounds?


Well, on one hand, when called with the length, I agree that it is not an insert operation; it is an append operation.

On the other hand, it is a valid location to place characters. It would be kinda inefficient to have a special case for append vs insert.... and additionally, the JavaDoc for StringBuilder does explicitly mention that it is within bounds, so why not?

Henry
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sama Willson wrote:I have attached a snippet of code from one of the mock tests.

Why do you post code snippets as screenshots and not the plain code snippets (and you should of course UseCodeTags )? If I want to compile (and execute) your code snippets, I know have to type them myself. So I'll be losing precious time which I could otherwise be spending on typing a nice reply... And another reason: attached images are not searchable using the search engine, plain text or a code snippet definitely is.

Sama Willson wrote:I am confused about line #5, it calls the insert method to add a char sequence at str.length(21) which is last index +1, when the index ends at 20. Why does it not throw an exception since the index is out of bounds? Please help me understand this.

According to the javadoc of the (overloaded) insert() method of the StringBuilder class, the offset argument must be greater than or equal to 0, and less than or equal to the length of the sequence itself. So the length of the StringBuilder object is a valid offset for the insert() method and that's why no exception is thrown at runtime.
If you are using the length of the StringBuilder object as the offset, you are inserting the value at the end of the char sequence. So you are actually appending the value That's why these two code snippets are functionally equivalent

Hope it helps!
Kind regards,
Roel
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic