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.
Expression in Java |
- Examples of expressions are in bold below:
int number=0;
anArray[0]=100;
System.out.println("Element1atindex0:"+anArray[0]);
intresult=1+2;//result is now
if(value1==value2)
System.out.println("value1==value2");
The data type of the value returned by an expression depends on the elements used in the expression.
The expression number=0 returns an int because the assignment operator returns a value of the same data type as its left-hand operand; in this case, number is an int.
As you can see from the other expressions, an expression can return other types of values as well,such as boolean or String.
The Java programming language allows you to construct compound expressions from various smaller expressions as long as the data type required by one part of the expression matches the data type of the other.
Here's an example of a compound expression: 1* 2 *3