So I want to print out the math functions, "PI" and "E."
Here is how I'm trying to do it:
System.out.println(Math.PI);
System.out.println(Math.E);
I think I have to import the math library.
So I want to print out the math functions, "PI" and "E."
Here is how I'm trying to do it:
System.out.println(Math.PI);
System.out.println(Math.E);
I think I have to import the math library.
E
and PI
are not functions, they're static fields (data members). That code will output their values correctly. You don't have to import anything, the Math
class is in the java.lang
package, which is imported by default. (It's the only package imported by default, I believe.)
You don't have to import anything here. The java.lang.Math
class should already be available as java.lang
package is imported by default
This works just fine:
/**
<P>{@code java MathXmpl}</P>
**/
public class MathXmpl {
public static final void main(String[] igno_red) {
System.out.println(Math.PI);
System.out.println(Math.E);
}
}
Output:
[C:\java_code\]java MathXmpl
3.141592653589793
2.718281828459045
Since Math
is in the java.lang
package, it does not need to be imported. java.lang
is the "default package" and everything in it is already implicitly imported for you.