Reputation: 818
Why doesn't Java need to import classes like Integer, String, etc. while it needs to import other classes?
Upvotes: 44
Views: 9367
Reputation:
For the question for "Is java.lang the only one auto-import package?" There is two packages.
you can look up here for more. https://www.quora.com/Which-package-is-available-by-default-in-java
Upvotes: 0
Reputation: 301
Integer,String etc classes are present in package java.lang which is default imported.
Upvotes: 1
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
Reputation: 7854
As it contains very frequently used classes, they have made it optional to import just for your convenience
Upvotes: 1
Reputation: 20751
java.lang is in-build, implicitly imported in java, does'nt need to be manually imported
Upvotes: 3
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
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
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
Reputation: 10997
java.lang
package is imported by default, no need to explicitly import it.
Upvotes: 35