Variables and Assignment Statements

Read this chapter, which covers variables and arithmetic operations and order precedence in Java.

24. Unary Minus


Answer:

    2 + 4/2 + 1 --- 2 + 2 + 1 ------ 4 + 1 --------- 5

Unary Minus

If you look in the operators, table of table of operators you will see that the character - is listed twice. That is because - is used for two purposes. In some contexts, - is the unary minus operator. In other contexts, - is the subtraction operator.

The unary minus is used to show a negative number. For example:

-97.34

means "negative ninety seven point thirty four." The subtraction operator is used to show a subtraction of one number from another. For example:

95 - 12

asks for 12 to be subtracted from 95.

The unary minus operator has high precedence. Addition and subtraction have low precedence. For example

-12 + 3

means add 3 to negative 12 (resulting in -9). The unary minus is done first, so it applies only to the twelve.

unary plus + can be applied to a number to show that it is positive. It also has high precedence. It is rarely used.


Question 25:

What is the value of the following expression?

    +24 + 3 * -4