Why doesn\'t Java need to import classes like Integer, String, etc. while it needs to import other classes?
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.
java.lang is in-build, implicitly imported in java, does'nt need to be manually imported
As it contains very frequently used classes, they have made it optional to import just for your convenience
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
.
Integer,String etc classes are present in package java.lang which is default imported.
Because, they belongs to java.lang.*
package. And, it is implicitly import by the compiler. If you do, then it won't complain you.