Expressions

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

(Review: ) Is the following correct?

13 * 6 -

Answer:

No.

Expressions

The literals (the integers) and the operators are out of order in the above incorrect expression. (One legal arrangement is     13 * -6    for 13 times minus 6.)

An expression is a combination of literals, operators, variable names, and parentheses used to calculate a value.

Expressions contain operators and operands. You already know what an operator is (a symbol such as +, -, *, or / that calls for an arithmetic operation).

An operand is a value that is acted upon by an operator.

The parts of an expression must be arranged correctly. The syntax of Java describes the correct arrangements. The rules for correct Java expressions are about the same as for algebra. Essentially,

  1. Each operator must have the correct number of operands.
    • Multiplication *, Division /, Addition +, Subtraction - should have two operands, one on each side.
    • Negation - and unary plus + should be followed by one operand.
  2. Parentheses () can surround a legal expression to make it an operand.

In Java expressions, operators and operands must be explicit. In 13 - 5 the 13 and the 5 are the operands and the - is the operator.

In 14*sum 14 and sum are the operands. The star for multiplication must be present. The expression 14sum is incorrect.

The details of expression syntax are in the Java documentation, should you care to dig through it. The best way to learn these rules is to study some examples.

QUESTION 2:

Which minus sign in the following stands for "subtraction" and which stands for "negation"?

-23 - 3