• Post Reply Bookmark Topic Watch Topic
  • New Topic

What is an expression?  RSS feed

 
Cedric Bosch
Ranch Hand
Posts: 99
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd like to know what is an expression exactly and does it return a value always ?

Mainly I'd like to know if this :

int i = a; is an expression ?
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the detailed explanation from oracleExpression.

Praveen
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As a gross oversimplification, an expression is anything you can put on the right of a = sign.
No,
int i = a;
isn't an expression. It is defined in Java® as a declaration (because you are introducing a new variable: that is the part left of the =) and initialisation (because you are giving the variable a value immediately: that is the rest of the line) and a is an expression.
Later on, if you write
i = j + 3;
then j + 3 is an expression, where 3 is a value and j is a variable. But j and 3 are themselves smaller expressions, and you can use i as an expression elsewhere.
In the link PK gave,
new Bicycle()
is also an expression, but a different type representing something of type Bicycle rather than a number as above.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cedric Bosch wrote:I'd like to know what is an expression exactly and does it return a value always ?

Yes, an expression always evaluates to a value.
 
Cedric Bosch
Ranch Hand
Posts: 99
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:
Cedric Bosch wrote:I'd like to know what is an expression exactly and does it return a value always ?

Yes, an expression always evaluates to a value.


No it does not, calling a method returning void does not return anything.

Anyway I'm caught up in trying to understand what "returning a value" actually means. I mean on a low level what does it actually mean ?

this code:



will compile to this (if you prevent weird optimizations):



There is nothing in the bytecode that hints to a returned value. I don't get it. What am I  not understanding here ?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The Java Language Specification states that an expression, when evaluated, results in either (1) a value, (2) a variable (aka. a lvalue), or (3) nothing (in the case of void). This is differentiated from other statements (such as declaration) which doesn't have a result (not even a void result).

Henry
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think you didn't took the lesson(the link i have given) yet.
method and expression are 2 different things.yes you are correct that method can return with or without any value.but this is entirely another thing.sometimes you can use them too to build an expression like suppose you have a method which returns a "int",say the method-expDemo().you can build an expression like
int exp = expDemo()
but you will not find any void returning methods in an expression.
i will recommend you read the description in the link,that's a good tutorial.

Hope it helps!

Kind Regards,
Praveen.
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
The Java Language Specification states that an expression, when evaluated, results in either (1) a value, (2) a variable (aka. a lvalue), or (3) nothing (in the case of void). This is differentiated from other statements (such as declaration) which doesn't have a result (not even a void result). Henry

Henry can you please exemplify it for the void type expressions.i don't know about it.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
praveen kumaar wrote:
Henry can you please exemplify it for the void type expressions.i don't know about it.


Cedric already mentioned it. A call to a void method, results in a void. Of course, since you really can't do anything else with a void result, the expression can't really get any more complicated that a single method call...

Henry
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry wrote:Cedric already mentioned it. A call to a void method, results in a void. Of course, since you really can't do anything else with a void result, the expression can't really get any more complicated that a single method call...

.
just now i have read the specification.
Got it! Thanks.
 
Cedric Bosch
Ranch Hand
Posts: 99
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
praveen kumaar wrote:
i will recommend you read the description in the link,that's a good tutorial.


I've already read it and I also read some of the spec on expression, but the answer wasn't clear to me ence why I posted here.
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cedric wrote:I've already read it and I also read some of the spec on expression, but the answer wasn't clear to me ence why I posted here.

Fine,you are at the right place.is the topic clear now.please ignore my previous post(i have some confusion while i am posting,henry clears it).

Kind Regards,
Praveen
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cedric Bosch wrote:Anyway I'm caught up in trying to understand what "returning a value" actually means. I mean on a low level what does it actually mean ?

this code: ...

There is nothing in the bytecode that hints to a returned value. I don't get it. What am I  not understanding here ?

See https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.1 -- if you read that section carefully, you might notice the use of the word "denote," as in "the result denotes ..." and "An expression denotes nothing if and only if ..." and "every other context in which an expression can appear requires the expression to denote something."  Your confusion probably stems from thinking about an expression as returning something rather than as expressions denoting something or nothing.
 
Achyut Sharma
Greenhorn
Posts: 8
1
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
double kilometers = (100 * 1.609344);

Here, double is  a datatype (which is used to create a valid java statement)  and everything else except "double" in this case is an "expression"
I hope it helps,although i'm new at Java.
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Yes, kilometres, 10 and 1.609344 are expressions. The = * ( and ) tokens are operators or separators and double is a type.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!