AI Hints
Home›Java Tutorial›double and float, Arithmetic, Division and Remainder
Java · Lesson 14 of 120

Arithmetic

Plus, minus, times.

⏱ 2 min read▶ Video at 18:04🎓 Free
Watch this lesson in the Java courseWatch on YouTube ↗Full Java course

Java does maths with the usual symbols. Plus, minus, star for times, and slash for divide. And it follows the order you learned at school.

  • + - * /
  • Like school maths
  • Order matters

The operators

A is seventeen, and b is five. We add, subtract, and multiply them. Times happens before plus, so two plus three times four is fourteen. Brackets change the order. Run it. Twenty two, twelve, eighty five, fourteen, and with brackets, twenty.

Main.javajava
public class Main {
    public static void main(String[] args) {
        int a = 17;
        int b = 5;
        System.out.println("a + b = " + (a + b));
        System.out.println("a - b = " + (a - b));
        System.out.println("a * b = " + (a * b));
        System.out.println("2 + 3 * 4 = " + (2 + 3 * 4));
        System.out.println("(2 + 3) * 4 = " + ((2 + 3) * 4));
    }
}
Output
a + b = 22
a - b = 12
a * b = 85
2 + 3 * 4 = 14
(2 + 3) * 4 = 20

Shortcuts

Plus equals five, adds five to points. Minus equals, and times equals, work the same way. Plus plus adds one. Minus minus takes one away. Run it. Fifteen, twelve, twenty four, twenty five, and back to twenty four.

Main.javajava
public class Main {
    public static void main(String[] args) {
        int points = 10;
        points += 5;
        System.out.println(points);
        points -= 3;
        System.out.println(points);
        points *= 2;
        System.out.println(points);
        points++;
        System.out.println(points);
        points--;
        System.out.println(points);
    }
}
Output
15
12
24
25
24

Operator summary

Here are all the shortcuts together. You'll see plus plus, all the time, in loops. These shortcuts are just shorter ways to write the same thing.

keyvalue
x += 5x = x + 5
x -= 3x = x - 3
x *= 2x = x * 2
x++ / x--add one / take one

Summary

Java uses plus, minus, star, and slash, with times before plus. Use brackets to change the order. Next, division, and the remainder.

Key takeaways
  • + - * /
  • * before +
  • +=, ++, --
Next up: Division and Remainder — surprises with whole numbers