Java Math: Constants
Generally, constants are values that do not change, they remain constant for the duration of the program. Some constants have their values set within your program and remain unchanged for the duration of the program and we will meet those in a later lesson. Other constants are mathematical constants that never change, like the ratio of a circle’s circumference to its diameter – that will never change no matter how big or small the circle is – and that constant is called Pi. Many mathematical constants are built into Java so you don’t need to remember them, we’ll look at two of those: Pi (π)and Euler number (e).
PI, π

PI, or π, is a mathematical constant that never changes. It represents the ratio of a circle’s circumference to its diameter. Like all Math constants, and methods as you will see later, PI is available to you by accessing the Math class:
java.lang.Math
To return the value of PI you should use dot syntax to inform Java that you wish to access the Math class and include the name of the class variable you require, in this case PI:
Math.PI
Let’s see how this might work in code:
System.out.println(Math.PI);
The output should be:
run: 3.141592653589793 BUILD SUCCESSFUL (total time: 0 seconds)
The number of digits returned is pretty long, this is because the datatype of the number returned is a double, and from what we learned in the Java Datatypes and Variables lesson we know that a double datatype is an 8 byte number that can store 15 decimal digits.
How can we change the number of decimal digits? Is there anything we have learned so far that might enable a reduction in the number of decimal digits? Give that problem some thought and when you have a solution check below to see if we agree.
Since Math.PI returns a double, how about we change the datatype to something smaller? In the the Java Datatypes and Variables lesson we discussed a number of common datatypes and one that might be useful is the float datatype – that also holds a large decimal point number but occupies half the number of bytes to that of a double, sufficient for storing 6 to 7 decimal digits.
In the lesson Java Datatype Conversion we learnt how to convert datatypes one one type to another, and to convert a double to a float we need to use Type Casting:
[java]System.out.println(Math.PI);
//Now, using Type Casting:
System.out.println((float)Math.PI);[/java]
Type this code into your IDE, the output should be:
run: 3.141592653589793 3.1415927 BUILD SUCCESSFUL (total time: 0 seconds)
Euler number, e
Named after the Swiss mathematician Leonhard Euler, the number e is a mathematical constant with a value of approximately 2.71828 and is the base of the natural logarithm.. Similar to PI, Euler’s number is available to you via the Math class, using similar dot notation only this time referring to the class variable E:
Math.E
Again similar to PI, the number that E returns is a double and may be converted to other datatypes to improve manageability. Let’s see how we might return E in Java:
System.out.println(Math.E); //Now, using Type Casting: System.out.println((float)Math.E);
Type this code into your IDE, the output should be:
run: 2.718281828459045 2.7182817 BUILD SUCCESSFUL (total time: 0 seconds)