The Math Class
Math has max, min, abs, sqrt, pow, round, floor, ceil and random. Math.random() returns a decimal from 0 up to 1. For a random integer in a range, multiply and cast, or use the Random class.
Example
System.out.println(Math.max(3, 9));
System.out.println(Math.sqrt(81));
System.out.println(Math.pow(2, 10));
System.out.println(Math.round(4.6));
System.out.println(Math.floor(4.9));
System.out.println(Math.ceil(4.1));
int dice = (int) (Math.random() * 6) + 1;
System.out.println(dice >= 1 && dice <= 6);Result
9 9.0 1024.0 5 4.0 5.0 true
Exercise
Find the larger of two numbers.
Math.(3, 9)
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.