Start learning free

LESSON 16 OF 410 · CALCULATIONS WITH NUMBERS

Area of a Rectangle in Java: width * height

Lesson Read the full explanation

We created two variables to store the width and the height of a rectangle.

On line 3, use these two variables and an arithmetic operation you just learned to print the area of the rectangle to the console.

STEP 1 OF 1
    Ctrl + Enter to check

    JAVA · CALCULATIONS WITH NUMBERS

    Area Of Rectangle in Java, explained

    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. widthheight is read as one unknown name, and width height is a syntax error. Always write width * height.
    • Printing text instead of the value. System.out.println("width * height") prints the characters width * height, because everything inside quotes is text. Leave the quotes off the expression.
    • Adding instead of multiplying. width + height gives 40, which is half the perimeter of this rectangle, not its area.
    • Mixing text and arithmetic without parentheses. "Area: " + width * height works, because * is evaluated before +. But "Sum: " + width + height prints Sum: 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 int holds values up to 2,147,483,647. 1_000_000 * 1_000_000 does not print 1000000000000. It silently wraps around and prints -727379968. For large sides, declare the variables as long.
    • Dividing integers for a fractional area. A triangle's area is half of width * height. With int values, width * height / 2 prints 195 and drops the half. Dividing by 2.0 prints 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 long instead of int.
    Keep going with JavaAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free