• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 22
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic