Java Pi8 May 2025 | 3 min read Programming is used to solve real-life problems that may include implementing different mathematical formulas. And these formulas are used in various mathematical constants and functions. What is Pi?The Pi is a constant value used in different formulas in geometry like calculating circumference, area, volume, etc. It is a mathematical constant that is defined as the circumference of a circle divided by its diameter. The value of a constant pi is approximately 3.14. Java provides built-in constant field of Pi that belong to java.lang,Math class. The following program shows the use of constant value pi without using the built-in constant field. SamplePi.java Output: Radius of circle: 5 Area of circle is: 78.5 Circumference of circle is: 31.400000000000002 In the above code, the pi value is set to 3.14 using a final double variable pi. And the area and circumference are calculated and displayed. Pi in JavaThe Java Math class provides methods for implementing numeric operations like logarithm, square root, trigonometric functions, minimum or maximum number. The pi is a field defined in the Math class as a static variable of type double. To access this constant the Java program has to import java.lang.Math class. As it's a static variable we can directly access it using the Math.PI into the Java program. The following program shows the use of Math.PI variable in a Java program. SamplePi2.java Output: Radius of circle: 5 Area of circle is: 78.53981633974483 Circumference of circle is: 31.41592653589793 In the above code, the Math.PI is used instead of declaring a local variable. And the area and circumference of the circle is displayed on the console. Program for calculating the volume of a cylinder using built-in variable and user-defined variableSamplePi3.java Output: Volume of cylinder by using built-in variable PI is: 1178.0972450961724 Volume of cylinder by using the user-defined Pi value is: 1177.5 The above Java code demonstrates both the ways of using Pi constant in a program. The area of a cylinder is calculated using multiplication operation and displayed using both methods. In this article, we have discussed the mathematical constant Pi, how it can be implemented in a Java program and a program to demonstrate it. |
Java Variant
In Java, Variant is a class that is defined in two different packages. javax.ws.rs.core.Variant org.eclipse.swt.ole.win32.Variant javax.ws.rs.core.Variant It is in-built in JDK. The class belongs to javax.ws.rs.core.Variant. It extends the Object class. the class is used for abstraction for a resource representation variant that contains information about media type, language...
3 min read
Pipes in Multithreading Programs in Java
Programming with many threads frequently requires thread communication. The idea of pipes is one of the many inter-thread communication techniques that Java offers. Java pipes are mainly used for unidirectional data transfer between two threads for inter-thread communication. Through this method, data may be controlled and...
5 min read
MetaClass in Java
Java itself does not have the concept of a MetaClass. Net, but the ideas behind MetaClasses can be found in many libraries and frameworks. It is noteworthy that in Java, MetaClass is usually understood as the class that contains meta-information about other classes and enables dynamic...
5 min read
DAO Class in Java
Data Access Object patterns, often known as DAO patterns, are used to divide high level business services from low level data accessing APIs or actions. The members of the Data Access Object Pattern are listed below. Data Access Object Interface: The Data Access Object Interface specifies the...
3 min read
Java Program to Demonstrate the Non-Lazy Initialization Thread-Safe
In Java, threads are independent units of execution that can run concurrently, allowing for improved processing speed by utilizing multiple CPU cores. Thread safety in Java refers to the property of a program or code that can handle and execute correctly regardless of the number of...
4 min read
Three-way operator | Ternary operator in Java
The one and only conditional operator that accepts three operands is the three-way operator in Java. Java programmers frequently use it as a one-line alternative to the if-then-else expression. The ternary operator can be used in place of if-else statements, and it can even be used...
3 min read
How to call a concrete method of abstract class in Java
The ability to call concrete methods of abstract classes is an important concept to understand when programming in Java. Abstract classes provide a way to define a standard set of behaviours and features that multiple classes can share without implementing them from scratch. These classes can...
5 min read
MinguoDate equals() method in Java with Examples
The java.time.chrono.MinguoDate class has equals() method. To compare this Minguo date with another Minguo date, use the MinguoDate class. Syntax: public boolean equals(Object obj) Parameter: To compare this Minguodate with an equivalent object, this method requires an equivalent object as an input. Return Value: If the two...
2 min read
Final Method Overloading in Java| Can We Overload Final Methods
? Java, being an object-oriented programming language, provides a powerful mechanism known as method overloading, allowing developers to define multiple methods with the same name but different parameters within the same class. However, when it comes to final methods, a question arises: can we overload final methods...
6 min read
Java vs. JavaScript
Java vs JavaScript Java is an object-oriented, general purpose programming language (though it is not entirely object-oriented as it contains primitive types). Java codes are platform-independent, meaning java codes can run on any platform which is supporting Java. There is no need for re-compilation of code. Java...
3 min read
We request you to subscribe our newsletter for upcoming updates.

We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India