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:

ParameterDescription
numberint, 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

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

LEAVE A REPLY

Please enter your comment!
Please enter your name here