Java/J2EE and SOA (324 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (41 Blogs)
  • Comprehensive Java Course (2 Blogs)
  • Java/J2EE and SOA (323 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-widget-banner-bg

Everything You Need To Know About Java Math abs()

Published on Sep 13,2019 79 Views

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-mobile-banner-bg

myMock Interview Service for Real Tech Jobs

  • Mock interview in latest tech domains i.e JAVA, AI, DEVOPS,etc
  • Get interviewed by leading tech experts
  • Real time assessment report and video recording

This article will introduce you to Java Math Abs() method, and in the process tell you how to generate absolute value of an argument. Following pointers will be covered in this article,

Lets begin!!

    Java Math abs()

    • As we cannot have all the logarithmic and trigonometric tables hard coded in our applications or data, Java provides Math – a very useful class for this purpose.
    • Math is final class of java.lang with static methods for performing the operations like exponential, logarithm, roots and trigonometric equations
    • The two most fundamental elements in Math are the ‘e’ and ‘pi’. These two constants are often required in the above calculations/operations
    • ‘e’ (base of the natural logarithm) and ‘pi’ (ratio of the circumference of a circle to its diameter) are provided as double fields with following values
    • Math.E – 2.718281828459045
    • Math.PI – 3.141592653589793
    • abs() method is used to calculate absolute(positive) value of the argument, where arguments can be int, long , double and float

    following are the overloaded abs methods from Math class –

    • public static int abs(int a)
    • public static double abs(double b)
    • public static float abs(float c)
    • public static long abs(long d)

    a, b c or d can be negative values, whereas returns will be positive value, but if argument is Integer.MIN_VALUE or Long.MIN_VALUE, the most negative representable int value or long value, the result is that same value, that is negative value. Above methods can take infinity and NaN as argument and return the same respectively

    Moving on with this article on Java Math abs()

    Examples of abs()

    Moving on with this article on Java Math abs()

    int
    public class Example1{
    public static void main(String args[]){
    int a = 56;
    int b = -47;
    //printing absolute value of int and Integer.MIN_VALUE
    System.out.println(Math.abs(a));
    System.out.println(Math.abs(b));
    System.out.println(Math.abs(Integer.MIN_VALUE));
    }
    }
    

    Output
    56
    47
    -2147483648

    Moving on with this article on Java Math abs()

    double –

    public class Example2
    {
    public static void main(String args[])
    {
    double x = -27.64;
    double y = -394.27;
    //printing absolute value of double type
    System.out.println(Math.abs(x));
    System.out.println(Math.abs(y));
    System.out.println(Math.abs(7.0 / 0)); //infinity
    }
    }
    
    
    Output –
    27.64
    394.27
    Infinity
    
    Moving on with this article on Java Math abs()
    
    float-
    
    
    public class Example3
    {
    public static void main(String args[])
    {
    float x = -63.02f;
    float y = -438.0f;
    //printing absolute value of float type
    System.out.println(Math.abs(x));
    System.out.println(Math.abs(y));
    }
    }
    
    

    Output
    63.02
    438.0

    Moving on with this article on Java Math abs()

    long-

    public class Example4
    {
    public static void main(String args[])
    {
    long x = 78730363;
    long y = -4839233;
    //printing absolute value of long type
    System.out.println(Math.abs(x));
    System.out.println(Math.abs(y));
    System.out.println(Math.abs(Long.MIN_VALUE));
    }
    }
    

    Output
    78730363
    4839233
    -9223372036854775808

    Now after executing the above program you would have understood the Java Math abs() Method. Thus we have come to an end of this article. If you wish to learn more, check out the Java Training by Edureka, a trusted online learning company. Edureka’s Java J2EE and SOA training and certification course is designed to train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.

    Got a question for us? Please mention it in the comments section of this article and we will get back to you as soon as possible.

    Comments
    0 Comments

    Browse Categories

    webinar REGISTER FOR FREE WEBINAR
    REGISTER NOW
    webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

    Subscribe to our Newsletter, and get personalized recommendations.