## \$show=/label

A quick guide to get the absolute value of the given number in java using Math.abs() method with examples.

## 1. Overview

In this tutorial, We'll learn how to get the absolute value for the given number in java
Java API has the handy Math utility class to work with arithmetic and mathematical operations.

Math.abs() method take the number and return the absolute value for the given value. Absolute value means the positive number for the given number. Here, a given number can be a positive value or negative value or zero.

Math.abs() is used to get the positive number for the given value.

## 2. Math.abs() Syntax

This method is an overloaded method and each takes a different type of data.

Syntax
```public static int abs(int a)
public static long abs(long a)
public static float abs(float a)
public static double abs(double a)
```

abs() method works with primitive number types such as int, long, float and double. Does not work for the char, byte, strings and boolean.

## 3. Java Absolute Value - Math.abs()

For int and long

abs() method returns the absolute value of an int/long value.

If the argument is not negative, the argument is returned. If the argument is negative, the negation of the argument is returned.

Note that if the argument is equal to the value of Integer.MIN_VALUE/Long.MIN_VALUE, the most negative representable int value, the result is that same value, which is negative.

For float and double

abs() method returns the absolute value of a primitive float/double value. If the argument is not negative, the argument is returned. If the argument is negative, the negation of the argument is returned.

Special cases

If the argument is positive zero or negative zero, the result is positive zero.
If the argument is infinite, the result is positive infinity.
If the argument is NaN, the result is NaN.
In other words, the result is the same as the value of the expression.

## 4. Java Absolute Value Example - Integer/Long

Let us create simple examples using Math.abs() function with integer and long values.

Example 1

This example is on primitive int type values. We have passed positive, negative and 0 to the abs function.
It has returned all positive values as a result.

```package com.javaprogramto.programs.numbers.math;

public class MathAbsoluteExample1 {

public static void main(String[] args) {

// for integer primitive values
int i = -100;
int absValueofI = Math.abs(i);

System.out.println("abs value of " + i + " is " + absValueofI);

i = 200;
absValueofI = Math.abs(i);

System.out.println("abs value of " + i + " is " + absValueofI);

i = -0;
absValueofI = Math.abs(i);

System.out.println("abs value of " + i + " is " + absValueofI);

}
}
```

Output
```abs value of -100 is 100
abs value of 200 is 200
abs value of 0 is 0
```

Example 2

For primitive long values.
```package com.javaprogramto.programs.numbers.math;

public class MathAbsoluteExample2 {

public static void main(String[] args) {

// for long primitive values
long l = -100;
long absValueofL = Math.abs(l);

System.out.println("abs value of " + l + " is " + absValueofL);

l = 200;
absValueofL = Math.abs(l);

System.out.println("abs value of " + l + " is " + absValueofL);

l = -0;
absValueofL = Math.abs(l);

System.out.println("abs value of " + l + " is " + absValueofL);

}
}
```

Output
```abs value of -100 is 100
abs value of 200 is 200
abs value of 0 is 0
```

Example 3

For Wrapper Integer values.
```package com.javaprogramto.programs.numbers.math;

public class MathAbsoluteExample3 {

public static void main(String[] args) {

// for Wrapper Integer values
Integer integer = -100;
Integer absValueofInteger = Math.abs(integer);

System.out.println("Integer - abs value of " + integer + " is " + absValueofInteger);

integer = 200;
absValueofInteger = Math.abs(integer);

System.out.println("Integer - abs value of " + integer + " is " + absValueofInteger);

integer = -0;
absValueofInteger = Math.abs(integer);

System.out.println("Integer - abs value of " + integer + " is " + absValueofInteger);
}
}
```

Output
```Integer - abs value of -100 is 100
Integer - abs value of 200 is 200
Integer - abs value of 0 is 0
```

Example 4

For wrapper Long values
```package com.javaprogramto.programs.numbers.math;

public class MathAbsoluteExample4 {

public static void main(String[] args) {

// for Wrapper Long values
Long longValue = -100l;
Long absValueofLong = Math.abs(longValue);

System.out.println("Long - abs value of " + longValue + " is " + absValueofLong);

longValue = 200l;
absValueofLong = Math.abs(longValue);

System.out.println("Long - abs value of " + longValue + " is " + absValueofLong);

longValue = -0l;
absValueofLong = Math.abs(longValue);

System.out.println("Long - abs value of " + longValue + " is " + absValueofLong);
}
}
```

Output
```Long - abs value of -100 is 100
Long - abs value of 200 is 200
Long - abs value of 0 is 0
```

## 5. Java Absolute Value Example - Double/Float

Math.abs() method takes the arguments of double and float type.

Example 5

For Double primitive and wrapper types
```package com.javaprogramto.programs.numbers.math;

public class MathAbsoluteExample5 {

public static void main(String[] args) {

// for primitive Double values

System.out.println("double primitive");
double doublePrimitive = -100;
Double absValueofDouble = Math.abs(doublePrimitive);

System.out.println("primitive double - abs value of " + doublePrimitive + " is " + absValueofDouble);

doublePrimitive = 200l;
absValueofDouble = Math.abs(doublePrimitive);

System.out.println("primitive double - abs value of " + doublePrimitive + " is " + absValueofDouble);

doublePrimitive = -0l;
absValueofDouble = Math.abs(doublePrimitive);

System.out.println("primitive double - abs value of " + doublePrimitive + " is " + absValueofDouble);

// for Wrapper Double values
System.out.println("\ndouble wrapper");
Double DoubleValue = Double.valueOf(-100);
Double absValueofWrapperDouble = Math.abs(DoubleValue);

System.out.println("Double - abs value of " + DoubleValue + " is " + absValueofWrapperDouble);

DoubleValue = Double.valueOf(100);
absValueofWrapperDouble = Math.abs(DoubleValue);

System.out.println("Double - abs value of " + DoubleValue + " is " + absValueofWrapperDouble);

DoubleValue = Double.valueOf(-0l);
absValueofWrapperDouble = Math.abs(DoubleValue);

System.out.println("Double - abs value of " + DoubleValue + " is " + absValueofWrapperDouble);
}
}
```

Output
```double primitive
primitive double - abs value of -100.0 is 100.0
primitive double - abs value of 200.0 is 200.0
primitive double - abs value of 0.0 is 0.0

double wrapper
Double - abs value of -100.0 is 100.0
Double - abs value of 100.0 is 100.0
Double - abs value of 0.0 is 0.0
```

Example 6

For float primitive and wrapper values.
```package com.javaprogramto.programs.numbers.math;

public class MathAbsoluteExample6 {

public static void main(String[] args) {

// for primitive Float values

System.out.println("Float primitive");
Float FloatPrimitive = -100f;
Float absValueofFloat = Math.abs(FloatPrimitive);

System.out.println("primitive Float - abs value of " + FloatPrimitive + " is " + absValueofFloat);

FloatPrimitive = 200f;
absValueofFloat = Math.abs(FloatPrimitive);

System.out.println("primitive Float - abs value of " + FloatPrimitive + " is " + absValueofFloat);

FloatPrimitive = -0f;
absValueofFloat = Math.abs(FloatPrimitive);

System.out.println("primitive Float - abs value of " + FloatPrimitive + " is " + absValueofFloat);

// for Wrapper Float values
System.out.println("\nFloat wrapper");
Float FloatValue = Float.valueOf(-100);
Float absValueofWrapperFloat = Math.abs(FloatValue);

System.out.println("Float - abs value of " + FloatValue + " is " + absValueofWrapperFloat);

FloatValue = Float.valueOf(100);
absValueofWrapperFloat = Math.abs(FloatValue);

System.out.println("Float - abs value of " + FloatValue + " is " + absValueofWrapperFloat);

FloatValue = Float.valueOf(-0l);
absValueofWrapperFloat = Math.abs(FloatValue);

System.out.println("Float - abs value of " + FloatValue + " is " + absValueofWrapperFloat);
}
}
```

Output
```Float primitive
primitive Float - abs value of -100.0 is 100.0
primitive Float - abs value of 200.0 is 200.0
primitive Float - abs value of -0.0 is 0.0

Float wrapper
Float - abs value of -100.0 is 100.0
Float - abs value of 100.0 is 100.0
Float - abs value of 0.0 is 0.0
```

## 6. Conclusion

In this article, We've seen how to get the absolute value for the given number in java. Math.abs() method takes long, float, int and double types and returns its absolute values. BLOGGER

Author: Venkatesh - I love to learn and share the technical stuff.
Name

accumulo,1,ActiveMQ,2,Adsense,1,API,37,ArrayList,18,Arrays,24,Bean Creation,3,Bean Scopes,1,BiConsumer,1,Blogger Tips,1,Books,1,C Programming,1,Collection,8,Collections,37,Collector,1,Command Line,1,Comparator,1,Compile Errors,1,Configurations,7,Constants,1,Control Statements,8,Conversions,6,Core Java,149,Corona India,1,Create,2,CSS,1,Date,3,Date Time API,38,Dictionary,1,Difference,2,Download,1,Eclipse,3,Efficiently,1,Error,1,Errors,1,Exceptions,8,Fast,1,Files,17,Float,1,Font,1,Form,1,Freshers,1,Function,3,Functional Interface,2,Garbage Collector,1,Generics,4,Git,9,Grant,1,Grep,1,HashMap,2,HomeBrew,2,HTML,2,HttpClient,2,Immutable,1,Installation,1,Interview Questions,6,Iterate,2,Jackson API,3,Java,32,Java 10,1,Java 11,6,Java 12,5,Java 13,2,Java 14,2,Java 8,128,Java 8 Difference,2,Java 8 Stream Conversions,4,java 8 Stream Examples,12,Java 9,1,Java Conversions,14,Java Design Patterns,1,Java Files,1,Java Program,3,Java Programs,114,Java Spark,1,java.lang,4,java.util. function,1,JavaScript,1,jQuery,1,Kotlin,11,Kotlin Conversions,6,Kotlin Programs,10,Lambda,2,lang,29,Leap Year,1,live updates,1,LocalDate,1,Logging,1,Mac OS,3,Math,1,Matrix,6,Maven,1,Method References,1,Mockito,1,MongoDB,3,New Features,1,Operations,1,Optional,6,Oracle,5,Oracle 18C,1,Partition,1,Patterns,1,Programs,1,Property,1,Python,2,Quarkus,1,Read,1,Real Time,1,Recursion,2,Remove,2,Rest API,1,Schedules,1,Serialization,1,Servlet,2,Sort,1,Sorting Techniques,8,Spring,2,Spring Boot,23,Spring Email,1,Spring MVC,1,Streams,31,String,61,String Programs,28,String Revese,1,StringBuilder,1,Swing,1,System,1,Tags,1,Threads,11,Tomcat,1,Tomcat 8,1,Troubleshoot,26,Unix,3,Updates,3,util,5,While Loop,1,
ltr
item
JavaProgramTo.com: Java Absolute Value - Math.abs()
Java Absolute Value - Math.abs()
A quick guide to get the absolute value of the given number in java using Math.abs() method with examples.