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
🌐
Reddit
reddit.com › r/learnjava › how come i didn't need to import java.lang.math for my code?
r/learnjava on Reddit: How come I didn't need to import java.lang.Math for my code?
January 24, 2015 -

I had this line of code:

int generator = 1 + (int) (Math.random() * 19);

http://puu.sh/sGVps/b318650d6b.png

The program I create works perfectly but i was wondering why I didn't need to import lang.math despite using random.

Discussions

Is the Math class a standard class of Java? - Stack Overflow
You definitely do not have to import it like the Scanner class, which isn't a standard class of Java. Perhaps I do not understand what a standard class is. Can someone explain this for me? ... Yes, you do have to import Math to use it. But most of its methods are static, so you'd need to do either ... More on stackoverflow.com
🌐 stackoverflow.com
Why does the **Math** class not need to be imported?
Find step-by-step Computer science solutions and the answer to the textbook question Why does the Math class not need to be imported?. More on quizlet.com
🌐 quizlet.com
1
[JAVA]Why do we have to import classes like Random and Scanner? Why not just have it built into the language?
EDIT: Based on other responses below, let me clarify how I'm responding. I'm assuming OP genuinely is asking why the classes cannot be built in. You certainly could do an implicit import of the necessary classes instead of building them into the language itself. I raise three issues in my post, but with an implicit import, only the first issue remains. The responses here about memory are absolutely not correct. Why not just have all of these classes already built in the language It adds complexity to the language for no gains. The only advantage you get is nominal: not having to type import. However, you pay a huge price. First, you introduce a large number of reserved keywords into the language. Let's say you create a new PRNG. What do you call the class? Random? No, you can't do that, because the name is already in use. So everyone has to be aware of all the different class names that are built in. And how many classes are we talking about here? Are all the class names even unique? Can you even state that across the Java platform, there are no class names that are the same? Second, you create implementation complexity. When you state that Scanner is actually a part of the language, it has to implemented as a new grammar rule. Lexing and parsing and optimization now need to take this name into account, for no benefit, because like I said, these classes don't actually have any unique grammar behind them. You do not want exploding complexity to implement and maintain in your compilers. Third, you create language inflexibility. You can't create a lean version and implementation of the Java platform, because to implement the language, you have to implement all those classes. They are built into the language itself, after all. More on reddit.com
🌐 r/learnprogramming
34
47
July 19, 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 24, 2015
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api
Math (Java Platform SE 8 )
October 20, 2025 - JavaScript is disabled on your browser · Frame Alert · This document is designed to be viewed using the frames feature. If you see this message, you are using a non-frame-capable web client. Link to Non-frame version
🌐
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
🌐
Quora
quora.com › If-I-import-util-*-do-I-have-to-import-the-math-and-format-class-as-well
If I import util *, do I have to import the math and format class as well? - Quora
Answer (1 of 4): You should not import anything you do not use in your code. While it does not affect final output it just takes space and extra things in your source decreases readability of your code.
🌐
Huda Tutorials
hudatutorials.com › java › lang › math
java.lang.Math Class in Java, Static Import
June 30, 2021 - No, you need not to import the Math class in Java. Since Math class is in the java.lang package , the Math class does not need to be imported . However , in programs extensively utilizing these functions , a static import can be used .
Find elsewhere
🌐
Sololearn
sololearn.com › en › Discuss › 1527906 › does-the-math-class-needs-to-be-imported-in-java-because-im-tring-to-use-the-mathpow-and-doesnt-work-and-if-so-how-do-i-do-it
Does the math class needs to be imported in java because im tring to use the math.pow and doesnt work.. and if so how do i do it | Sololearn: Learn to code for FREE!
I mean, you could import static java.lang.Math; 1st Oct 2018, 5:41 PM · LunarCoffee · 0 · thank you it worked but tring to go on and running the code tells me 'time limit exceeded' whats up with that? any idea? 1st Oct 2018, 4:19 PM · ThermalBlast22 · 0 · wierd its not long at all i think but its my first one so what do i know..
🌐
Quizlet
quizlet.com › science › computer science
Why does the Math class not need to be imported? | Quizlet
We don't have to import Math class because it is in the java.lang package which provides fundamental classes for the programming in Java language (e.g.
🌐
FavTutor
favtutor.com › blogs › java-math-class
Math Class in Java & Methods (with Examples)
October 9, 2023 - Here are some tips to keep in mind: Avoid Redundant Imports: While it's possible to import the Math class using the import statement, it's not always necessary. If you only need to use a few methods or variables from the Math class, it's better ...
🌐
Reddit
reddit.com › r/learnprogramming › [java]why do we have to import classes like random and scanner? why not just have it built into the language?
r/learnprogramming on Reddit: [JAVA]Why do we have to import classes like Random and Scanner? Why not just have it built into the language?
July 19, 2014 -

for example every time you want to ask the user for input or a random number generator you have to type.

import java.util.Scanner;
import java.util.Random;

at the top of your code. Then you have to create the objects inside the main method.

public static void main(String[] args){
    Scanner keyboard = new Scanner(System.in);
    Random rand = new Random();
}

Why do we have to do the import command? Why not just have all of these classes already built in the language, then we can just create the objects?

Top answer
1 of 5
36
EDIT: Based on other responses below, let me clarify how I'm responding. I'm assuming OP genuinely is asking why the classes cannot be built in. You certainly could do an implicit import of the necessary classes instead of building them into the language itself. I raise three issues in my post, but with an implicit import, only the first issue remains. The responses here about memory are absolutely not correct. Why not just have all of these classes already built in the language It adds complexity to the language for no gains. The only advantage you get is nominal: not having to type import. However, you pay a huge price. First, you introduce a large number of reserved keywords into the language. Let's say you create a new PRNG. What do you call the class? Random? No, you can't do that, because the name is already in use. So everyone has to be aware of all the different class names that are built in. And how many classes are we talking about here? Are all the class names even unique? Can you even state that across the Java platform, there are no class names that are the same? Second, you create implementation complexity. When you state that Scanner is actually a part of the language, it has to implemented as a new grammar rule. Lexing and parsing and optimization now need to take this name into account, for no benefit, because like I said, these classes don't actually have any unique grammar behind them. You do not want exploding complexity to implement and maintain in your compilers. Third, you create language inflexibility. You can't create a lean version and implementation of the Java platform, because to implement the language, you have to implement all those classes. They are built into the language itself, after all.
2 of 5
9
Why not just have all of these classes already built in the language It's important to know the difference between "the language" and the "standard library". The language mostly consists of the reserved words and how you use them. For example, defining classes and creating objects. The standard library is all of the things you can use with the language without having to add additional libraries. In java this is generally called the JDK. There is a special package in the JDK where everything in it is automatically imported, as you're requesting. This is the packaged named 'java.lang'. This is why, for example, you don't have to ever import String or Math. So, you're basically asking, why isn't the entire JDK under java.lang (or, why not have the entire JDK in a nameless package)? The biggest problem would be name collisions. There are thousands of classes in the JDK and you'd never be able to reuse any of those names. It would also likely make compiling noticeably longer as it'd have to find all the definitions from a huge flat listing instead of a defined hierarchy. There are many other reasons but I'll let you look it up. The concept here is called namespaces. Good question and good luck!
🌐
Quora
quora.com › How-do-I-import-a-math-class-in-Java
How do I import a math class in Java?
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.
🌐
Sololearn
sololearn.com › en › Discuss › 2816992 › how-can-i-import-javalangmaths-to-use-exponents-mathpow
How can I import java.lang.maths to use exponents? (Math.pow) | Sololearn: Learn to code for FREE!
June 20, 2021 - No need to import Math class.. You can directly use Math.pow(2,3) returns 8.0 ... Katharina Hohenfels round, pow, ceil are methods of Math class. As you have already used Math.round() without importing so you can also use Math.pow() without ...
🌐
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.
🌐
TutorialsPoint
tutorialspoint.com › static-import-the-math-class-methods-in-java
Static import the Math Class Methods in Java
July 30, 2019 - The Math class is not required along with the method sqrt() as static import is used for the java.lang package. The number num and its square root is displayed. A code snippet which demonstrates this is as follows: double num = 36.0; System.out.println("The number is: " + num); System.out....
🌐
Educative
educative.io › answers › how-to-use-the-mathrandom-method-in-java
How to use the Math.random() method in Java
The range of this random number ... number. This method belongs to the java.lang.Math class, so you need to import this class before implementing​ this method....
🌐
DataCamp
datacamp.com › doc › java › import
import Keyword in Java: Usage & Examples
By importing static methods from the java.lang.Math class, you can directly use methods like sqrt and pow without prefixing them with Math.. Avoid Unnecessary Imports: Only import the classes or packages you need. Unnecessary imports can clutter your code and may lead to ambiguity.