Unary Minus

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   hear noise   go to next page

Answer:

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

Unary Minus

If you look in the 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

is an expression that calls 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 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