• 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
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

ForEach

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

I am aware that forEach method takes only consumer but the below code the takes a submit method and it returns a value.
I expected a compiler error. But it compiled. Can someone please help me understand the same.




Thanks in Advance!!
 
Saloon Keeper
Posts: 1646
61
Eclipse IDE Postgres Database C++ Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Priyanka:

It is late and I am tired, but this is something that weirded me out for a while if I am not mistaken due to the foregoing reasons.

This is not specific to the submit() call, there are rules around return values in lambdas and method references that might or might not always seem to make sense but "They are the Rules"...

Are you used to the following situations?

These are errors (as you expect):
jshell> Consumer<Integer> ci = i -> 2 * i;
|  Error:
|  incompatible types: lambda body is not compatible with a void functional interface
|      (consider using a block lambda body, or use a statement expression instead)
|  Consumer<Integer> ci = i -> 2 * i;
|                              ^---^

jshell> Consumer<Integer> ci = i -> { return 2 * i;} ;
|  Error:
|  incompatible types: bad return type in lambda expression
|      unexpected return value
|  Consumer<Integer> ci = i -> { return 2 * i;} ;
|                                       ^---^


But these two which seem similar to you and me are NOT legal EDIT: NOT ILLEGAL -- (my wife and dog were demanding I get to bed for the night!)
jshell> class Example {
  ...>     static int returns( Integer i ) { return 2 * i;}
  ...> }
|  created class Example

jshell> Consumer<Integer> ci = i -> Example.returns(i) ;
ci ==> $Lambda$21/0x0000000800c0b000@28c97a5

jshell> Consumer<Integer> ci = Example::returns;
ci ==> $Lambda$20/0x0000000800c0a800@69663380

Now, as you expect, the value gets thrown away before we can see it:
jshell> System.out.println( ci.accept(42) );
|  Error:
|  'void' type not allowed here
|  System.out.println( ci.accept(42) );
|                      ^-----------^

I am too tired to be sure this addresses your doubt, but I think it does.
If so, you see that the submit and execute are maybe just one example of this behavior.
 
Sheriff
Posts: 16669
278
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

Priyanka Leo wrote:the below code the takes a submit method and it returns a value.


Are you sure about that? What makes you think it returns a value? The submit() method is overloaded, you know. Which of those overloaded versions do you think the code resolves to?
 
Junilu Lacar
Sheriff
Posts: 16669
278
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
Consider this from the JLS (Chapter 15):

Generally speaking, a lambda of the form () -> expr, where expr is a statement expression, is interpreted as either () -> { return expr; } or () -> { expr; }, depending on the target type.

 
Jesse Silverman
Saloon Keeper
Posts: 1646
61
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi:

I haven't formally studied this stuff yet, but don't all 3 overloads of .submit() return a Future?

https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/concurrent/ExecutorService.html

The difference in behavior between the overloads of .submit() refer to what the .get() call on the Future will return, no?

Additionally, the first parameter to .submit() takes either a Runnable or a Callalble<T>...that is different.
 
Saloon Keeper
Posts: 24558
168
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

A little indentation can make things a LOT easier to read!
 
Jesse Silverman
Saloon Keeper
Posts: 1646
61
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:
A little indentation can make things a LOT easier to read!



Strongly agreed!
I believe my first response and Junilu's second went to the heart of what was confusing her, this was another fun question that is about more than one topic at the same time.  Cool and interesting, but a bit stressful with 108 seconds to dedicate to each one if you can go 90 minutes without blinking, drinking or a pause in your thinking, less than that otherwise.

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic