The task
The exercise starts with two variables, width and height, that hold the sides of a rectangle. Your job is a single line: use an arithmetic operation to print the area of the rectangle to the console. The area of a rectangle is its width multiplied by its height, and in Java multiplication is the * operator.
int width = 23;
int height = 17;
These are int variables, so they hold whole numbers.
Calculating and printing the area
You can pass an expression straight to System.out.println. Java evaluates width * height first and prints the result, so there is no need to store the area in a variable:
int width = 23;
int height = 17;
System.out.println(width * height);
391
Check it by hand: 23 * 17 = 23 * 10 + 23 * 7 = 230 + 161 = 391.
If you prefer to name the result, use a variable. It is the same calculation, and it reads well once a program gets longer:
int width = 23;
int height = 17;
int area = width * height;
System.out.println("Area: " + area);
Area: 391
Here + joins the text and the number. Because the multiplication is stored in area first, there is nothing to get wrong with the order of operations.
A complete program
Outside the lesson, a Java program needs a class and a main method. A program that asks for the area of a rectangle, which many beginner courses set as a first exercise, looks like this:
public class Main {
public static void main(String[] args) {
int width = 23;
int height = 17;
System.out.println(width * height);
}
}
Change the two values at the top to calculate the area of any other rectangle. To read the sides from the keyboard you would use a Scanner, but this lesson only practises the arithmetic.
Common mistakes
- Leaving out the operator. Java has no implicit multiplication.
widthheightis read as one unknown name, andwidth heightis a syntax error. Always writewidth * height. - Printing text instead of the value.
System.out.println("width * height")prints the characterswidth * height, because everything inside quotes is text. Leave the quotes off the expression. - Adding instead of multiplying.
width + heightgives 40, which is half the perimeter of this rectangle, not its area. - Mixing text and arithmetic without parentheses.
"Area: " + width * heightworks, because*is evaluated before+. But"Sum: " + width + heightprintsSum: 2317, not 40, because the numbers are appended to the text one after the other. Add parentheses, as in"Sum: " + (width + height). - Overflow with big sides. An
intholds values up to 2,147,483,647.1_000_000 * 1_000_000does not print 1000000000000. It silently wraps around and prints -727379968. For large sides, declare the variables aslong. - Dividing integers for a fractional area. A triangle's area is half of
width * height. Withintvalues,width * height / 2prints 195 and drops the half. Dividing by2.0prints 195.5.
Key takeaways
- The area of a rectangle is
width * height, and*is the multiplication operator in Java. System.out.println(width * height);evaluates the expression and prints the result on its own line.- Text goes in quotes and expressions do not. Quoting an expression prints it as text.
- For very large values, use
longinstead ofint.
