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

Answer from T.J. Crowder on Stack Overflow
Discussions

How do I use/import the Apache Commons Math Java library?
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
🌐 r/javahelp
4
1
January 19, 2025
In Java, you cannot import single methods from a class, so how would I do it in my language?
It's always legal to use the fullly qualified name of something instead of importing it in Java source code, like java.lang.System.out.println(libraries.standard.READ_LINE). JVM bytecode also does not have an "import" concept and always refers to things by their fully qualified names. If you'd like to compile to Java source code, you could first compile to an intermediate Java abstract syntax tree that always refers to things with their fully qualified names, then as one of the last operations iterate over the tree, hoisting as many names into import statements as you can. (Sortof the reverse operation of "name resolution" which is one of the first things a compiler does.) So basically this is a code formatting question. Note that Java source does not allow importing two things with the same name, so in some cases you won't be able to hoist all the imports. That's ok. More on reddit.com
🌐 r/ProgrammingLanguages
25
6
March 9, 2024
[Java] The import java.util.Math cannot be resolved
it is java.lang.Math More on reddit.com
🌐 r/learnprogramming
9
5
February 13, 2014
How come I didn't need to import java.lang.Math for my code?

java.lang is always imported by default for Java.

More on reddit.com
🌐 r/learnjava
8
2
January 29, 2015
🌐
Baeldung
baeldung.com › home › java › java numbers › a guide to the java math class
A Guide to the Java Math Class | Baeldung
January 8, 2024 - In this tutorial, we’re going to describe the Math class that provides helpful static methods for performing numeric operations such as exponential, logarithm, etc.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › Math.html
Math (Java Platform SE 8 )
October 20, 2025 - Java™ Platform Standard Ed. 8 ... The class Math contains methods for performing basic numeric operations such as the elementary exponential, logarithm, square root, and trigonometric functions.
🌐
W3Schools
w3schools.com › java › java_math.asp
Java Math
assert abstract boolean break byte case catch char class continue default do double else enum exports extends final finally float for if implements import instanceof int interface long module native new package private protected public return requires short static super switch synchronized this throw throws transient try var void volatile while Java String Methods
🌐
Linux Hint
linuxhint.com › import-math-in-java
How to Import Math in Java? – Linux Hint
To import Math in Java, use “java.lang” package or import Math class using “java.lang.Math.*” It will access all the methods and variables of Math class.
Find elsewhere
🌐
CodeGym
codegym.cc › java blog › java math › math pow() method in java
Math.pow() Method in Java
December 5, 2024 - Therefore, instead of writing a power function from scratch, we can take advantage of this library that is included in the Java Lang package. Math pow can be found in the java.lang package as a method of the Math library. It is used to calculate the power of numbers, both integers as well as doubles.
🌐
JanBask Training
janbasktraining.com › community › java › should-i-import-a-math-library-and-if-so-how
Should I import a math library, and if so how? | JanBask Training Community
August 6, 2025 - The math module provides functions for square roots, trigonometry, logarithms, constants like π (pi) and e, and much more. ... Once imported, you can access its functions using the dot notation.
🌐
DEV Community
dev.to › mohamad_mhana › mastering-the-math-class-in-java-5ad4
🧮 Mastering the Math Class in Java - DEV Community
October 3, 2025 - The Math class in Java is a utility class that provides a rich set of static methods for performing...
🌐
Emory
cs.emory.edu › ~cheung › Courses › 170 › Syllabus › 04 › java-lib.html
Importing methods in the Java library
import java.lang.Math in our program. Summary: importing methods in the Java library ·
🌐
Quora
quora.com › How-do-I-import-a-math-class-in-Java
How to import a math class in Java - Quora
Answer (1 of 4): Math class is from java.lang package. So you dont have to import Math class. Also the methods and variables in Math class are static. So you can call them using class name.
🌐
Medium
medium.com › @toimrank › java-math-class-6ed5e23f2db0
Java Math Class. Java.lang.Math package in java provides… | by Imran Khan | Medium
September 21, 2022 - Java Math Class Java.lang.Math package in java provides Math class. Math class provides us some of the predefined method such as min, max, sqrt etc. Below are the examples of some of the methods from …
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-lang-math-class-java-set-2
Java.lang.Math Class in Java | Set 2 - GeeksforGeeks
July 23, 2025 - // Java program explaining lang.MATH class methods // incrementExact(), log10(), pow() import java.lang.*; public class NewClass { public static void main(String[] args) { // Use of incrementExact() method int f1 = 30, f2 = -56; f1 =Math.incrementExact(f1); System.out.println("Incremented value of f1 : "+f1); f2 =Math.incrementExact(f2); System.out.println("Incremented value of f2 : "+f2); System.out.println(""); // Use of log10() method double value = 10; double logValue = Math.log10(value); System.out.println("Log10 value of 10 : "+logValue); System.out.println(""); // Use of pow() method double b = 10, e = 2; double power = Math.pow(b,e); System.out.println("Use of pow() : "+power); } } Output :
🌐
TutorialsPoint
tutorialspoint.com › static-import-the-math-class-methods-in-java
Static import the Math Class Methods in Java
July 30, 2019 - import static java.lang.Math.sqrt; public class Demo { public static void main(String[] arg) { double num = 36.0; System.out.println("The number is: " + num); System.out.println("The square root of the above number is: " + sqrt(num)); } }
🌐
iO Flood
ioflood.com › blog › math-class-java
Exploring Java's Math Class: Methods and Examples
February 29, 2024 - If you’re working on applications where precision is critical, such as scientific or financial applications, you might want to consider using StrictMath. However, for most general purposes, the Math class should suffice. Remember, Java’s Math class and StrictMath class are tools at your disposal.
🌐
Reddit
reddit.com › r/javahelp › how do i use/import the apache commons math java library?
r/javahelp on Reddit: How do I use/import the Apache Commons Math Java library?
January 19, 2025 -

I have been trying to use the Apache Commons Math library in my code. I have been having trouble importing it. I'm new to downloading libraries/packages outside of the built-in java packages. It feels like I have tried everything, and I might just be missing an obvious detail. I am using a Windows 11 machine.

I have tried the following with the most recent release of the Apache Commons Math library (3.6.1) and the most recent version (4.0).

The error that I get every compile time is: package org.apache.commons does not exist.

I have installed IntelliJ IDE and tried

  • adding it as a library in the project structure (under project settings)

  • and adding it as a dependency in the build.gradle file (implementation 'org.apache.commons:commons-math4:4.0').

I have installed Eclipse IDE and tried

  • importing the JAR files into the src folder.

  • I have tried following the steps in this article (add User Library -> add external JAR(s) -> fill in Javadoc location -> configure build path).

I have tried adding the directories containing the JAR files to CLASSPATH using Control Panel (edit system variables).

I'm still somewhat confused as to why there are multiple JAR files in the directory that you get after unzipping the library and as to which one(s) I'm supposed to be using directly. But, I tried extracting the class files using the jar xf command in the terminal from what I thought were the important JAR files (commons-math4-legacy-4.0-beta1.jar and commons-math3-3.6.1.jar). I then tried

  • adding the resulting org directory to CLASSPATH

  • and tediously grouping all of the class files from all of the subdirectories into one single directory and adding that directory to CLASSPATH.

I have tried using these directories by compiling with javac -cp path/to/JAR/file/filename.jar *.java, which by my understanding does the same thing as adding them to CLASSPATH in Control Panel but I tried it anyway.

I even tried downloading the source version of the library and collecting all of the .java files into one single directory.

I also tried what the answerer suggested here, and it did not work.

Do you know what I am doing wrong? Let me know if I need to provide any more info. Thank you!

Top answer
1 of 2
2
You mentioned build.gradle. That's good, you want to be using a dependency tool like maven or gradle. The latest version I see in the maven central repository is 3.6.1 . Add that to your build.gradle with implementation 'org.apache.commons:commons-math3:3.6.1' You might need to right-click your project and do something like "Refresh Gradle Dependencies". That tells your IDE to download the new files. Now in your code, you need to import whatever Apache class you need. For example, let's say you want to use the ArithmeticUtils class. You would need to import org.apache.commons.math3.util.ArithmeticUtils into your code. Now you can use the methods from ArithmeticUtils.
2 of 2
1
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
🌐
Medium
medium.com › @AlexanderObregon › beginners-guide-to-java-math-21edc9cc1ee0
Java Math Guide for Beginners | Medium
March 9, 2024 - As we go deeper into Java’s ... of the java.lang package, which means it is automatically available and does not require an import statement....
🌐
Reddit
reddit.com › r/programminglanguages › in java, you cannot import single methods from a class, so how would i do it in my language?
r/ProgrammingLanguages on Reddit: In Java, you cannot import single methods from a class, so how would I do it in my language?
March 9, 2024 -

Hey y'all, I'm writing a transpiled language, which, you guessed it, transpiles to Java.

Now, I was planning on doing a import statement like this:

incorp standard {
print_line,
read_line,
STD_SUCCESS,
STD_FAILURE

}

which would transpile to something like this:

import libraries.standard;
import libraries.standard.STD_FAILURE; 
import libraries.standard.print_line; 
import libraries.standard.read_line; 

Problem is, I found out that you can't actually import a single method from a class in Java, so how would I go about fixing the problem? One solution I thought about would be that when importing a single function, it actually transpiles the single function to the Java code, while when importing the full library it imports the library as a object.

🌐
Coderanch
coderanch.com › t › 629237 › java › java-lang-Math-methods-import
Use java.lang.Math methods without import in IDE [Solved] (Beginning Java forum at Coderanch)
February 22, 2014 - programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums · this forum made possible by our volunteer staff, including ... ... I am able to use a Math method without importing the java.lang.Math.