How does Java decide when to import?

java.lang package is imported by default, no need to explicitly import it.


Classes in the java.lang package do not need to be imported (the compiler acts like they are always imported). This package includes core classes such as String, Enum, Runnable, NullPointerException, and of course, the primitive wrapper classes such as Integer and Double.


Because, they belongs to java.lang.* package. And, it is implicitly import by the compiler. If you do, then it won't complain you.


There is an implicit import of java.lang.*.

From the Java specification:

A compilation unit automatically has access to all types declared in its package and also automatically imports all of the public types declared in the predefined package java.lang.

Tags:

Java

Class

Import