Pi (π) is a well-known mathematical constant and its value is 22/7 or 3.14159265359. Programming languages also use this constant for mathematical calculation.
Here we discuss PI with respect to the Java programming language.
In Java PI is defined as a field in java.lang.Math class.
It is defined as
static double PI
and described as
The
https://docs.oracle.com/double
value that is closer than any other to pi, the ratio of the circumference of a circle to its diameter.
How to use PI in Java
PI is a static constant in Math class, so to access it we use class name. PI. It can also be accessed using object.
Math.PI
1 2 3 4 5 6 | public class MyClass { public static void main(String args[]) { System.out.println("Value of PI = " + Math.PI); } } |
1 | Value of PI = 3.141592653589793 |
Java Program to calculate Area of Circle using Math.PI
Write a Java Program to calculate the Area of Circle
1 2 3 4 5 6 7 8 9 10 11 12 | import java.util.Scanner; public class MyClass { public static void main(String args[]) { int r; Scanner sc=new Scanner(System.in); r=sc.nextInt(); double area=Math.PI*r*r; System.out.println("Area of Circle is = " + area); } } |
1 | Area of Circle is = 314.1592653589793 |
Write a Java program to calculate circumference of a Circle

1 2 3 4 5 6 7 8 9 10 11 12 | import java.util.Scanner; public class MyClass { public static void main(String args[]) { int r; Scanner sc=new Scanner(System.in); r=sc.nextInt(); double circumference=2*Math.PI*r; System.out.println("Circumference of Circle is = " + circumference); } } |
1 | Circumference of Circle is = 62.83185307179586 |
How to print PI (π) symbol in Java
1 2 3 4 5 6 7 8 9 | import java.util.Scanner; public class MyClass { public static void main(String args[]) { System.out.println("PI Symbol is \u03c0 "); System.out.println("PI Symbol is π "); } } |
1 2 | PI Symbol is π PI Symbol is π |
How to write pi in java
You can write Pi by using Unicode value ‘\u03c0’ or by using the string constant “π”.
1 2 | System.out.println("PI Symbol is \u03c0 "); System.out.println("PI Symbol is π "); |
How to use Math.PI in java
to use Math.pi Just use Math.PI as below
1 2 3 4 5 | public class MyClass { public static void main(String args[]) { System.out.println("Value of PI = " + Math.PI); } } |