-5

When I want to use the mathematical methods in Java like abs acos I have to put it like this: Math.abs(int a), Math.acos(double a).

But what does it really mean?

Is Math the name of the class or some object? How does it work?

Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • 3
    `Math` is `class` which have `static` methods like `abs` etc. – Sandeep Singh Mar 15 '18 at 11:52
  • `Math` is name of class placed in `java.lang` package which is why we don't need to import it and can use in our code directly (just like any other classes from that package, like for `String`, `Integer`). That is *utility* class, which has only `static` methods for mathematical expressions. Because methods are static we invoke them on class itself like `Math.abs(x)`. – Pshemo Mar 15 '18 at 12:04

2 Answers2

2

Math class has static methods. So you can invoke it like:

int absolute = Math.abs(-123);
// absolute now has +123

A static method can be invoked without creating an instance of a class.

Aniket Sahrawat
  • 12,410
  • 3
  • 41
  • 67
pavithraCS
  • 709
  • 6
  • 23
  • 2
    or use static import: `import java.lang.Math.abs;` and invoke `abs(int a)` directly in your code as you would do with a static method inside the same class. But to keep in mind, this may confuse the devs and only use if your code is getting lengthy. – Aniket Sahrawat Mar 15 '18 at 11:55
1

The class Math contains methods for performing basic numeric operations such as the elementary exponential, logarithm, square root, and trigonometric functions.

https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html

Emre Acar
  • 920
  • 9
  • 24