How to Use Pi Constant in Java

An error occurred trying to load this video.

Try refreshing the page, or contact customer support.

Coming up next: Colors in Java: Class Constants & Codes

You're on a roll. Keep up the good work!

Take Quiz Watch Next Lesson
Your next lesson will play in 10 seconds
  • 0:04 Pi in Java
  • 1:34 An Example
  • 3:30 Lesson Summary
Save Save Save

Want to watch this again later?

Log in or sign up to add this lesson to a Custom Course.

Log in or Sign up

Speed Speed

Recommended Lessons and Courses for You

Lesson Transcript
Instructor: Martin Gibbs

Martin has 16 years experience in Human Resources Information Systems and has a PhD in Information Technology Management. He is an adjunct professor of computer science and computer programming.

Computer programs can help us with numerous math functions. Java provides a huge library of tools and functions for mathematical operations. This lesson covers the use of pi in Java.

Pi in Java

Java has very powerful math functions, including pi. If you need to work with pi in Java, there is PI, which is a built-in math function that's part of the overall Math class. For many Java programs, you'll notice that you have to import the parent class to get special tools such as importing input or output classes so we can accept user input. The Java Math class, however, is included in the lang (language) class, and that's imported by default. However, it's considered good programing practice to import these classes anyway. This shows right away that your program will be using math for its calculations. The import code looks like this:

import java.lang.Math.*;

Even though pi goes on seemingly forever, Java stores the value of pi as a constant, or 3.141592653589793. For programming purposes, this is precise enough. Normally we don't like the term 'good enough' in mathematics, but we can't have infinite values in our Java variables. For our purposes, and even complex gaming or medical systems, the precision is acceptable.

Since PI is a constant, you cannot set its value, say back to 3.14. It is a final variable, meaning that you cannot change its value. This is a good thing, since we wouldn't want any code to be able to change a key foundation of mathematics.

Here is the code to invoke Math.PI:

// let's figure the area of a circle
double radius = 15;
double area;
area = Math.PI * radius * radius;

When we run the program, this output is displayed:

Java pi basic output

An Example

Okay, let's now consider a little more complicated example. This one calculates the volume of a cylinder, which might come in handy for game programming or other fun programming. Recall that the volume of a cylinder is its area times the length. To get the area, we need to multiply pi times the radius squared. First, we will calculate area, then the volume. To make the code easier to follow, we'll set the radius and length as constants, like this:

import java.lang.Math.*;
public class Pie {
  public static void main(String[] args) {
   //radius and length
   double radius = 5;
   double len = 15;
   // calculate the area using PI
   double area = radius * radius * Math.PI;
   //and now volume
   double volume = area * len;
   System.out.println("Volume of cylinder is: " + volume);

Here's another useful application of both PI and Java's object-oriented nature. We'll create our own classes for a Circle and a Cylinder. That way, we can send different values to each of those classes and get back the volume or area, without having to re-write a lot of code, like this:

public class Circle
  // setup calculation of area
  public double calcArea(double r) {
   return Math.PI * r * r;

To unlock this lesson you must be a Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use

Become a member and start learning now.
Become a Member  Back
What teachers are saying about
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account