This week's book giveaway is in the HTML Pages with CSS and JavaScript forum.
We're giving away four copies of Testing JavaScript Applications and have Lucas da Costa on-line!
See this thread for details.
Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript 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
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Expression to statement conversion

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
The following two lambda expressions are the same:


This one is using a block statement, while the next one:


is using an expression.

I know that a Java expression consists of variables, operators, literals, and method calls. On the other hand,  each statement is a complete unit of execution.
I also know we can convert an expression into a statement by terminating the expression with a ;. These are known as expression statements.
But I am little unclear as to how all these work?


 
Marshal
Posts: 69847
278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving th‍read to our λs forum.

A. M. Anisul Huq wrote:. . . we can convert an expression into a statement by terminating the expression with a ;. . . .

No, you can't.In the case of a λ, you must write an expression of the appropriate type, or a method call, after the ->. If you write a statement, it must be enclosed in {...}.I think you would create controversy by calling System.out.println("CodeRanch") an expression, because it has no value nor a type. There are doubtless different meanings for the wo‍rd expression, and I can't remember whether a void method call fulfils the definition sed in the JLS (=Java® Language Specification).
 
Sheriff
Posts: 15801
264
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The JLS has an entire chapter on Expressions: https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html

And yes, there is such a thing as a void expression which can only be an invocation of a method that is declared void.

Also, if you look at the ExpressionStatement and StatementExpression productions listed here: https://docs.oracle.com/javase/specs/jls/se8/html/jls-14.html#jls-StatementExpression, it does in fact appear that what makes the method invocation expression a statement is the presence of a semicolon at the end of the method call. However, this is only true for method invocation expressions and other StatementExpressions; it won't work with any other kind of expression. Take for example the lambda expression in question. If you just add a semicolon after it to try to make it into a statement, you'll get a compiler error telling you that it's not a statement:

In the case of this:

the applicable productions here are LambdaExpression and LambdaBody. The { System.out.println(msg); } part is what constitutes the LambdaBody (a Block).

In the case of this:

you have a LambdaExpression whose LambdaBody is a MethodInvocation expression.

As this example shows, if you want to understand how the compiler interprets all these things, you'll need to understand a little bit about parsing, how compilers do it, and how the productions like those listed in the JLS determine how the program is broken down into its different syntactical parts.
 
Saloon Keeper
Posts: 12154
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:I think you would create controversy by calling System.out.println("CodeRanch") an expression


Yet, technically it is. It's a method invocation, and method invocations are part of a class of expressions called "statement expressions". Its return type is void.
 
Without deviation from the norm, progress is not possible - Zappa. Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic