DonkeyKong
DonkeyKong

Reputation: 818

How does Java decide when to import?

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

Upvotes: 44

Views: 9367

Answers (9)

user11309468
user11309468

Reputation:

For the question for "Is java.lang the only one auto-import package?" There is two packages.

  1. java.lang
  2. unnamed

you can look up here for more. https://www.quora.com/Which-package-is-available-by-default-in-java

Upvotes: 0

wizneel
wizneel

Reputation: 301

Integer,String etc classes are present in package java.lang which is default imported.

Upvotes: 1

sumit sharma
sumit sharma

Reputation: 1057

every class in java is in a package and if no package is defined then it is understood as in default package. And at the top of the package is java.lang.* so we don't need to import it an require to import other classes.

Upvotes: 1

vishal_aim
vishal_aim

Reputation: 7854

As it contains very frequently used classes, they have made it optional to import just for your convenience

Upvotes: 1

Nidhish Krishnan
Nidhish Krishnan

Reputation: 20751

java.lang is in-build, implicitly imported in java, does'nt need to be manually imported

Upvotes: 3

Alexis King
Alexis King

Reputation: 43912

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.

Upvotes: 9

Ravi
Ravi

Reputation: 31437

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

Upvotes: 6

Mark Byers
Mark Byers

Reputation: 839214

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.

Upvotes: 49

Subin Sebastian
Subin Sebastian

Reputation: 10997

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

Upvotes: 35

Related Questions