'Using abs () method in java. My compiler doesn't know the method

I have a simple question, but i can't find a solution for it.

I want to use abs() method, but it doesn't work. I'm always getting the error

Cannot find symbol: method abs(int)

I have already tried to use the method by including "import java.math" above the code. But that doens't work too.



Solution 1:[1]

Call it as

Math.abs(number)

or import as:

import static java.lang.Math.abs;

Solution 2:[2]

All functions in Java are part of a class. abs() is a static member of the Math class, so call

 Math.abs(val);

It's in java.lang, so no need to import anything

Solution 3:[3]

It's a static method. It has to be used like this:

Math.abs(int);

javadoc

See Class Methods in the Java Tutorial.

Solution 4:[4]

You have to refer to the Math class when you use it:

Math.abs(<intval>)

Solution 5:[5]

Or

import static java.lang.Math.*;

Solution 6:[6]

First of all, it's java.lang.Math (your package was wrong and Math is capitalized) but that's not a problem since all classes in java.lang are automatically imported.

As Brian says, use Math.abs(). Or, you can import the methods statically:

import static java.lang.Math.*;

This will allow you to use just abs() (and all other static methods from the Math class) without prefixing them with Math.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Lou Franco
Solution 3 Adam
Solution 4 Marcus Leon
Solution 5 Amir Afghani
Solution 6 Mark Peters