Math.abs in Java

 Math. abs it's a process that takes the accurate value from the user.

if the user give input x= -78

then if we use (Math.abs(x));

it' will be printed only x=78


Syntax:

  1. public static int abs(int i)  
  2. public static double abs(double d)  
  3. public static float abs(float f)  
  4. public static long abs(long lng)  

Parameters:

  1. The argument whose absolute value is to be determined  

Return:

  1. This method returns the absolute value of the argument 


Example 1:

  1. public class AbsExample1  
  2. {  
  3.     public static void main(String args[])  
  4.     {  
  5.          
  6.         int x = -48;  
  7.         //print the absolute value of int type  
  8.         System.out.println(Math.abs(x));  
  9.     }  
  10. }  

Output

48

Post a Comment

Previous Post Next Post