2

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.

user3314801
  • 417
  • 2
  • 4
  • 7

3 Answers3

3

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.)

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
2

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

sadhu
  • 1,429
  • 8
  • 14
0

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.

aliteralmind
  • 19,847
  • 17
  • 77
  • 108