• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us

How to Use PI in Java?

February 11, 2024 //  by Anees Asghar

“PI” is a static final double constant in Java that belongs to the “java.lang.Math” class. It is equivalent to the mathematical π and is used to solve various mathematical and scientific computations. Java users use the PI constant to address different real-world challenges, such as computing the area of a circle, the circumference of a circle, and so on. You can use it in Java as a predefined or as a user-defined constant, depending on your needs. 

This guide illustrates the use of the PI constant using various use cases.

What is PI

PI (π) is a mathematical constant used in different fields of life, such as mathematics, statistics, medicine, biochemistry, and others. Generally, it is used in mathematics to solve different Geometry problems, like finding the Circle Area, the Cylinder Volume, etc. Also, it is used in many other fields of life, for example, biochemists use it to understand the DNA structure, Statisticians use it to track/compute population dynamics, and so on. It is denoted by “π” and is equal to “22/7” or “3.141593”.

How to Use PI in Java

You can use the PI constant in Java either as a Predefined Constant or a User-Defined Constant. Let’s learn how the PI constant works in Java using the stated methods:

Method 1: How to Use PI as a Predefined Constant in Java

The PI constant is defined in Java’s Math class as follows:

public static final double PI = 3.14159265358979323846;

Important: In the above snippet, the “final” keyword indicates/ensures that the PI’s value will always remain the same(constant).

Syntax

As PI is a static constant, you can use it directly with the class name like this:

Math.PI;

Alternatively, you can import the Math class at the start of your program and then use it anywhere without using the class name. 

import static java.lang.Math.*;

Now, let’s jump into practical implementation of the PI constant:

Example 1: How to Use Math.PI Without Importing It

Suppose we have to find the circumference of a soccer ball whose radius is “2.25” inches. For this purpose, we will use the math formula “2πr”:

package exp;
public class ComputePI {
    public static void main(String args[]) {
      double ballRadius = 2.25;
      double circumferenceOfBall = 2* Math.PI * ballRadius;
      System.out.println("The circumference of a Ball is: " + circumferenceOfBall);
  }
}

We create a couple of double-type variables: “ballRadius” and “circumferenceOfBall”. We initialize the “ballRadius” with “2.25”. Up next, we use the PI constant to compute the circumference of the ball and store the resultant value in the variable “circumferenceOfBall”. Finally, we print the computed value on the console:

Important: In this example, we need Math.PI only once, so we call it directly without importing. However, if you have to use the PI constant multiple times, it is recommended to import it once and use it as many times as needed (without using the class name again and again).

Example 2: How to Import and Use Math.PI

Suppose a user enters a radius value and we are supposed to compute the Area of a Circle, the Circumference of a Circle, and the Surface Area of a Circle. For this purpose, we will use the math formulas “πr^2”, “2πr”, and “4πr^2”, respectively. But first, we will import the PI constant from the Math class statically as follows:

package exp;
import static java.lang.Math.PI;

public class ComputePI {
    public static void main(String args[]) {
double r = 7.25;
System.out.println("Circumference of a circle is: " + 2*PI*r);
System.out.println("Area of Circle is: " + PI*r*r);
System.out.println("Surface Area of the Circle is: " + 4*PI*r*r );
    }
}

We import the “Math.PI” at the start of our application and successfully use it throughout the program without the class name:

Method 2: How to Use PI as a Custom/User-Defined Constant in Java

Instead of importing and using a predefined PI constant, you can declare your own PI constant and initialize it with a value “22/7” or “3.141593”. To create a constant (non-modifiable), you must use the keyword “final” while variable declaration:

package exp;

public class ComputePI {
    public static void main(String args[]) {
double r = 7.25;       //declaring and initializing user-defined PI constant
final double piVal = 3.141593;
System.out.println("Circumference of a circle is: " + 2*piVal*r);
System.out.println("Area of Circle is: " + piVal*r*r);
System.out.println("Surface Area of the Circle is: " + 4*piVal*r*r );
    }
}

The output confirms that the user-defined PI constant works perfectly fine:

Java PI Vs Custom PI

The value of “Math.PI” is a little bit different from the custom PI (the difference lies in decimal digits, i.e., from the third digit onwards). The built-in “Math.PI” is more accurate/precise, as illustrated in the following example:

package exp;

public class ComputePI {
    public static void main(String args[]) {
    double numerator = 22, denominator = 7;
    final double piVal = numerator/denominator;
System.out.println("Predefined PI Constant: " + Math.PI);
System.out.println("User-defined PI Constant: " + piVal );
    }
}

The output snippet illustrates the difference between the predefined and custom PI constants:

That’s all about the use of the PI constant in Java. 

Conclusion

To use PI in Java, call the predefined PI constant of the “java.lang.Math” class. It is a static constant that is called/used directly with the class name like this “Math.PI”. To make your code concise, import the “Math.PI” statically at the start of your program and use it anywhere without using the class name. Also, you can create and use a custom PI constant by simply dividing “22” by “7”. 

It is recommended to use the predefined PI constant as it provides a more accurate value of π. This post has explained the use of the Pi constant in Java using suitable examples.

Category: Java

About Anees Asghar

Anees, a go-to expert of various technologies like PostgreSQL, Java, JS, and Linux. He has been contributing to the community through his words. A passion for serving the people excites him to craft primo content.

Previous Post: « How to Fix an ArrayIndexOutOfBoundsException in Java
Next Post: How to Use Stack.peek() Method in Java? »

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

How to Initialize an Array in Java

Introduction to Java Server Faces (JSF)

Introduction to Java 6.0 New Features, Part–1

Java 6.0 Features Part – 2 : Pluggable Annotation Processing API

Introduction to Java Server Faces(JSF) HTML Tags

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact