Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

OCA/OCP Java SE 8 Programmer Practice Tests  RSS feed

 
cleverson teo
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm studying the book OCA/OCP Java SE 8 Programmer Practice Tests, by Scott Selikoff and Jeanne Boyarsky. The question 46 at chapter 9 states the following:

46. Which of the following types can you pass as a parameter to the replace() method on the String class?
I. char
II. String
III. StringBuilder

And the answer is the following:

D.
There are two signatures for the replace() method. One takes two char parameters. The other signature takes a CharSequence. Both String and StringBuilder implement this interface. This makes all three alternatives correct, and Option D is correct.

The question asks for the types that can be passed for replace() method on the String class. The String class only have the following versions of the replace method:
public String replace(char oldChar, char newChar)
public String replace(CharSequence target, CharSequence replacement)
So far so good, this is contemplated by the answer. However, why String and StringBuider are considered valid, once there isn't a method that takes them as parameter. I couldn't really understand what "Both String and StringBuilder implement this interface" has to do with it.
 
O Shea
Ranch Hand
Posts: 102
2
Java MySQL Database Oracle
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


OUTPUT-> GAR


Hope this could help
 
Roel De Nijs
Sheriff
Posts: 11333
177
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
cleverson teo wrote:However, why String and StringBuider are considered valid, once there isn't a method that takes them as parameter. I couldn't really understand what "Both String and StringBuilder implement this interface" has to do with it.

If classes String and StringBuilder implement interface CharSequence, would these classes not be valid parameters for the second overloaded version of the replace method?

If you check the javadoc of the CharSequence interface, you'll find a nice overview of all known implementing classes.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!