Basic arithmetic
Description
Use +, -, *, and / for common calculations.
Explanation
Parentheses make calculation order explicit.
Example
int total = 4 + 6 * 2;← Chapter 2 — Core Syntax & Data
mixed
Arithmetic operators let programs calculate totals, differences, products, quotients, and remainders. They work naturally with numeric variables.
When both operands are integers, division returns an integer result. Convert one operand to double when a fractional answer matters.
Use +, -, *, and / for common calculations.
Parentheses make calculation order explicit.
int total = 4 + 6 * 2;The % operator returns the remainder.
It is useful for even/odd checks and repeating patterns.
boolean even = 12 % 2 == 0;Operator precedence can surprise beginners. Write parentheses whenever they make a formula easier for another person to verify.
public class ArithmeticDemo {
public static void main(String[] args) {
int scored = 82;
int total = 100;
double percent = scored * 100.0 / total;
System.out.println(percent);
}
}