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