Core Java

Pi in Java Example

In this post, we feature a comprehensive article about math.pi java.

1. Introduction

PI is a mathematics number defined as the circumference of any circle divided by its diameter. Java has provided the java.lang.Math class since version 1. It includes static constant math.pi java with the value of 3.141592653589793.

/*the double value that is closer than any other to pi, the ratio of the circumference of a circle to its diameter.*/
static double PI = 3.141592653589793;

In this example, I will print out the math.pi java value and use it to calculate a circle’s circumference and area.

2. Technologies Used

The example code in this article was built and run using:

  • Java 11
  • Maven 3.3.9
  • Eclipse Oxygen
  • Junit 4.12

3. Maven Project

3.1 Dependencies

I will include Junit in the pom.xml.


<project xmlns=""

3.2 Circle

In this step, I will create a Circle class which has a diameter data member and two methods:

  • calculateCircumference() – calculate a circle’s circumference by multiplying PI by its diameter.
  • calculateArea() – calculate a circle’s area by multiplying PI by the square of its radius.

package jcg.zheng.demo;

public class Circle {

	public Circle(double diameter) {
		this.diameter = diameter;

	private double diameter;

	public double calculateCircumference() {
		return Math.PI * diameter;

	public double calculateArea() {
		return Math.PI * Math.pow(diameter / 2, 2);

3.3. Circle Test

In this step, I will create a CircleTest class to print out Math.PI value and calculate a circle’s circumference and area.

  • print_Pi_value() – prints out Math.PI value.
  • calculateCircumference() – calculates the circumference of a circle whose diameter is 10 units.
  • calculateArea() – calculates the area of a circle whose diameter is 20 units.

package jcg.zheng.demo;

import static org.junit.Assert.assertEquals;

import org.junit.Test;

public class CircleTest {

	public void print_Pi_value() {
		System.out.printf("%.16f", Math.PI);
		assertEquals(3.14, Math.PI, 2);

	public void calculateCircumference() {
		double diameter = 10;
		Circle c = new Circle(diameter);

		assertEquals(31.42, c.calculateCircumference(), 2);
	public void calculateArea() {
		double diameter = 20;
		Circle c = new Circle(diameter);

		assertEquals(314.23, c.calculateArea(), 2);

Execute mvn test -Dtest=CircleTest and capture output here.


 T E S T S
Running jcg.zheng.demo.CircleTest
Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.126 sec

Results :

Tests run: 3, Failures: 0, Errors: 0, Skipped: 0

Note: Math.PI value is defined as 3.141592653589793.

4. Summary

PI is a mathematics number defined as the circumference of any circle divided by its diameter. In this example, I demonstrate through examples the Math.PI value and how to use it to calculate a circle’s circumference and area.

6. Download the Source Code

This example consists of a Maven project which demonstrates the Math.PI constant.

You can download the full source code of this example here: Pi in Java Example

Last updated on Jun. 15th, 2021

Want to know how to develop your skillset to become a Java Rockstar?

Join our newsletter to start rocking!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions


Mary Zheng

Mary has graduated from Mechanical Engineering department at ShangHai JiaoTong University. She also holds a Master degree in Computer Science from Webster University. During her studies she has been involved with a large number of projects ranging from programming and software engineering. She works as a senior Software Engineer in the telecommunications sector where she acts as a leader and works with others to design, implement, and monitor the software solution.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Newest Most Voted
Inline Feedbacks
View all comments
3 years ago

Oh, I thought you were going to calculate PI, not just use it. Useful post though, nevertheless.

Back to top button