Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Map put() & BiConsumer functional interface

 
Ranch Hand
Posts: 127
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

The put() method in Map interface adds or replace the key value pair. And it returns the previous value or null.

But BiConsumer functional interface's abstract method "accept()" does not return anything.
See line 7. The code compiles and no error. Why is that?


 
Sheriff
Posts: 16767
281
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because the return value simply is ignored and not used.
 
henry leu
Ranch Hand
Posts: 127
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:Because the return value simply is ignored and not used.



Just ignored like that?

So any value or null that is returned by lambda expression doesn't count as return type in Java?

 
Sheriff
Posts: 26973
85
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


You can write that as a line of code in Java. There is no rule saying you have to use the result of the put method; you don't have to assign it to a variable or call one of its methods or anything. It's the same thing in the construct you're asking about.
 
Junilu Lacar
Sheriff
Posts: 16767
281
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

henry leu wrote:Just ignored like that?

So any value or null that is returned by lambda expression doesn't count as return type in Java?


You have to remember that lambda expressions are syntactical constructs only. That means they exist in that form only in source code. When compiled, they are actually objects that implement a functional interface. What functional interface that is depends on the context in which the lambda is declared. In your example, it would be a BiConsumer<String, Integer> implementation. Essentially, it is compiled as this:

The lambda expression is just shorthand for this, so it's easy for us as programmers to express the idea in a succinct way instead of drowning out the idea with all the syntactical infrastructure required by the language.
 
Marshal
Posts: 74783
336
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

henry leu wrote:. . .  any value or null that is returned by lambda expression doesn't count as return type in Java?

I think you have misunderstood what Junilu said. He pointed out that a value returned is recorded inmemory somewhere, and if it isn't used it will simply be overwritten next time that memory area is used for something else. I am sure you ignore returned values all the time; I  bet you have often written...and forgotten that you can write...instead. If a λ happens to have a method returning something, you can use that return value or forget about it. The only time you can't use it, but must ignore it, is when there is no return value. That means, when the functional method is declared as void.
 
henry leu
Ranch Hand
Posts: 127
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The only time you can't use it, but must ignore it, is when there is no return value. That means, when the functional method is declared as void.



Okay, but the BiConsumer functional method is declared as void.
 
Rancher
Posts: 4071
56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And?  I'm not sure why there's a "but" in that sentence.
 
henry leu
Ranch Hand
Posts: 127
5
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got it.


So a method that returns, you can either use the return value or ignore the return value.

Thanks!
 
Bartender
Posts: 370
44
Firefox Browser MySQL Database Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As we discussed in this thread, the Java Language Specification specifically allows a functional interface with a void return type to be implemented by a statement expression, regardless of whether that expression returns a value.

JLS 15.27.3 wrote:A lambda expression is congruent with a function type if all of the following are
true:
  • The function type has no type parameters.
  • The number of lambda parameters is the same as the number of parameter types of the function type.
  • If the lambda expression is explicitly typed, its formal parameter types are the same as the parameter types of the function type.
  • If the lambda parameters are assumed to have the same types as the function type's parameter types, then:
      – If the function type's result is void, the lambda body is either a statement expression (§14.8) or a void-compatible block.
      – If the function type's result is a (non- void ) type R , then either (i) the lambda body is an expression that is compatible with R in an assignment context, or (ii) the lambda body is a value-compatible block, and each result expression (§15.27.2) is compatible with R in an assignment context.



 
reply
    Bookmark Topic Watch Topic
  • New Topic