How do I get the PI value?

The code snippet below show you how to obtain the PI value in Java. We use the Math.PI static field to get the value of PI.

package org.kodejava.example.math;

public class GetPIExample {
    public static void main(String[] args) {
        // The PI value represented by Math.PI
        System.out.println("PI = " + Math.PI);

        // Using the Math.PI to calculate area of a circle.
        double radius = 8;
        double circleArea = Math.PI * Math.pow(radius, 2);
        System.out.println("Circle Area = " + circleArea);
    }
}

Here is the program output:

PI = 3.141592653589793
Circle Area = 201.06192982974676
Wayan Saryada

Wayan Saryada

A programmer, runner, recreational diver, currently living in the island of Bali, Indonesia. Mostly programming in Java, creating web based application with Spring Framework, JPA, etc. If you need help on Java programming you can hire me on Fiverr.
Wayan Saryada

Leave a Reply