Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between an expression and a statement in Java? [closed]

Tags:

java

I'm a beginner for Java, and I want to know the difference between expressions and statements in Java?

like image 475
lxgeek Avatar asked Sep 16 '16 03:09

lxgeek


People also ask

What is the difference between expression and statement?

In programming language terminology, an “expression” is a combination of values and functions that are combined and interpreted by the compiler to create a new value, as opposed to a “statement” which is just a standalone unit of execution and doesn't return anything.

What is an expression statement in Java?

Expression Statements Expression is an essential building block of any Java program. Generally, it is used to generate a new value. Sometimes, we can also assign a value to a variable. In Java, expression is the combination of values, variables, operators, and method calls.


2 Answers

This is an example :

b + 1 is an expression while a = b + 1; is a statement. A statement consists of expressions.

This is not specific to the Java language. Many languages use this kind of grammar e.g. C, C++, Basic etc (not SQL).

like image 168
fastcodejava Avatar answered Oct 14 '22 04:10

fastcodejava


From Javadoc,

Expression

An expression is a construct made up of variables, operators, and method invocations, which are constructed according to the syntax of the language, that evaluates to a single value.

For example,

int cadence = 0;

The data type of the value returned by an expression depends on the elements used in the expression. The expression cadence = 0 returns an int because the assignment operator returns a value of the same data type as its left-hand operand; in this case, cadence is an int.

Statement

Statements are roughly equivalent to sentences in natural languages. A statement forms a complete unit of execution.

like image 23
Somnath Musib Avatar answered Oct 14 '22 04:10

Somnath Musib