Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Strange Statement about Lambda in Oracle Java Tutorial

 
Ranch Foreman
Posts: 122
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found the following statement from Syntax of Lambda Expressions in the Oracle Java tutorial is strange. Other online sources and books also mentioned the same thing. I assume they are all copied from the Oracle Java tutorial.

Oracle's Java Tutorial wrote:However, you do not have to enclose a void method invocation in braces. For example, the following is a valid lambda expression:

email -> System.out.println(email)


I'm not saying this is wrong. I'm saying this is strange, because single line method invocation is common, no matter it returns anything or not, you always don't need to enclose it in braces.

Why do they consider "void method invocation" as a special case?
 
Marshal
Posts: 25961
70
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's a special case when compared to the sentence immediately before that:

The tutorial wrote:A return statement is not an expression; in a lambda expression, you must enclose statements in braces ({}).



So a void method invocation is a statement, but it's a special kind of statement in this context. Unlike other types of statement, you don't need braces around it.
 
Marshal
Posts: 70684
288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:. . . a special kind of statement . . .

Doesn't that mean it is a statement expression as mentioned in FM's other post from this morning?
 
Saloon Keeper
Posts: 12431
269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. Statement expressions are expressions that can be used as statements by terminating them with a semi-colon.

While a method invocation is indeed a statement expression, statement expressions have nothing to do with what is valid as a lambda expression body. Any type of expression may be used as a lambda expression body.

Frank is correct. It is indeed strange to make a special case for void returning method invocations. Method invocations are always expressions, no matter whether they return void or not. It is also not a special case when compared to the previous sentence from the tutorial: The tutorial says that return statements are not expressions. It does not say that void returning methods invocations are not expressions.
 
Campbell Ritchie
Marshal
Posts: 70684
288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you; it is surprising how many little errors like that you can find in those tutorials. And it would be nice if they updated them from Java8.
 
Stephan van Hulst
Saloon Keeper
Posts: 12431
269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it's all part of Oracle's evil plan to sell more trainings.
 
Master Rancher
Posts: 3701
44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But it's not an error... it's just un unnecessary restatement.  But remember, this is a tutorial, for people who may be learning this stuff for the first time.  They often restate things in ways that may seem unnecessary.  If you got it the first time, great, just ignore the restatement.  They had just been talking about how a non-void expression could be replaced with a block with a return; they probably figured some readers might be wondering, having forgotten, what about void expressions?  If anything, I would have expected them to go a bit further, showing that the void expression could also be replaced by an equivalent block, just to complete the parallel with what they just showed about a non-void expression.  Yeah, they could have explained better... but that's different from being wrong.
 
Paul Clapham
Marshal
Posts: 25961
70
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mike Simmons wrote:But it's not an error... it's just un unnecessary restatement.  But remember, this is a tutorial, for people who may be learning this stuff for the first time.



Yes; when I was first trying to use lambda expressions I had an awful lot of extra braces. For me, when learning this sort of thing it's more useful to look at code examples and intuit what's okay and what's not, rather than trying to learn the differences (or not) between a statement and an expression and a statement expression.
 
Frank Mi
Ranch Foreman
Posts: 122
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mike Simmons wrote:But it's not an error... it's just un unnecessary restatement.  But remember, this is a tutorial, for people who may be learning this stuff for the first time.


I would say, that is really confusing. Not only for starters like me, but also for some advanced experts. For example, the book Modern Java in Action specifically took this out and tried to explain it, and the conclusion is shown below (which doesn't look correct):

Modern Java in Action wrote:LAMBDAS AND VOID METHOD INVOCATION
...
After all, System.out.println returns void so this is clearly not an expression! Why don’t we have to enclose the body with curly braces like this?
       process(() -> { System.out.println("This is awesome"); });
It turns out that there’s a special rule for a void method invocation defined in the Java Language Specification. You don’t have to enclose a single void method invocation in braces.


I believe the authors of the book read the same Java tutorial and were confused the same way, otherwise they don't even have to mention this.
 
Oh sure, it's a tiny ad, but under the right circumstances, it gets bigger.
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic