• Post Reply Bookmark Topic Watch Topic
  • New Topic

forEach(IntConsumer action)  RSS feed

 
Jerry Ye
Greenhorn
Posts: 25
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I would like to ask in this code snippet, with value1 AtomicLong

value1.incrementAndGet returns a long. So here is in fact forEach(int -> long). Why does it compile while it doesn't work with(i->100)?
Thanks.
 
Junilu Lacar
Sheriff
Posts: 11481
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you be more specific about what isn't working? Does it not compile at all? Or are you getting unexpected results when you run? Please TellTheDetails
 
Jerry Ye
Greenhorn
Posts: 25
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Can you be more specific about what isn't working? Does it not compile at all? Or are you getting unexpected results when you run? Please TellTheDetails

Well, it compiles and works well, and that's my question. Why does it compiles?
 
Junilu Lacar
Sheriff
Posts: 11481
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was referring to the part where you said "it doesn't work with (i->100)" - what happens when you use that instead of what you say works?
 
Junilu Lacar
Sheriff
Posts: 11481
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think about though, which of the statements below will compile:

Assume that value1 is defined as you had it before.

[EDIT: modified code to make it more equivalent to what the lambda does]
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is not clear to me as well.

This code:

does not compile. NetBeans reports "int cannot be converted to void".

Now, that seems to imply that we can only use a method call, no matter if this method is a void method or that it returns some value. For instance:

Funny thing is that in the cases that do compile, NetBeans reports that the boxing to Integer is innecessary. If you then let NetBeans do what it suggests (i -> 100), it immediately reports that compiler error again.

In the first line, the return value is void, so it is clear why this is allowed. I do not know the difference between the second and the third case. I think we need some expert here to explain what is going on.
 
Junilu Lacar
Sheriff
Posts: 11481
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see the point of your question now.  I couldn't find any reference in the JLS after a quick search but it seems to me that the type inference rules for the lambda body is what's controlling this. The expression value1.incrementAndGet() returns a long but syntactically, it's a method invocation. In a normal block of code, you could call the method as I showed in my previous reply and ignore the value that was returned. That would make the method invocation still compatible with a void return type. That is, Java seems to be making a best-effort interpretation of the lambda expression as this:

With any constant expression, Java cannot make any best-effort interpretation that would even compile:

So Java has to interpret (i)->100 as

If you try to replace the lambda body with any method invocation that returns something/anything, like "X".concat("Y"), it works. For example, this works:

So, I guess the bottom line is, for the lambda body expression to be compatible with void, it has to be what the JLS calls "a statement expression or void-compatible" (I think one of the SO links in those search results has the reference to the relevant parts of the JLS)
 
Jerry Ye
Greenhorn
Posts: 25
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:I see the point of your question now.

Thanks for your answer. I think I understand now.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!