Let’s first look at expressions. An expression is a program construct defined according to the syntax of the programming language, which uses variables, operators, and methods. Let’s now look at some examples of expressions:
int category_var = 0;
cat_Array[0]=200;
Now you can use these variables in the following expression:
System.out.println(“An element 1 at index 0: ” + cat_Array[0]);
int result_operator = 22 + 40; // result is now 62
The data type of resultant value returned by an expression depends on the data types of the operand used in the expression. In the example shown above, the data type of the expression category_var = 0 return an int. This is because the assignment operator returns the value of int data type. Apart from int, the expressions can also return data of other types also such as Boolean or String.
Not only this, you can also construct compound expressions in the Java programming language. These compound expressions can be constructed by combining a number of smaller expressions. The only condition being the data type of the left side of the expression should match the data type of the right type.
The following code snippet displays the following compound expression:
3 * 4 * 5
In this example, apart from the product obtained from this expression, it is also important to note the order in which this expression is evaluated by the compiler. The outcome or the product of the expression always remains same irrespective of the order in which they appear in the expression.
For example, in the expression given below, the result or the outcome would vary depending on the use or evaluation of the division or the addition operator.
Num_var_1 + Num_var_2 / 25 // an unclear expression
The use of parenthesis in such expression makes them clear and helps determine the order of precedence.
Let’s see how the use of parenthesis helps in making the above unclear expression clear.
(Num_var_1 + Num_var_2) / 25 // use of parenthesis to rectify an unclear statement
The operator precedence defined within an expression takes precedence and the output is determined in that manner, if you don’t explicitly define the order of operators. As you’ve already learnt operators having higher precedence are evaluated before the others. In the expression we just saw, the division operator would be evaluated before the addition operator. Therefore, the result or the output of the following two statements would be the same:
Num_var_1 + Num_var_2 / 10
Num_var_1 + (Num_var_2 / 25) // use of parenthesis to rectify an unclear statement
When defining compound expressions, it is important to explicitly define the order of operator precedence with the use of parenthesis. It will also help a programmer to make the code more readable and maintainable.
You can convert expressions into a statement by terminating the expression with a semicolon (;). Some such expressions are:
- Assignment expressions
- Use of ++ or –
- Method invocations
- Object creation expressions
These statements are known as expression statements. Some examples of expression statements are:
Sample_Value_var = 9834.435;
Sample_Value_var++;
System.out.println(“Hello! How are you?”);