• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

chapter 3 is the syntax for startsWith has boolean? (Java OCA 8 Programmer I Study Guide, Sybex)

 
Nithya Mohan anand
Greenhorn
Posts: 7
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have started preparing for my oca 8 exam,I have a question in the book.
In page 109 oca java 8 book by jeanne and scott selikoff, startsWith and endsWith has the following method signatures

boolean startsWith(String prefix)
boolean endsWith(String suffix)

is this right ?

thanks,
Nithya
 
Roel De Nijs
Sheriff
Posts: 10666
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Nithya Mohan anand,

First of all, a warm welcome to CodeRanch!

Nithya Mohan anand wrote:is this right ?

Why would you think it's incorrect?

That's something you can easily verify for yourself. Either check the API documentation of the String class for both methods: boolean startsWith(String) and boolean endsWith(String). Or you could write a little code snippet using both methods and see if the program compiles and see what both method returnsOutput:
starts with '!'? false
ends with '!'? true


Hope it helps!
Kind regards,
Roel
 
Paul Clapham
Sheriff
Posts: 21583
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel, your "compile an example and see what happens" suggestion does confirm that the startsWith() and endsWith() method will accept Strings as parameters, but that doesn't exclude the possibility that their signatures are boolean startsWith(CharSequence cs), for example. However your "check the API documentation" suggestion leads to the unambiguous answer.
 
Roel De Nijs
Sheriff
Posts: 10666
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Roel, your "compile an example and see what happens" suggestion does confirm that the startsWith() and endsWith() method will accept Strings as parameters, but that doesn't exclude the possibility that their signatures are boolean startsWith(CharSequence cs), for example.

True! But when you are preparing for a certification exam, it should become almost a reflex to experiment with every code snippet you encounter during your preparation (preferably without using an IDE). Hands on experience is key! And that will only happen if you write lots of (small) code snippets.

The provided code snippet can easily be adjusted to verify if a CharSequence parameter is allowed for those methods.If you try to compile the Stringy class now, you'll notice that line1 and line2 result in a compiler error, so a CharSequence parameter is not allowed for those methods, only a String parameter is. Exactly like mentioned in the Java API documentation
 
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
Nithya: Welcome to CodeRanch!

Paul: CharSequence isn't on the OCA exam though. So sometimes signatures are simplified in certification books.
 
Nithya Mohan anand
Greenhorn
Posts: 7
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all for your replies... Thanks Jeanne.

I got this question when i was seeing the errata for the chapter 3... in that ,there was a mention about wrong method signature in toLowerCase() method. If that is wrong ,I was wondering,the same applies to many of the other string classes like startsWith() etc...

this is the note on the errata page....
108 Chapter 3 The method signatures for toLowerCase() and toUpperCase() are wrong. They do not take a String parameter.

Kindly clarify.
Thanks

Nithya
 
Roel De Nijs
Sheriff
Posts: 10666
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nithya Mohan anand wrote:I got this question when i was seeing the errata for the chapter 3... in that ,there was a mention about wrong method signature in toLowerCase() method. If that is wrong ,I was wondering,the same applies to many of the other string classes like startsWith() etc...

The errata overview will always clearly mention what's wrong (and how to correct it). In this case the errata item only mentions the methods toLowerCase() and toUpperCase() having a wrong method signatures, so that means that only the method signatures of those two methods are incorrect and should not have a String parameter.
If any other methods would be involved with an incorrect signature, they would have been mentioned as well. Either in this errata item or in another one on a different page (because the errata item occurs on a different page). So it's very easy and simple to interpret (and process) this errata overview: go to the page (mentioned in the first column) and in the "Error" column you'll find what's wrong and what's the correct version so you could fix it in your copy of the study guide. The reporter's name in the "Reported by/via" column is very often a link as well. If you click on this link, you'll find a post/topic in this forum where that issue was reported (and you might find some additional explanation, information, discussion,...).

It's always possible you might encounter another errata item which is not yet mentioned on the official overview. Just create a new topic in this forum to report the potential error Or if you are in doubt about a code snippet or statement from the book, also don't hesitate to create a new topic and share your questions/doubts. You are very likely to get an answer, even from one of the authors.

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