KwickAcademy Java · 7 min · free
Mathematical Library Methods
Java's ready-made Math methods: sqrt, cbrt, pow, abs, max, min, round, ceil, floor and random, plus writing maths formulas as Java code.
Follows the syllabus of: CBSE Class 11 Information Technology (802), ICSE Class 9 Computer Applications, ICSE Class 10 Computer Applications, Cambridge IGCSE Grade 10 Computer Science (0478)
On screen in this lesson
The Math class
| Math is a ready-made class in java.lang |
| No import needed |
| Call a method as Math.methodName(value) |
| Most methods return a double |
Roots, powers and abs
| Method | Gives | Example |
|---|---|---|
| Math.sqrt(x) | square root | sqrt(49) = 7.0 |
| Math.cbrt(x) | cube root | cbrt(27) = 3.0 |
| Math.pow(a, b) | a to power b | pow(2, 10)=1024.0 |
| Math.abs(x) | value without sign | abs(-7) = 7 |
Rounding methods
| Method | Does | 4.5 gives |
|---|---|---|
| Math.round(x) | nearest whole | 5 |
| Math.ceil(x) | next whole up | 5.0 |
| Math.floor(x) | next whole down | 4.0 |
Math.random()
| Returns a double from 0.0 up to, not including, 1.0 |
| Multiply to stretch the range |
| Cast with (int) to drop the decimal |
| Add the smallest value to shift the range |
Writing formulas in Java
| In maths | In Java | Note |
|---|---|---|
| a² + b² | a*a + b*b | no ² symbol |
| √(a² + b²) | Math.sqrt(a*a+b*b) | * is needed |
| πr² | Math.PI * r * r | PI is a constant |
| (a + b) / 2 | (a + b) / 2.0 | 2.0 keeps decimal |
Recap
| sqrt, cbrt, pow return double; abs keeps the type |
| max and min compare exactly two values |
| round: nearest; ceil: up; floor: down |
| (int)(Math.random() * (max - min + 1)) + min |
| Every multiplication needs *, use Math.PI for pi |
Quick answers
Why does Math.ceil(4.5) print 5.0 and not 5?
ceil returns a double, so it always shows a decimal part.
What does Math.round(-4.5) give?
-4, because a half always rounds up towards the bigger number.
KwickClips from this lesson
Short clips, one idea each. Good for revision the night before.
Why 7.0 and not 7?43 sec
Can Math.max take three numbers?43 sec
Why is Math.round(-4.5) equal to -4?41 sec
How do you roll a dice in Java?40 sec
Is 2ab valid Java?45 secThe full lesson, in text
Hello students, welcome to Kwickprep. Why does Math dot ceil of four point five print five point zero, and not just five? Today we learn Java's ready-made maths methods. We will find square roots, powers, the bigger of two numbers, rounding, and random numbers. Then we will turn a formula from your maths book into Java.
A library method is a method that Java has already written for us. Java keeps its maths methods in a class called Math, with a capital M, in the package java dot lang. This package is loaded automatically, so we do not need any import line. We call a method by writing Math, a dot, the method name, and the value in brackets. Most of these methods give back a double, which is a number with a decimal point.
Here are four common methods. Square root, written sqrt, gives the number which multiplied by itself makes x, so the square root of forty nine is seven point zero. Cube root, written cbrt, gives the number which multiplied three times makes x, so the cube root of twenty seven is three point zero. Pow of a and b gives a raised to the power b. So two to the power ten is one thousand twenty four point zero. Abs gives the absolute value, which is the number without its minus sign.
Let us run them. Square root of forty nine prints seven point zero, with a decimal, because sqrt always returns a double. Pow of two and ten prints one thousand twenty four point zero, again a double. Abs of minus seven prints seven, with no decimal. That is because abs returns the same type it receives, so an int gives an int.
Math dot max gives the bigger of two numbers, and Math dot min gives the smaller. Each takes exactly two values. So to find the topper among three students, we use max twice. First, max of Riya and Aman is seventy two. Then max of seventy two and Neha's sixty is still seventy two.
Pause and predict. Min of eight and three is easy, it prints three. Max of three and four point five gives four point five. Now the tricky one, max of five and five point zero. Both are equal, but one is a double, so Java treats both as doubles. The answer is five point zero, not five.
Java has three ways to round a number. Round gives the nearest whole number, and a half goes up, so four point five becomes five. Ceil, short for ceiling, always goes up to the next whole number, and returns a double. Floor always goes down to the whole number below, and also returns a double.
Board papers love negative numbers here. Think of a number line, where up means towards the right. Here x is minus four point five. Ceil goes up, to minus four point zero. Floor goes down, to minus five point zero. Round gives minus four, not minus five, because a half always rounds up towards the bigger number.
Math dot random gives a different number every time we run it. It returns a double that is at least zero, but always less than one. To get a bigger range, we multiply it. Then we cast it with int in brackets, which cuts off the decimal part. Finally we add the smallest value we want, to move the range up.
Let us roll a dice. Math dot random times six gives a number from zero up to just below six. Cast to int, and we get zero to five. Add one, and we get one to six. The general rule is random times max minus min plus one, cast to int, then add min. For a number from ten to twenty, we multiply by eleven, and add ten.
Now let us write maths formulas in Java. A squared plus b squared becomes a star a plus b star b, because Java has no small two symbol. For a square root, we put the whole expression inside Math dot sqrt, and every multiplication needs a star. Pi r squared uses Math dot pi, a constant already stored in the Math class. For an average, divide by two point zero, because int divided by int drops the decimal.
Here is a full formula. The radius r is seven. Area equals Math dot pi times r times r. The exact area is about one hundred fifty three point nine four. Math dot round gives the nearest whole number, so it prints one hundred fifty four.
Some exams ask for pseudocode instead of Java. Pseudocode is a plain, exam-style way to write steps, which is never run on a computer. The arrow means store the value. Round with two values rounds the area to zero decimal places. Random gives a number from zero to one, so this line gives a dice value from one to six.
Let us recap. Square root, cube root and power return a double, but abs keeps the type it gets. Max and min compare exactly two values. Round goes to the nearest whole number, ceil goes up, and floor goes down. For a random number in a range, use random times max minus min plus one, cast to int, plus min. And in a formula, every multiplication needs a star.
Courses that teach this
| Course | Unit |
|---|---|
| CBSE Class 11 Information Technology (802) | Part B, Unit 3: Office Automation Tools |
| ICSE Class 9 Computer Applications | Mathematical Library Methods |
| ICSE Class 10 Computer Applications | Revision of Class IX Syllabus |
| Cambridge IGCSE Grade 10 Computer Science (0478) | 8. Programming |
Voice-over in this lesson is AI-generated. The script is written and checked by Kajal Ma'am. Boards can revise a syllabus mid-year, so confirm anything you plan around against the official board circular. Keep your passwords, OTPs and ID numbers to yourself — we never ask for them. To reach Kajal Ma'am, use the WhatsApp button; sharing your number there is how we call you back.
Free to watch, no sign-up. Live classes with Kajal Ma'am are the paid course; these lessons stay free either way.

