JAVA Expressions

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.

Continue reading JAVA Expressions