• 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
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Map put() & BiConsumer functional interface

 
Ranch Hand
Posts: 73
3
  • Mark post as helpful
  • send pies
  • 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?


 
Marshal
Posts: 15630
264
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because the return value simply is ignored and not used.
 
henry leu
Ranch Hand
Posts: 73
3
  • Mark post as helpful
  • send pies
  • 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?

 
Marshal
Posts: 25594
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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
Marshal
Posts: 15630
264
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • 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: 69411
276
  • Mark post as helpful
  • send pies
  • 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: 73
3
  • Mark post as helpful
  • send pies
  • 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.
 
Master Rancher
Posts: 3537
39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And?  I'm not sure why there's a "but" in that sentence.
 
henry leu
Ranch Hand
Posts: 73
3
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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!
 
Master Rancher
Posts: 170
24
Firefox Browser MySQL Database Java Ubuntu
  • Mark post as helpful
  • send pies
  • 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.



 
This is my favorite show. And this is my favorite tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic