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

Different indexOf signatures in String & StringBuilder (Java OCA 8 Programmer I Study Guide, Sybex)

 
Claudiu Stroe
Greenhorn
Posts: 29
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I'm reading the second time the Jeanne and Scott's OCA java 8 book, I've found a kind of incorrect fact in Chapter 3, at page 114.

charAt(), indexOf(),length(), and substring()
These four methods works exactly the same as in String class. ...


I've found that StringBuilder accepts only indexOf(String s) or indexOf(String s, int fromIndex), while String accepts also indexOf(int ch) and indexOf(int ch,int fromIndex).

Trying:
 
Winston Gutkowski
Bartender
Pie
Posts: 10571
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Claudiu Stroe wrote:I've found a kind of incorrect fact in Chapter 3...
charAt(), indexOf(),length(), and substring()
These four methods works exactly the same as in String class...

Well, if you're being really nitpicky, you could also say that "four methods" is wrong too, since there are clearly more than 4; and only one of those takes no arguments.

My reading of it would be that the commonly defined versions of those methods work the same.

Winston
 
Claudiu Stroe
Greenhorn
Posts: 29
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry Winston, I didn't want to upset anyone.
I thought that someone may get this kind of question on the exam and they will treat it like a String, but the code will not compile.
The other 3 methods -
charAt(), lenght() and substring()
- have exactly the same signiture in both String and StringBuilder.

 
Winston Gutkowski
Bartender
Pie
Posts: 10571
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Claudiu Stroe wrote:Sorry Winston, I didn't want to upset anyone.

No 'sorry' involved; I just wasn't sure what the remark was aimed at. Now I do, have a cow; because it IS worthy of note.

Winston
 
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
Claudiu Stroe wrote:I've found a kind of incorrect fact in Chapter 3, at page 114.

charAt(), indexOf(),length(), and substring()
These four methods works exactly the same as in String class. ...

For me that's not an incorrect fact! You are correct about the fact that the String class has other signatures for the indexOf method, but the statement only claims that those methods work exactly the same as in the String class. Meaning if you know what will be returned when executing str.substring(1, 6), you won't be surprised when you execute the same method on a StringBuilder object as the outcome will be similar (of course it depends on the value/content of both objects ). That statement doesn't claim that the method signatures of those four methods are exactly the same in both the String and StringBuilder class.
But that's just my own interpretation, and yours is definitely different. So it might be useful (to avoid confusion) an additional note is added (in the next edition of the study guide, if any) about the method signatures of the indexOf method.

Just my two cents.
Kind regards,
Roel

PS. I have edited the subject and added a reference to the involved study guide (that's not only easier for other ranchers but also for the authors to spot topics/errata about their study guide). Hope that's okay with you!
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Roel about the interpretation. There are lots of things not on the exam and mentioning too many of them is a distraction.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic