This
import java.lang.Math.*;
imports all (accessible) types declared within Math.
This
import java.lang.Math;
is redundant because Math is part of java.lang which is imported by default.
Both will require that you use
Math.PI
to access the field.
This
import static java.lang.Math.PI;
imports the static member Math.PI so that you can use its simple name in your source code.
how to use math.pi in java - Stack Overflow
Is java.lang.Math.PI equal to GCC's M_PI? - Stack Overflow
[Java] Using PI question
why are java constants declared static? - Stack Overflow
Videos
This
import java.lang.Math.*;
imports all (accessible) types declared within Math.
This
import java.lang.Math;
is redundant because Math is part of java.lang which is imported by default.
Both will require that you use
Math.PI
to access the field.
This
import static java.lang.Math.PI;
imports the static member Math.PI so that you can use its simple name in your source code.
'Allow Math.PI as a reference to the PI constant' means that your code will have to look like this in order to work:
static double getCircumference(double radius ) {
return Math.PI * 2 * radius;
}
public static double getArea(double radius) {
return Math.PI * radius * radius;
}
What import java.lang.Math; does is importing the class java.lang.Math so you can reference it with Math instead of the qualified version java.lang.Math. import java.lang.Math.*; does the same for Math and all nested classes, but not it's members.
You're missing the multiplication operator. Also, you want to do 4/3 in floating point, not integer math.
volume = (4.0 / 3) * Math.PI * Math.pow(radius, 3);
^^ ^
Here is usage of Math.PI to find circumference of circle and Area
First we take Radius as a string in Message Box and convert it into integer
public class circle {
public static void main(String[] args) {
// TODO code application logic here
String rad;
float radius,area,circum;
rad = JOptionPane.showInputDialog("Enter the Radius of circle:");
radius = Integer.parseInt(rad);
area = (float) (Math.PI*radius*radius);
circum = (float) (2*Math.PI*radius);
JOptionPane.showMessageDialog(null, "Area: " + area,"AREA",JOptionPane.INFORMATION_MESSAGE);
JOptionPane.showMessageDialog(null, "circumference: " + circum, "Circumfernce",JOptionPane.INFORMATION_MESSAGE);
}
}
Note the following.
The two numbers are the same to 16 decimal places. That's almost 48 bits which are the same.
In an IEEE 64-bit floating-point number, that's all the bits there are that aren't signs or exponents.
The #define M_PI has 21 digits; that's about 63 bits of precision, which is good for an IEEE 80-bit floating-point value.
What I think you're seeing is ordinary truncation of the bits in the M_PI value.
What you want to do is print out the raw bit pattern for the PI values and compare them.
In Java use the http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Double.html#doubleToRawLongBits(double) method to get the long value that you should print as binary.
Java 5 gives:
- PI is 3.141592653589793
- Raw bits is 4614256656552045848
- Binary is 100000000001001001000011111101101010100010001000010110100011000
In C, you can do double pi = M_PI; printf("%lld\n", pi); to obtain the same 64bit integer: 4614256656552045848 (thanks Bruno).
So I know I can do something like a direct import (not sure if that's the real name) but without importing java.lang.Math I can do a Math.PI and use this value for pi. I have an program where I use it more than once, so I think I should make a constant out of it. Personally I don't know why I can't put a Math.PI wherever I need it, because it seems clear and readable already, but I think for my assignment I need to adhere to this convention of use a number more than once with meaning > make constant.
How can I do that though? I have an import java.lang.Math; at the top of my program. Then in my class (but before my main) I have a public static final double PI = Math.PI;..so was there no need to import the Math package? How should I best do this?
If a constant is not static, Java will allocate a memory for that constant in every object of the class (i.e., one copy of the constant per object).
If a constant is static, there will be only one copy of the constant for that class (i.e., one copy per class).
Therefore, if the constant has only one value, it should declared static.
If the constant might have different value for each object, for example the creation time of the object, it should not be declared static.
If it could vary by the instance of a class, then it's clearly not a constant. What would it mean to get a different value of pi for each instance of Math (not that Math even allows instances to be constructed)? Or a different case insensitive ordering for each instance of String?
That is perfectly acceptable, probably even the standard.
(public/private) static final TYPE NAME = VALUE;
where TYPE is the type, NAME is the name in all caps with underscores for spaces, and VALUE is the constant value;
I highly recommend NOT putting your constants in their own classes or interfaces.
As a side note: Variables that are declared final and are mutable can still be changed; however, the variable can never point at a different object.
For example:
public static final Point ORIGIN = new Point(0,0);
public static void main(String[] args){
ORIGIN.x = 3;
}
That is legal and ORIGIN would then be a point at (3, 0).
I would highly advise against having a single constants class. It may seem a good idea at the time, but when developers refuse to document constants and the class grows to encompass upwards of 500 constants which are all not related to each other at all (being related to entirely different aspects of the application), this generally turns into the constants file being completely unreadable. Instead:
- If you have access to Java 5+, use enums to define your specific constants for an application area. All parts of the application area should refer to enums, not constant values, for these constants. You may declare an enum similar to how you declare a class. Enums are perhaps the most (and, arguably, only) useful feature of Java 5+.
- If you have constants that are only valid to a particular class or one of its subclasses, declare them as either protected or public and place them on the top class in the hierarchy. This way, the subclasses can access these constant values (and if other classes access them via public, the constants aren't only valid to a particular class...which means that the external classes using this constant may be too tightly coupled to the class containing the constant)
- If you have an interface with behavior defined, but returned values or argument values should be particular, it is perfectly acceptible to define constants on that interface so that other implementors will have access to them. However, avoid creating an interface just to hold constants: it can become just as bad as a class created just to hold constants.