Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Java decide when to import?

Tags:

java

import

class

Why doesn't Java need to import classes like Integer, String, etc. while it needs to import other classes?

like image 798
DonkeyKong Avatar asked Dec 23 '12 04:12

DonkeyKong


People also ask

How do import work in Java?

Java has an import statement that allows you to import an entire package (as in earlier examples), or use only certain classes and interfaces defined in the package. The import statement is optional in Java.

Is file automatically imported by Java?

Java automatic importsJava compiler automatically imports two packages: java. lang and the current package. The Constants class is located in the same package as the AutomaticImports which is referring to its version member. In this example, we refer to some classes that are automatically imported by Java compiler.

Do I need to import Java Lang package any time why?

No, java. lang package is a default package in Java therefore, there is no need to import it explicitly. i.e. without importing you can access the classes of this package.

When must we use import statements Java?

The import statement can be used to import an entire package or sometimes import certain classes and interfaces inside the package. The import statement is written before the class definition and after the package statement(if there is any). Also, the import statement is optional.


1 Answers

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.

like image 165
Mark Byers Avatar answered Sep 28 '22 13:09

Mark Byers