Math abs java code example

Example 1: finding absolute value in java

import java.lang.Math;
int sum,x=-5;
sum = Math.abs(x);

Example 2: abs in java

public class Test { 

   public static void main(String args[]) {
      Integer a = -8;
      double d = -100;
      float f = -90;

      System.out.println(Math.abs(a));
      System.out.println(Math.abs(d));     
      System.out.println(Math.abs(f));    
   }
}

Example 3: math.absolute java

float a = 123.0f;
System.out.println(Math.abs(a)); //123.0

double d = -999.3456;
System.out.println(Math.abs(d)); //999.3456

int e = -123;
System.out.println(Math.abs(g)); //123

long g = -12345678;
System.out.println(Math.abs(g)); //12345678

Tags:

Java Example