What you will learn
In this lesson, you will learn how Java performs basic mathematical calculations using arithmetic operators such as addition, subtraction, multiplication, division, and modulus. You will also learn how to use the Math class to perform common mathematical tasks such as finding the maximum value, square root, power, absolute value, and random number generation.
After reading the lesson, you can solve the assignments and then attempt the interactive quiz. The quiz checks your answers using JavaScript and gives instant feedback.
Arithmetic operators in Java
Math class methods
Assignments for practice
Interactive MCQ quiz
Arithmetic Operators in Java
Arithmetic operators are symbols used to perform mathematical operations on variables and values. These are some of the first operators every Java learner uses.
| Operator | Meaning | Example | Result |
|---|---|---|---|
| + | Addition | 10 + 5 |
15 |
| - | Subtraction | 10 - 5 |
5 |
| * | Multiplication | 10 * 5 |
50 |
| / | Division | 10 / 5 |
2 |
| % | Modulus or remainder | 10 % 3 |
1 |
Example program
public class Main {
public static void main(String[] args) {
int a = 12;
int b = 5;
System.out.println("Addition: " + (a + b));
System.out.println("Subtraction: " + (a - b));
System.out.println("Multiplication: " + (a * b));
System.out.println("Division: " + (a / b));
System.out.println("Remainder: " + (a % b));
}
}
Notice the division result. Since both a and b are integers, Java performs integer division. That means the decimal part is removed. So 12 / 5 becomes 2, not 2.4.
double x = 12; double y = 5; System.out.println(x / y); // 2.4
Increment and Decrement
Java also provides shortcut operators to increase or decrease a value by one.
int count = 7; count++; // count becomes 8 count--; // count becomes 7 again System.out.println(count);
The increment operator ++ adds one. The decrement operator -- subtracts one.
Math Class Methods in Java
The Math class in Java contains many useful built-in methods. These methods help you perform mathematical operations quickly without writing your own logic for every task.
| Method | Purpose | Example | Output |
|---|---|---|---|
Math.max(a, b) |
Returns the larger value | Math.max(7, 11) |
11 |
Math.min(a, b) |
Returns the smaller value | Math.min(7, 11) |
7 |
Math.sqrt(x) |
Returns square root | Math.sqrt(81) |
9.0 |
Math.pow(a, b) |
Returns a raised to power b | Math.pow(2, 4) |
16.0 |
Math.abs(x) |
Returns absolute value | Math.abs(-14) |
14 |
Math.random() |
Returns random decimal from 0.0 to less than 1.0 | Math.random() |
Example: 0.738... |
Example program using Math methods
public class Main {
public static void main(String[] args) {
System.out.println(Math.max(15, 22));
System.out.println(Math.min(15, 22));
System.out.println(Math.sqrt(64));
System.out.println(Math.pow(3, 4));
System.out.println(Math.abs(-90));
System.out.println(Math.random());
}
}
To generate a random integer from 1 to 100, you can write:
int randomNumber = (int)(Math.random() * 100) + 1; System.out.println(randomNumber);
Assignments
Math.max() and Math.min().
Math.sqrt().
Math.pow().
Math.abs().
Interactive MCQ Quiz
Select one answer for each question and click Check Answers. The quiz is implemented using JavaScript and shows your score instantly.
0 / 0
Your result will appear here.
Quick Revision
- Use
+,-,*,/, and%for arithmetic operations. - Use
%when you need the remainder. - Integer division removes the decimal part.
- Use
Math.max()andMath.min()to compare values. - Use
Math.sqrt()for square root andMath.pow()for powers. - Use
Math.abs()to get the positive distance from zero. - Use
Math.random()for random decimal values.