Java Math Methods

Built-in mathematical functions for calculations

🧮 What are Math Methods?

Java Math methods are built-in functions that perform mathematical calculations like finding maximum values, calculating square roots, and generating random numbers without complex coding.


// Basic math methods example
System.out.println(Math.max(10, 20));    // 20
System.out.println(Math.sqrt(16));       // 4.0
System.out.println(Math.abs(-5));        // 5
System.out.println(Math.random());       // Random number 0-1
                                    

Types of Math Methods

📊

Basic Operations

Essential mathematical calculations

max() min() abs()
📐

Power & Roots

Exponential and root calculations

pow() sqrt() cbrt()
🎲

Random & Round

Generate random numbers and rounding

random() round() ceil()
📏

Trigonometry

Angle and triangle calculations

sin() cos() tan()

🔹 Basic Math Operations

Essential mathematical functions for everyday calculations:

public class BasicMath {
    public static void main(String[] args) {
        int a = 10, b = 20;
        double x = -15.7;
        
        // Find maximum and minimum
        System.out.println("Max of " + a + " and " + b + ": " + Math.max(a, b));
        System.out.println("Min of " + a + " and " + b + ": " + Math.min(a, b));
        
        // Absolute value (remove negative sign)
        System.out.println("Absolute value of " + x + ": " + Math.abs(x));
        
        // Sign function
        System.out.println("Sign of " + x + ": " + Math.signum(x));
    }
}

Output:

Max of 10 and 20: 20

Min of 10 and 20: 10

Absolute value of -15.7: 15.7

Sign of -15.7: -1.0

🔹 Power and Root Calculations

Calculate powers, square roots, and cube roots:

public class PowerAndRoots {
    public static void main(String[] args) {
        double number = 16;
        
        // Power calculations
        System.out.println(number + " squared: " + Math.pow(number, 2));
        System.out.println(number + " cubed: " + Math.pow(number, 3));
        System.out.println("2 to the power of 8: " + Math.pow(2, 8));
        
        // Root calculations
        System.out.println("Square root of " + number + ": " + Math.sqrt(number));
        System.out.println("Cube root of 27: " + Math.cbrt(27));
        
        // Natural logarithm and exponential
        System.out.println("e^2: " + Math.exp(2));
        System.out.println("ln(10): " + Math.log(10));
    }
}

Output:

16.0 squared: 256.0

16.0 cubed: 4096.0

2 to the power of 8: 256.0

Square root of 16.0: 4.0

Cube root of 27: 3.0

e^2: 7.38905609893065

ln(10): 2.302585092994046

🔹 Random Numbers and Rounding

Generate random numbers and round decimal values:

public class RandomAndRound {
    public static void main(String[] args) {
        double decimal = 15.7;
        
        // Rounding methods
        System.out.println("Original: " + decimal);
        System.out.println("Rounded: " + Math.round(decimal));
        System.out.println("Ceiling: " + Math.ceil(decimal));
        System.out.println("Floor: " + Math.floor(decimal));
        
        // Random numbers
        System.out.println("Random 0-1: " + Math.random());
        
        // Random integer between 1-10
        int randomInt = (int)(Math.random() * 10) + 1;
        System.out.println("Random 1-10: " + randomInt);
        
        // Random integer between 50-100
        int randomRange = (int)(Math.random() * 51) + 50;
        System.out.println("Random 50-100: " + randomRange);
    }
}

Output:

Original: 15.7

Rounded: 16

Ceiling: 16.0

Floor: 15.0

Random 0-1: 0.7234567891234567

Random 1-10: 7

Random 50-100: 73

🔹 Math Constants

Java provides useful mathematical constants:

public class MathConstants {
    public static void main(String[] args) {
        // Mathematical constants
        System.out.println("PI: " + Math.PI);
        System.out.println("E (Euler's number): " + Math.E);
        
        // Using constants in calculations
        double radius = 5;
        double area = Math.PI * Math.pow(radius, 2);
        double circumference = 2 * Math.PI * radius;
        
        System.out.println("Circle with radius " + radius + ":");
        System.out.println("Area: " + Math.round(area * 100.0) / 100.0);
        System.out.println("Circumference: " + Math.round(circumference * 100.0) / 100.0);
    }
}

Output:

PI: 3.141592653589793

E (Euler's number): 2.718281828459045

Circle with radius 5.0:

Area: 78.54

Circumference: 31.42

🔹 Practical Math Examples

Real-world applications of Math methods:

Common Use Cases:

  • Games: Random dice rolls, damage calculations
  • Finance: Interest calculations, rounding money
  • Graphics: Distance calculations, angles
  • Statistics: Finding averages, ranges
public class PracticalMath {
    public static void main(String[] args) {
        // Dice roll simulation
        int dice1 = (int)(Math.random() * 6) + 1;
        int dice2 = (int)(Math.random() * 6) + 1;
        System.out.println("Dice roll: " + dice1 + " + " + dice2 + " = " + (dice1 + dice2));
        
        // Distance between two points
        double x1 = 0, y1 = 0, x2 = 3, y2 = 4;
        double distance = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
        System.out.println("Distance: " + distance);
        
        // Compound interest calculation
        double principal = 1000;
        double rate = 0.05; // 5%
        int years = 3;
        double amount = principal * Math.pow(1 + rate, years);
        System.out.println("Investment after " + years + " years: $" + Math.round(amount * 100.0) / 100.0);
    }
}

🧠 Test Your Knowledge

Which method returns the square root of a number?