Witscale Test Center

13.2 The java.lang.Math class


13.2 The java.lang.Math class

The Math class contains methods for performing basic numeric operations such as the number comparison, finding square root, rounding numbers and trigonometric functions. All of the operations are services where you pass some argument and get the result. Thus, none of the service method requires storing their arguments as object’s state in member variables. For this reason, this class defines all its public methods as static methods. So any class can invoke them without creating an instance of Math class. Therefore, you do not need an instance of Math class to use the operations it performs. In fact, you cannot create an instance of Math class because it has only one constructor and it is declared as private. Since this class performs the fundamental operations, it should not be subclassed so that someone can redefine them. Hence, this class is also defined as a final. So keep following things in mind about the Math class.

 

You cannot instantiate Math class. It has only one private constructor. Hence, you cannot create instance of this class. Since all its public methods (and variables) are also static, you never need to create an instance anyway.

You cannot extend Math class. It is declared as a final class. Hence, you cannot subclass it.