</>
Skip to content
Java lessons (12/47)

Java — Math

Math class

Math.max(10, 20);     // 20
Math.min(10, 20);     // 10
Math.abs(-10);        // 10
Math.sqrt(25);        // 5.0
Math.pow(2, 3);       // 8.0
Math.round(3.7);      // 4
Math.ceil(3.2);       // 4.0
Math.floor(3.8);      // 3.0

Random number

Math.random();  // 0.0 to 1.0
int num = (int)(Math.random() * 100);  // 0 to 99

Constants

Math.PI;   // 3.14159...
Math.E;    // 2.71828...

Mini Practice

  1. Use max/min
  2. Calculate square root
  3. Generate random numbers
  4. Use PI constant

Up Next

Continue with Booleans - Boolean logic.

Related Topics

Frequently Asked Questions about Math

What is Math in Java?

Math is a fundamental concept in Java. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn Math?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Math.

Why is Math important in Java?

Math is essential for Java development. Understanding this concept will help you write better code and solve real-world problems more effectively.