Numbers & Math
7 min · Java
Do math with int variables and the basic operators.
Enemy 1 of 5
This code stores a price and a quantity in int variables, multiplies them, and prints the total. Press Run to see 15.
Why this works
Whole numbers use the int type (short for "integer"). The basic arithmetic operators are + (add), - (subtract), * (multiply), and / (divide), and Java follows the same order of operations you'd expect from math class: multiplication and division happen before addition and subtraction, unless parentheses say otherwise.
Store the parts in clearly-named int variables, then combine them inside println. Because both price and quantity are declared as int, Java guarantees the multiplication is safe, whole-number arithmetic, no risk of one of them secretly being text.