Math Class Methods
We use various Math
class methods here to present the concept of static
methods. Class Math
provides a collection of methods that enable you to perform common mathematical calculations. For example, you can calculate the square root of 900.0
with the static
method call
Math.sqrt(900.0)
This expression evaluates to 30.0
. Method sqrt
takes an argument of type double
and returns a result of type double
. To output the value of the preceding method call in the command window, you might write the statement
System.out.println(Math.sqrt(900.0));
In this statement, the value that sqrt
returns becomes the argument to method println
. There was no need to create a Math
object before calling method sqrt ...
Get Java™ How To Program (Early Objects), Tenth Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.