Math.PI in Java – How to Get PI (π) Value in Java

In Java, to get the π (pi) value 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:-

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

Output:-

3.141592653589793

Java program to find the area of a circle using Math.PI

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

Output:-

314.1592653589793

If we import Math class statically and then we can access PI without calling through it’s class name.

import static java.lang.Math.*;
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 another static method and variables of Math class then we can use the “import static java.lang.Math.PI;” statement. Learn more about static import in Java

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

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

Also Learn

Solved:- PI in java, Java pi, math.pi java, Java math.pi, pi Java, math.pi in Java, how to use pi in Java, Java math pi, math pi Java, How to get pi in Java, How to write pi in java, Java pi constant, How to use math.pi in java, pi value in Java, pie in Java, Java pi math, pi en java, what is math.pi in java, how to use math.pi, using pi in java, use pi in Java, math pi in Java, what is pi in Java, how to do pi in java

Leave a Reply