Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why autoimport only java.lang package?

Tags:

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.

like image 640
gameover Avatar asked Jan 15 '10 17:01

gameover


People also ask

Why is Java lang the default package?

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.

Why Java Lang is not imported?

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.

Why is Java Lang package basic Java package and is required to be automatically imported?

To avoid unnecessary load of other classes in program other packages are not auto imported while essential package java. lang is auto imported.

Why import Java Lang * is used?

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.


1 Answers

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.

like image 79
danben Avatar answered Nov 10 '22 04:11

danben