Skip to content
QuizMaker logoQuizMaker
Activity
Java Programming: From Zero to Enterprise
1. Java Fundamentals
1. Getting Started with Java & the JVM
2. Data Types & Variables
3. Control Flow: Ifs & Loops
4. String Manipulation in Depth
5. Methods (Functions) Architecture
6. Arrays & The Enhanced For Loop
7. User Input via Scanner
8. Mathematical Operations & The Math Class
9. Operators in Depth
10. Block Scope & Variable Lifecycles
11. Introduction to Object-Oriented Programming
12. Classes & Instances Deep Dive
13. Constructors
14. Encapsulation & The 'this' Keyword
15. Inheritance: Extending Functionality
16. Polymorphism & Method Overriding
17. Abstraction & Abstract Classes
18. Interfaces: The Ultimate Contract
19. Packages & Access Modifiers
20. Enums (Enumerations)
21. Exceptions: Handling Runtime Errors
22. The 'throw' and 'throws' keywords
23. Dates, Times, and Formatting
24. Enumerable Data Structures
25. LinkedLists: The Alternative
26. HashMaps: Key-Value Architecture
27. HashSets: The Art of Uniqueness
28. Iterator: Safe Collection Traversal
29. Wrapper Classes & Autoboxing
30. Basic File I/O
31. Generics: Type-Safe Templates
32. Lambda Expressions & Functional Interfaces
33. The Stream API: Functional Data Pipelines
34. Optional: Beating the NullPointerException
35. Multithreading & Concurrency Basics
36. JDBC: Connecting to SQL Databases
37. Annotations & Reflection
38. The JVM Garbage Collector
39. Introduction to Spring Boot
40. Unit Testing with JUnit
41. Java Collections for DSA
CONTENTS

8. Mathematical Operations & The Math Class

Performing complex mathematical calculations easily.

Feb 22, 202625 views0 likes0 fires
18px

[!NOTE] Basic operators like +, -, *, / are built securely into Java. But for advanced tasks like absolute values, square roots, and randomness, you utilize the Math class.

The Math Class Toolkit

The Math class contains static methods. This means you do not have to create an object via new Math()—you just access the tools directly!

int maxNumber = Math.max(5, 10);      // returns 10
int minNumber = Math.min(5, 10);      // returns 5
double root = Math.sqrt(64);          // returns 8.0
double absolute = Math.abs(-4.7);     // returns 4.7

Generating Random Numbers

One of the most heavily used functions in game development and randomized testing is Math.random().

By default, it returns a double value with a positive sign, greater than or equal to 0.0 and strictly less than 1.0.

double randomNum = Math.random(); // E.g., 0.8273648

Controlling the Range

If you want to generate a random integer between 0 and 100, you have to multiply the result and explicitly "cast" the double back down into an int.

// 1. Math.random() generates 0.55// 2. Multiplied by 101 becomes 55.55// 3. The (int) cast chops off the decimals, leaving 55.int randomInt = (int)(Math.random()

101);

[!TIP] If you need complex randomization (like Gaussian distributions or robust seeds), look into the java.util.Random class instance approach instead of Math.random().

Share this article

Share on TwitterShare on LinkedInShare on FacebookShare on WhatsAppShare on Email

Test your knowledge

Take a quick quiz based on this chapter.

easyJava
Quiz: Math
9 questions5 min

Continue Learning

9. Operators in Depth

Beginner
10 min

10. Block Scope & Variable Lifecycles

Beginner
8 min

11. Introduction to Object-Oriented Programming

Beginner
10 min
Lesson 8 of 10 in 1. Java Fundamentals
Previous in 1. Java Fundamentals
7. User Input via Scanner
Next in 1. Java Fundamentals
9. Operators in Depth
← Back to Java Programming: From Zero to Enterprise
Back to Java Programming: From Zero to EnterpriseAll Categories