• 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
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

BiConsumer and the put () method of Map

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


V put (K key, V value)

The put () method has a return type V, but BiConsumer has return type void. I knew it compiles, but why?
 
Bartender
Posts: 5585
214
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi moioi.

Welcome to the Ranch!

A return value of a method call may always be discarded. So, since map.put is such a call, you can use it as if it were a void method. Just like you can do a single statement: 'map.put(k, v);' where you also discard the return value.

This doesn't work:
 
Sheriff
Posts: 28371
99
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's quite common to write code which ignores the value returned by a Java method. You'll often see the Map.put(key, value) method used like that:



The compiler doesn't flag that code as an error in any way, and the value returned by the put() method is just ignored and discarded.

It's exactly the same in your example. Methods which return a value can be used in a context which doesn't expect a value, and the return value is ignored.
 
moioi Yang
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your answers. I didn't realize the method return values could be discarded or ignored in situations like these.

All the examples in the book happen to have the method return types match the interface method return type. So I assumed it's a rule.
 
Paul Clapham
Sheriff
Posts: 28371
99
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's a reasonable assumption, especially if you haven't seen examples where the returned value is ignored. And even if you had, it isn't obvious that they can also be ignored in that lambda-expression context. But yeah, there's actually a rule somewhere in the Java language spec which says that methods' return values can be ignored.
 
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:But yeah, there's actually a rule somewhere in the Java language spec which says that methods' return values can be ignored.


That would be here: https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.1

The Java Language Specification wrote:An expression statement or lambda body that is a method invocation may also invoke a method that produces a result; in this case the value returned by the method is quietly discarded.


Note: emphasis is mine.
 
Bring out your dead! Or a tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic