Java Math abs() method with examples
In Java, abs() function returns the absolute value of the given argument. By using abs function you can return absolute value for an int, float, double, short, long, byte etc. data types.
Basic Syntax
Following is the basic syntax for abs() function in Java:
int abs(int number)
double abs(double number)
float abs(float number)
short abs(short number)
long abs(long number)
byte abs(byte number)
And the parameters are:
Parameter | Description |
---|---|
number | int, float, double, short, long or byte number. |
Return Value
abs() function returns absolute value of the given number.
- For an integer value, it will return an integer
- For float value, it will return a float and so on.
Example
public class Demo { public static void main(String args[]) { int num1 = -8; float num2 = -90.0f; double num3 = -100.01; long num4 = 20345676; short num5 = 98; byte num6 = -5; System.out.println("Integer num1 = "+Math.abs(num1)); System.out.println("Float num2 = "+Math.abs(num2)); System.out.println("Double num3 = "+Math.abs(num3)); System.out.println("Long num4 = "+Math.abs(num4)); System.out.println("Short num5 = "+Math.abs(num5)); System.out.println("Byte num6 = "+Math.abs(num6)); } }
The output for the above program is as given below:
Integer num1 = 8
Float num2 = 90.0
Double num3 = 100.01
Long num4 = 20345676
Short num5 = 98
Byte num6 = 5
Float num2 = 90.0
Double num3 = 100.01
Long num4 = 20345676
Short num5 = 98
Byte num6 = 5
LATEST POSTS
-
Queue C++
-
C strcat() function with example
-
How to Print Without Newline in Python
-
How to Upgrade PIP in Windows
-
Substring in C++
-
numpy.median() in Python
-
numpy.argmax() in Python
-
Java Math abs() method with examples
-
numpy.sum() in Python
-
numpy.append() in Python
-
numpy.where() in Python with Examples
-
numpy.transpose() in Python