Coding/Learn/java/numbers math
Java Foundational Course
01.Introduction to Java
02.Your First Java Program
03.Syntax & Code Structure
04.Comments in Java
05.Variables & Declaration
06.Primitive Data Types
07.Numbers & Arithmetic Operators
08.Strings in Java
09.Booleans & Relational Operators
10.Type Casting & Conversion
11.Scanner & Interactive Input
12.Logical Operators (&&, ||, !)
13.If Statements
14.If-Else Statements
15.If-Else-If Ladders
16.Switch Statements
17.While Loops
18.Do-While Loops
19.Standard For Loops
20.Break & Continue Statements
21.Arrays in Java
22.Array Indexing & Traversal
23.Enhanced For-Each Loop
24.Essential String Methods
25.Methods in Java
26.Method Parameters & Arguments
27.Method Overloading
28.Classes & Objects in Java
29.Constructors in Java
30.Exception Handling (try-catch-finally)
31.Capstone Mini Projects
Course Certificate0%

Complete all lessons to earn

LearnNumbers & Arithmetic Operators
7 / 31 0%
Lesson 07 of 31
6 mins read

Numbers & Arithmetic Operators

PreviousNext Lesson
In Plain English

Java provides standard arithmetic operators along with a powerful built-in Math library for advanced calculations. Understanding integer division and operator precedence prevents subtle math bugs.

The Mental Model

"Integer division in Java is like cutting whole apples. If you divide 7 apples among 2 people, each gets 3 whole apples—the fractional half is discarded unless you use double!"

Deep Dive: How It Works

Basic operators: `+` (add), `-` (subtract), `*` (multiply), `/` (divide), `%` (modulus / remainder).

Integer Division Trap: Dividing two integers (e.g. `5 / 2`) produces an integer (`2`), truncating the decimal. To get `2.5`, at least one operand must be a floating point (`5.0 / 2`).

Compound operators: `+=`, `-=`, `*=`, `/=`, `%=`, `++` (increment), `--` (decrement).

Math utility class: `Math.max(a, b)`, `Math.min(a, b)`, `Math.sqrt(x)`, `Math.pow(base, exp)`, `Math.abs(x)`.

Syntax Blueprint

int sum = a + b;
int remainder = a % b;
double root = Math.sqrt(25.0);

Use % to find remainders (useful for even/odd checks). Use Math methods for powers and roots.

PointCore Rules to Remember

Point star
Modulus Operator %: Returns remainder: 10 % 3 equals 1.
Point star
Integer Division: 7 / 2 yields 3; 7.0 / 2 yields 3.5.
Point star
Math Library: Built into java.lang—no import needed.

Common Beginner Traps & How to Fix Them

Mistake:Expecting a decimal from int division: double d = 5 / 2; (results in 2.0)

Why it happens: 5 / 2 evaluates to 2 as an int, which is then cast to 2.0.

How to fix: Write double d = 5.0 / 2; or (double) 5 / 2;

Pro Tip: Use n % 2 == 0 to check if any integer n is even.

Live Interactive Example

Hit Run Code to see it live
Arithmetic and Math Functions
Java 13
1
2
3
4
5
6
7
8
9
10
11
12
Output Console
Click "Run Code" to view the rendered output.
How it works: Notice how 14 / 4 gives 3, but 14.0 / 4 gives 3.5. Math.pow returns a double.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Calculate rectangle area and perimeter

  • PointGiven length = 8 and width = 5, calculate area (length * width) and perimeter (2 * (length + width)).
  • PointPrint "Area: " + area and "Perimeter: " + perimeter on separate lines.
1
2
3
4
5
6
7
8
9
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates