java featued image art

1.12 Java | Expression Evaluation, Operator Augmentation, Increments & Decrements

Expression Evaluation

Java expressions and Arithmetic expressions evaluate in the same way.

So the rules of arithmetic apply the same when used in Java. The only difference is the format. For example the arithmetic expression:

(2 + 3x)/4 - 5(y-6)(a + b + c)/x + 7(8+x)/y

translates to the Java expression (2 + 3x)/4 - 5(y-6)(a + b + c)/x + 7(8+x)/y .

As I said before, the same arithmetic rules apply for Java expressions. So here are the rule that you need to know:

Order of evaluations:

  1. What’s inside the parenthesis must be evaluated first.
  2. Parenthesis can be nested so that the inner parenthesis are evaluated first.
  3. Multiplication, Division and Remainder operators are evaluated after
  4. Lastly, addition and subtraction operators are evaluated last.

Assignment Operator Augmentation

Combine +, -, *, /, and % Operators with the Assignment operator to make an augmented operator.

It is a common occurrence in many Java programs that the value of a variable is used, changed, but then reassigned back to the same variable. For example, here’s a statement that increases a variables count by 1:

In Java you can combine the addition operator and the assignment operator into an augmented operator to do and equal the same as the statement above. Continuing the example:

+= is called an addition assignment operator. Down below I show the other augmented assignment operators:

Notice that augmented operators is done after all other operations are evaluated in an expression. For example, this:

means this:

Also note that augmented assignment operators don’t have spaces in between them. So + = is wrong, it should be +=.

Increment & Decrement Operators

You can increase a variable by 1 with the increment operator ++, or decrease by 1 with the decrement operator --. These operators are extremely useful because in many programming tasks, you only need to change a variables value by 1, like with Loops. Here are example of incrementation and decrementation operations:

When the increment and decrement operators are placed after a variable, they are called posfix increments/decrements; like in the above example. When increment and decrement operators are placed before a variable, they are called prefix increments/decrements, as I show in the below example:

Note that you see the same effect regardless of the variables and operators order in these examples. However the order of variable and operator do have different effects in other situations:

Example 1

Here’s an example showing how prefixing the operator is significantly different from postfixing the operator:

Which is the same as:


Results: x is 6, new number is 25

Notice that switching the order of the increment operator to prefix changes the evaluation:

Which is the same as:

Notice that the incrementation occurred before the evaluation, meaning that the output is affected:

x is 6, new number is 30
Notice that the new number is 30, not 25.

Example 2

Here’s another example that tests whether you have understood how increment & dercrement operators work with prefix & postfix positions:

After all 3 lines are executed, a = 0.0, b = 6.0, and c = 7.0.

What's Your Opinion?