by Dinesh Thakur Category: Operators and Expressions

Most of the computer languages typically support advanced math operations (such as square root, trigonometric sine, cosine etc.) by way of function libraries. Java also provides a range of methods that support such functions in the standard libraries Math class stored in the package java.lang. Java's Math class contains a collection of methods and two constants that compute standard mathematical functions.

All the methods and constants available in the Math class are static; so they can simply be referred by just writing class name Math and name of the method or constant you want to use separated by a period (.)

//Program to Show various Methods of Math Class
 public class MathematicalOperations
       public static void main(String [] args)
          System.out.println("Absolute value -16 = " + Math.abs(-16));
          System.out.println("Sine of va1ue PI/2= " + Math.sin(Math.PI/2));
          System.out.println("Arc Cosine of -1  = " + Math.acos(-1));
          System.out.println("Upper limit of 7.8 = "+ Math.ceil(7.8));
          System.out.println("Lower limit of 7.8 = "+ Math.floor(7.8));
          System.out.println("Maximum of 15 and 20 = " +Math.max(15,20));
          System.out.println("Rounded value = " + Math.round(7.68));
          System.out.println("Square root of 4 = " + Math.sqrt(4.0));
          System.out.println("2 power 3 is  = " + Math.pow(2.0,3.0));
          System.out.println("Log of 1000 = " + Math.log(1000.0));

Mathematical Operations in Java Example

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.

Search Content