Ⓜ️Math Class

The Math class contains useful common mathematical methods like square root, trigonometric functions, rounding, etc. It's located in the java.lang package, which contains core Java classes.

Why are all the methods in Math declared as static?

There are a few important reasons:

  1. Mathematics is not an object - mathematical operations don't need an object instance to work. They can be called independently.

  2. Having static methods allows calling Math methods without creating a Math object. We can simply say Math.method() rather than new Math().method().

  3. The implementations of math operations like sine, square root, etc. are optimized and don't depend on object state. They will always work the same way.

  4. Declaring methods static means there is only one copy loaded into memory. This is more efficient than creating Math objects as they are needed.

  5. Mathematics deals with immutable values like integers/doubles. Static methods are well-suited for operating on immutable values.

Last updated