Sub topic 1
Values, Operators, and Expressions
Java programs work with values. A value is a piece of data, such as the number 4 or the truth value true. A variable is a named place that holds a value.
An expression is a piece of code that Java evaluates. To evaluate an expression means to work out its result. Many expressions produce a value that the program can use.
For example, if a variable named apples holds 4, the expression apples + 3 produces the value 7.
An operator is a symbol that tells Java to perform an operation. In apples + 3, the operator is +, which adds numbers. An operand is a value or expression that an operator uses. Here, apples and 3 are the operands.
This chapter assumes you can write and run a simple Java program and have used variables and basic data types.
Reading a simple expression
Consider this expression:
apples + extra
If apples holds 4 and extra holds 3, Java uses those values to work out the result:
applesprovides the value4.extraprovides the value3.- The
+operator adds the values. - The expression produces
7.
The result is a value, not a new piece of code. The expression can be used wherever Java expects a suitable value, such as in a variable declaration.
A literal is a value written directly in code. For example, 3 and true are literals. A literal can be an expression by itself. A variable name, such as apples, can also be an expression: it produces the value held by that variable.
| Expression | Operator | Operands | Result when `apples` is `4` |
|---|---|---|---|
| `3` | None | None | `3` |
| `apples` | None | None | `4` |
| `apples + 3` | `+` | `apples`, `3` | `7` |
| `apples * 2` | `*` | `apples`, `2` | `8` |
The * operator multiplies numbers. An expression does not need an operator: 3 and apples are expressions too.
Try a complete program
Save the following code in a file named ExpressionDemo.java:
public class ExpressionDemo {
public static void main(String[] args) {
int apples = 4;
int extra = 3;
int total = apples + extra;
int doubled = total * 2;
boolean enough = total > 5;
System.out.println(total);
System.out.println(doubled);
System.out.println(enough);
}
}Run it from the folder containing the file:
javac ExpressionDemo.java
java ExpressionDemoThe program prints three lines:
714true
In the declaration int total = apples + extra;, the expression apples + extra produces 7. That value is stored in total.
Next, total * 2 uses the value 7 and produces 14. The program stores that result in doubled.
Finally, total > 5 asks whether total is greater than 5. Because 7 is greater than 5, the expression produces true. The program stores that result in enough.
The program then prints the stored values. A method is a named action in a program. A method call runs that action. Here, System.out.println(total) calls a method that prints the value of total.
Expression results have types
A type tells Java what kind of value it is working with. For example, int is a type for whole numbers, and boolean is a type with two possible values: true and false.
The expressions in the example produce different types of results:
| Expression | Result | Result type |
|---|---|---|
| `apples + extra` | `7` | `int` |
| `total * 2` | `14` | `int` |
| `total > 5` | `true` | `boolean` |
The type matters when you store a result. The declaration boolean enough = total > 5; works because the expression produces a boolean value.
By contrast, int enough = total > 5; would cause a compile error. A compile error means Java cannot turn the code into a runnable program. The expression produces true or false, but the variable is declared to hold an int.
Expressions and statements
A statement is a complete instruction in a Java program. A statement can contain an expression.
Look again at this declaration:
int total = apples + extra;
The whole line is a variable declaration statement. The part apples + extra is an expression within it. The statement declares total and gives it the expression’s result.
The semicolon marks the end of the statement. It is not part of the expression apples + extra.
Not every expression can stand alone as a statement. For example, apples + extra is a valid expression, but writing apples + extra; on its own is not a valid Java statement. In the program, the result is used to give total a value.
Expressions can also appear inside other expressions. In apples + extra, both apples and extra are expressions used as operands. This lets Java build a larger calculation from smaller pieces.
Most examples in this chapter produce a value that can be used. Some method calls are also expressions, but do not produce a usable value. For example, the println calls in the program perform the action of printing rather than providing a value to store.
Check your understanding
When you see an expression, identify each part before working out the result. Suppose items holds 6 and boxes holds 2.
For items + boxes:
- The operator is
+. - The operands are
itemsandboxes. - The values used are
6and2. - The result is
8, with typeint.
The names help a reader understand what the values represent. The operator says what to do with them.
Exercise
If score holds the int value 8, what are the operator, operands, result, and result type of score + 2?
Show answer
The operator is +, the operands are score and 2, and the result is 10 with type int.
Exercise
In boolean ready = score > 5;, which part is the expression, and what does it produce when score is 8?
Show answer
score > 5 is the expression. It produces the boolean value true.
Exercise
Is 7 an expression even though it has no operator?
Show answer
Yes. It is a literal expression that produces the value 7.