2.6.2 - Arithmetic Operators

2.6.2.1 - Unary Arithmetic Operators

2.6.2.1.1 - Unary Plus Operator +

The unary plus is an unary operator acting upon a single operand. The action mechanism is illustrated below:

unary plus operator Operand;

Note that the statement of unary plus is terminated by a semi-colon(;).

Unary plus operator acts from right to left; His action is exacly equivalent to his algebraic counterpart., namely the plus sign. As in the algebra, all unsigned numbers are considered positive numbers. Therefore, the action of the unary plus operator has no effect upon his operand and there is no reason of his application.

2.6.2.1.2 - Unary Minus Operator-

The unary minus is an unary operator acting upon a single operand. The action mechanism is illustrated below:

unary minus operator Operand;

Action of the unary minus operator changes the sign of his operand.
This action is an important impact upon the operand and changes the
state of the operand and respectively the state of the system. We have
mentioned before the meaning of the **State of the
System**, but we repeat again, this is an I.T term indicating
actual values of the system elements. When the value of any system
elements changes, for example the value of a variable changes, the
state of the system change to a new state, determined by the actual
values of the system elements. An example of the action of the unary
minus operator is given below:

package preliminary; public class UnaryMinus { public static void main(String[] args){ double height = 9.45; Height = -height; // Do not confuse with the shortcut assignment operator -= !!! System.out.println("Height under the street = " + height ); } // end main } //end class Unary minus

The results of this program:

```
Height under the street = -9.45
```

It is clearly visible that the application of the unary minus operator results the sign change of his operand, exacly like his algebraic counterpart.

2.6.2.1.3 - Unary Incrementing / Decrementing Operators ++/--

There is four unary incrementing/decrementing operators. These are shown in the table below:

Name | Action | Result |
---|---|---|

unary prefix decrement operator. | --operand | decrement the value of the operand by 1; |

unary postfix decrement operator. | operand-- | decrement the value of the operand by 1; |

unary prefix increment operator. | ++operand | increment the value of the operand by 1; |

unary postfix increment operator. | operand++ | increment the value of the operand by 1; |

Prefix unary incrementing or decrementing operators may be used in a single statement like below:

++<integer variable>; or, --<integer variable>;

Postfix unary incrementing or decrementing operators may be used in a single statement like below:

<integer variable>++;; or, <integer variable>--;;

Used in a single statement, either in prefix or postfix form, they return the same value, i.e incrementing or decrementing the value of their integer operand by one. Example of application of unary incrementing or decrementing operators in a single statement is given below:

package preliminary;

public class UnaryOperators {

public static void main (String[] args) { { int i = 0; System.out.println("Initial value of i : " + i); ++i; System.out.println("Value of i, after applying unary prefix incrementing operator as ++i; : " + i); }

{ int i = 0; System.out.println("Initial value of i : " + i); --i; System.out.println("Value of i, after applying unary prefix decrementing operator as --i; : " + i); }

{ int i = 0; System.out.println("Initial value of i : " + i); i++; System.out.println("Value of i, after applying unary postfix incrementing operator as i++; : " + i); }

{ int i = 0; System.out.println("Initial value of i : " + i); i--; System.out.println("Value of i, after applying unary postfix decrementing operator as --i; : " + i); } } }

The results of this program:

Initial value of i : 0 Value of i, after applying unary prefix incrementing operator as ++i; : 1 Initial value of i : 0 Value of i, after applying unary prefix decrementing operator as --i; : -1 Initial value of i : 0 Value of i, after applying unary postfix incrementing operator as i++; : 1 Initial value of i : 0 Value of i, after applying unary post decrementing operator as --i; : -1

The inspection of the results of this program show clearly that the results of application of the unary incrementing operators in a single statement either in pre or postfix form, results with the incrementing or decrementing the value of the variable by 1. Therefore, if the initial value of an int type integer variable is 1, applying either ++i; or i++; result always to i = i + 1 and the final value of the variable i will always be 2. Respectively, when the value of the variable i is 1, applying --i; or i--; to this variable will always result to i = i - 1; and the final value of i, will always be 0. Note that in common practice, we apply normally postfix forms as i++; or i--;

The application of unary incrementing/decrementing operators with other operators gives different results for their application in pre or postfix form. As an example we can look the assignment results of some expressions involving unary incrementing operators.

package preliminary; public class UnaryPrefixIncrementingAndAssignment { public static void main(String[] args){ int brick = 0; int dozen = 0; brick = ++dozen; System.out.println("Number of Bricks = " + brick + " Dozen(s). "); } // end main } //end class UnaryPrefixIncrementingAndAssignment

The result of this program :

Number of Bricks = 1 Dozen(s).

The results are not surprising, because the what was intended is achieved. In the expression brick = ++dozen, first incrementing of the right variable is executed then the assignment takes place. The assigned value and the actual value of the right operand is the same incremented value.

In the prefix form, incrementation (decrermentation) of the right operand in assignment statement, the right operand is invremented /decremented and resulting value is assigned to the left operand. In short, in prefix form, first incrementation then assignment, we should memorize this.

Now, we'll try the postfix form.

package preliminary; public class UnaryPostfixIncrementingAndAssignment { public static void main(String[] args){ int brick = 0; int dozen = 0; brick = dozen++; System.out.println("Number of Bricks = " + brick + " Dozen(s). "); } // end main } //end class UnaryPostfixIncrementingAndAssignment

The result of this program :

Number of Bricks = 0 Dozen(s). Actual Value of the Dozen = 1

That is surprising of course. The intention of the statement was the incrementing the variable and assigning him destructively to the left side. But, the obtained result is the assignment before incrementation and incrementation after the assignment. Attention to the fact that, by the termination of the statement, the actual value of the right operand is the incremented/decremented value.

Postfix and prefix forms behave differently in expressions and the result is difficult to predict especially with expressions with many operators. The advice is not using unary decrementing or incrementing operators in expressions having more than one operator. These are applied more safely in the expressions where solely a prefix or postfix unary incrementation/decrementation is intenteded as,

i++; or --i;

A last warning about incrementing/decrementing operators, that there might be a danger of overflow in incrementing and underflow in decrementing. Programmers should consider this possibility and take appropriate measures in their programs.