PI in Java – Math.PI Java Example

PI in Java – Math.PI Java Example | To get and use the PI (π) value in Java Language, we can use Math.PI constant defined in the java.lang.Math class. The Math.PI is a static final double type variable having a double value that is closer than any other to π, the ratio of the circumference of a circle to its diameter.

public static final double PI = 3.14159265358979323846;

Since it is a static variable so, we can directly access it through its class name like Math.PI. Example:-

public class Test {
  public static void main(String[] args) {
     double x = Math.PI;
     System.out.println(x);
  }
}

Output:-

3.141592653589793

If we import the Math class statically and then we can access PI without calling through its class name.

import static java.lang.Math.*;
public class Test {
  public static void main(String[] args) {
    System.out.println(PI);
  }
}

The “import static java.lang.Math.*;” statement will import all static members of the Math class. But if we want to import only the PI variable of the Math class, not other static methods and variables of the Math class then we can use the “import static java.lang.Math.PI;” statement.

Learn more about:- static import in Java Language

import static java.lang.Math.PI;
public class Test {
  public static void main(String[] args) {
     System.out.println(PI);
  }
}

How to Use PI in Java

To use PI in Java, first, get the PI value by calling the PI variable of the Math class, and then use it in the program. Let us see this through an example:-

Java Program to Find the Area of a Circle using Math.PI

public class Area {
  public static void main(String[] args) {
     double radius = 10;
     double area = Math.PI*radius*radius;
     System.out.println(area);
  }
}

Output:-

314.1592653589793

In this example, we are accessing the PI value through the class name directly. Using the import statement we can get the PI value without using its class name.

import static java.lang.Math.*;
public class Area {
  public static void main(String[] args) {
     double radius = 10;
     double area = PI*radius*radius;
     System.out.println(area);
  }
}

In this example, even though we need only the PI value but we are importing all the static methods and variables of the Math class which are not being used in this program. Therefore it is better to import only the PI variable of the Math class to get the π value.

If we import the PI variable of the Math class through static import then only the PI variable will be available to the class, other static methods and variables will not be there.

import static java.lang.Math.PI;
public class Area {
  public static void main(String[] args) {
     double radius = 10;
     double area = PI*radius*radius;
     System.out.println(area);
  }
}

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *