I'm a beginner for Java, and I want to know the difference between expressions and statements in Java?
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.
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.
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
).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With