← Back to Core Java Course | Chapter 14: Advanced Topics | Lesson 3 of 6

Java Math Operations

Math operations are the calculations Java can do, from adding to square roots and random numbers, like the buttons on a scientific calculator. This topic groups the most useful ones.

Basic Arithmetic Operations

Java's basic arithmetic operators (+, -, *, /, %) work directly on numeric primitives, with % giving the remainder rather than a true modulo for negative operands.

Mixing integer and floating-point operands in the same expression automatically promotes the result to a floating-point type.

Example: Basic Arithmetic Operations

java
public class Main {
	public static void main(String[] args) {
		System.out.println(10 + 3);
		System.out.println(10 % 3); // remainder
		System.out.println(-10 % 3); // not a true modulo for negatives
	}
}

Absolute and Power Functions

Math.abs() strips a value's sign to get its absolute magnitude, and Math.pow(base, exp) raises a number to a given power, both returning results appropriate to their input types.

Example: Absolute and Power Functions

java
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Print a line to the console
		System.out.println(Math.abs(-7));
		// Print a line to the console
		System.out.println(Math.pow(2, 4));
	}
}

Square Root and Rounding

Math.sqrt() computes a square root as a double, and combining it with Math.round() or casting lets you get a clean integer result when the exact square root isn't needed.

Casting the double result of Math.sqrt() back to an int truncates any decimal portion rather than rounding it, which is a common source of off-by-one bugs.

Example: Square Root and Rounding

java
public class Main {
	public static void main(String[] args) {
		double root = Math.sqrt(20);
		System.out.println(root);
		System.out.println(Math.round(root)); // clean integer result
	}
}

Min and Max Helpers

Math.max() and Math.min() return the larger or smaller of two values respectively, avoiding a manual if-else comparison for this extremely common operation.

Both methods are overloaded to accept int, long, float, and double arguments, so they work seamlessly across Java's numeric types.

Example: Min and Max Helpers

java
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Print a line to the console
		System.out.println(Math.max(4, 9));
		// Print a line to the console
		System.out.println(Math.min(4, 9));
	}
}

Generating Random Numbers

Generating a random number typically pairs Math.random() (which returns a double between 0.0 and 1.0) with scaling and casting to land within whatever integer range you actually need.

Example: Generating Random Numbers

java
public class Main {
	public static void main(String[] args) {
		int random = (int) (Math.random() * 10); // scaled and cast to an int range
		System.out.println(random >= 0 && random < 10);
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Using integer division and losing the decimal part.
  2. Using Math.abs(Integer.MIN_VALUE), which is still negative.
  3. Expecting Math.random() to return an integer, when it returns a double from 0.0 up to but not including 1.0.
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.