This week's book giveaway is in the Web Services forum.
We're giving away four copies of Microservices in Action and have Morgan Bruce & Paulo A. Pereira on-line!
See this thread for details.
Win a copy of Microservices in Action this week in the Web Services forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Sybex OCA OCP Practice Test Chap 13 Q 40  RSS feed

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why can’t String::charAt be used as a method reference?

Please take a look at the following example



This code is correctly compiled and runs
 
Sheriff
Posts: 4578
286
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What problem do you see with it?
 
Konstantin Barylnikov
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Cooke wrote:What problem do you see with it?



I meant that String::charAt can be used as a method reference. I think this question is incorrect
 
Tim Cooke
Sheriff
Posts: 4578
286
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you think that?
 
Marshal
Posts: 61805
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does the question give as an answer?
 
Konstantin Barylnikov
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Cooke wrote:Why do you think that?



In the example code, I used String::charAt as a method reference. Question was "Why can’t String::charAt be used as a method reference?" but it can be used as method reference.
Question "says" it can't, but it can, so I think that this question is incorrect
 
Campbell Ritchie
Marshal
Posts: 61805
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That isn't clear, I am afraid. Are you saying there is a mistake in the book?
 
Master Rancher
Posts: 3010
105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String::charAt is in fact a BiFunction, taking two parameters: a String and an int. A Stream usually has only one parameter, so String::charAt is not possible in a Stream (? really). However, if s is a given String, then s::charAt is very well possible in a Stream. And if, like in the examples given, you do have two parameters, then there is nothing wrong with String::charAt.  For example:
 
Konstantin Barylnikov
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:That isn't clear, I am afraid. Are you saying there is a mistake in the book?


Yes, I think there is mistake in the book. It's confusing question
 
Campbell Ritchie
Marshal
Posts: 61805
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please post some more of what the book says, where you think the mistake is.
 
author & internet detective
Posts: 38925
686
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I missed this post two months ago. This is incorrect in the book. When we wrote it, I didn't realize you could pass two values to a method reference.

It was originally pointed out as an errata in this post and is listed in the book errata
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!