• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

K&B book page 422&426: index from zero or one?

 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On page 422, there is a discussion about String.substring(int begin, int end)
ON page 426, there is a discussion about StringBuilder.delete(int begin, int end)

the book says the the second argument is one based and give a way to count it, the result is correct.

As I recalled, Java is a language using index starts from zero (a few cases related with database etc. excepted), so we'd better to use the way described in JDK API:
start - The beginning index, inclusive.
end - The ending index, exclusive.

I also think what is max value the end parameter is worth to be mentioned.
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ranchers,

Joseph Zhou wrote

... so we'd better to use the way described in JDK API:
start - The beginning index, inclusive.
end - The ending index, exclusive.


Agreed, also my impression.
API also says why it is this way:

Thus the length of the substring is endIndex-beginIndex.



Yours,
Bu.
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure I understand the concern here - let me paraphrase what I think you're sayng:

While the explanations on pages 422 and 426 are technically correct, they don't match the language used in the API - and therefore they should be changed to match the language in the API.

Is that what you guys are saying?

Thanks,

Bert
 
Joseph Zhou
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are right Bert. Also, uses the same way for both good for getting correct result: Thus the length of the substring is endIndex-beginIndex.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bert, I'd say that both descriptions of the SDK are equally confusing; therefore, I'd err on the side of including the concept as it's described in the API. The apparent oddity is still worth trying to explain, however.

My own explanation for it has always been that the path-of-least-surprise can be defined to be the one that the one-parameter version of the method probably chains gracefully into the two parameter version.

So for any given String s:


That might be confusing/backwards, but coming from a CS 101 perspective when I first learned this, I imagined that because the SDK implementation would be simpler this way, it made more sense. Call me crazy.
 
Bert Bates
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you're crazy!

Thanks for all your input guys - as it's not really an error I'm ading to my list of things to review before the next reprint.

Bert
 
I will open the floodgates of his own worst nightmare! All in a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic