Author
Vasyl Malik
Senior Java Developer at CodeGym

Math.PI in Java

Published in the Java Developer group

What is “π” in mathematics?

The ratio of a circle's circumference to its diameter, which equals 22/7 and is represented by a constant value of 3.14159, is called "pi" in mathematics.

What is Math.PI in Java?

Math.PI is a static final double constant in Java, equivalent to in π Mathematics. Provided by java.lang.Math class, Math.PI constant is used to carry out multiple mathematical and scientific calculations like finding the area & circumference of a circle or the surface area and volume of a sphere. In real life, the “pi” quantity has a fundamental position with never-ending uses. Some of them are listed below.
  • Aerospace designers use pi to compute the area of the body of the aircraft.
  • Medical Science benefits from pi by using it to analyze the structure of the eye.
  • Biochemists use pi to study the composition of DNA.
  • Statisticians use pi to project the population dynamics of the state.
  • Pi has a core value in the current Global Positioning System (GPS) we have today.

Example

If you want to learn how to get and how to use the value of Math.PI in Java let’s have a look at the following executable example.

public class PiInJava {

	public static double circumferenceOfCircle(int radius) {

		return Math.PI * (2 * radius);
	}

	public static double areaOfCircle(int radius) {

		return Math.PI * Math.pow(radius, 2);
	}

	public static double volumeOfSphere(int radius) {

		return (4 / 3) * Math.PI * Math.pow(radius, 3);
	}

	public static double surfaceAreaOfSphere(int radius) {

		return 4 * Math.PI * Math.pow(radius, 2);
	}

	public static void main(String[] args) {

		int radius = 5;

		System.out.println("Circumference of the Circle = " + circumferenceOfCircle(radius));
		System.out.println("Area of the Circle = " + areaOfCircle(radius));
		System.out.println("Volume of the Sphere = " + volumeOfSphere(radius));
		System.out.println("Surface Area of the Sphere = " + surfaceAreaOfSphere(radius));

	}

}

Output

Circumference of the Circle = 31.41592653589793 Area of the Circle = 78.53981633974483 Volume of the Sphere = 392.6990816987241 Surface Area of the Sphere = 314.1592653589793
Math.PI in Java - 1

Conclusion

By now you must be familiar with the use of constant Math.PI in Java. Its application in Java is majorly based on your requirements & your sound understanding of its inherent mathematical value. Feel free to consult the article when you get stuck and like forever, keep practising and keep growing!
Comments (1)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
George Simpson Level 0, United States
22 September 2022
Since pi definitely doesn't equal 22/7 I must comment in a purely mathematical sense. Pi is truly an irrational number whose decimal representation never terminates or repeats; conversely, 22/7 is _rational_ because its decimal representation _will_ repeat indefinitely 3.142857 142857 142857, etc. A better statement would be as follows. The ratio of a circle's circumference to its diameter, which can be approximated (correctly to four significant figures) as 22/7 and is represented (correctly to five significant figures) by a constant value of 3.14159, is called "pi" in mathematics. When used for computer calculations, every binary representation of every non-binary real number is an approximation. Every negative power of 2 is also rational numbers because its decimal representation necessarily terminates (the negative powers are used to store the decimal portion of a real number). I'm saying, binary approximations of pi can never be exact, and a binary computer's implementation of pi is _always_ approximated. The fraction 22/7 is often presented to middle schoolers as a reasonable approximation of pi. Many measuring devices in the real world lose their accuracy around .001" so using 22/7 to calculate an area or volume does not degrade the significance of the result.