- Page ID
So far we have looked at the elements of a programming language – variables, expressions, and statements – in isolation, without talking about how to put them together.
One of the most useful features of programming languages is their ability to take small building blocks and compose them. For example, we know how to multiply numbers and we know how to display values. We can combine these operations into a single statement:
System.out.println(17 * 3);
Any arithmetic expression can be used inside a print statement. We’ve already seen one example:
System.out.println(hour * 60 + minute);
You can also put arbitrary expressions on the right side of an assignment:
int percentage; percentage = (minute * 100) / 60;
The left side of an assignment must be a variable name, not an expression. That’s because the left side indicates where the result will be stored, and expressions do not represent storage locations.
hour = minute + 1; // correct minute + 1 = hour; // compiler error
The ability to compose operations may not seem impressive now, but we will see examples later on that allow us to write complex computations neatly and concisely. But don’t get too carried away. Large, complex expressions can be hard to read and debug.