I know that the package java.lang
is auto-imported by every java program we write, hence all the classes in it are automatically available to us.
My question is why not auto import java.util
and other packages too? That sure will save some typing :)
So please explain why is this not done.
Java compiler imports java. lang package internally by default. It provides the fundamental classes that are necessary to design a basic Java program. The important classes are Object, which is the root of the class hierarchy, and Class, instances of which represent classes at run time.
lang package anytime during running a program? 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.
To avoid unnecessary load of other classes in program other packages are not auto imported while essential package java. lang is auto imported.
The java. lang package is the default package in Java, by default, it will be imported. Therefore, there is no need to import this package explicitly. i.e. without importing you can access the classes of this package.
A good reason not to autoimport too much is to avoid namespace clashes. If everything in java.util
was imported automatically and then you wanted to refer to a different class named 'Map', for example, you would have to refer to it by its fully-qualified name.
In response to other answers in this thread, import
does not actually modify the internal representation of your class files. In fact, here is a link to the JVM spec describing the class file structure: see that imports are not stored anywhere.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With