21

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.

Donato Szilagyi
  • 4,279
  • 4
  • 36
  • 53
gameover
  • 11,813
  • 16
  • 59
  • 70
  • 7
    What's the use of having separate packages if we could just have them all in one package and have it imported automatically? Think about that. – MAK Jan 15 '10 at 18:01
  • 5
    Ya..thought about it. Thats why the question!! Why is it not done? – gameover Jan 15 '10 at 18:03
  • Because otherwise you wouldn't really have packages at all, and you would instead have unreconcilable name clashes.. – user207421 Jul 10 '18 at 04:40

10 Answers10

33

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.

MC Emperor
  • 22,334
  • 15
  • 80
  • 130
danben
  • 80,905
  • 18
  • 123
  • 145
  • +1: Not only that, but some classnames appear in different namespaces even in the Java standard library. For example: java.util.List and java.awt.List. – Powerlord Jan 15 '10 at 18:17
  • 5
    That's kind of a "it is because it is" answer. Sure, if you wanted another Map (java.util), then you'd have to fully qualify it, but if you want another Process (java.lang) you'll still have to. The question remains: what's so special about the set of classes in java.lang that it's always imported, and nothing else is? Did Gosling think that we'd be using StrictMath more often than List? – Ken Jan 15 '10 at 18:31
  • 6
    I don't agree this is a meaningless answer. He asked why not autoimport other packages - he didn't ask why we autoimport `java.lang`. But if you are asking that, I would guess that the answer has something to do with the package description from the Javadocs - "Provides classes that are fundamental to the design of the Java programming language.". Without getting into a discussion of the validity of that statement, which is beyond the scope of this thread, I'd say that if you felt that a particular package was fundamental to a language you were designing, you might see fit to automatically – danben Jan 15 '10 at 18:49
  • 1
    @Ken. Well actually, yes, considering that `List` didn't exist in the first version of Java. – Tyler Aug 29 '11 at 17:55
  • @danben is there any other auto-imported packages ? – Aguid Apr 11 '17 at 08:43
  • @Aguid - Answer - no, – Stephen C Mar 08 '18 at 22:21
  • @MatrixFrog That makes sense, `List` didn't exists in the first version. But that opens the question of till when is it going to be like this? Perhaps build tools like Maven can be a middle ground solution here (configure auto imported packages). – Nader Ghanbari Jun 14 '18 at 17:43
4

All the good IDE's will resolve your imports automatically, only prompting when there is a conflict (two packages with the same classname).

BlueRaja - Danny Pflughoeft
  • 84,206
  • 33
  • 197
  • 283
2

Because java.lang have the core Java language classes, and java.util for example not.

But some other languages, like Groovy automatically imports java.util for you :)

Kico Lobo
  • 4,374
  • 4
  • 35
  • 48
1

I think the idea behind java.lang is that these classes all have some connection to the language and runtime which is special, and can't be implemented on your own. Primitive wrappers, VM security and permissions and inspection, package and class loading -- all things that must be built-in to the Java system. Everything in java.util, like collections, while incredibly useful, could be implemented in pure Java. Some parts of it (time zones come to mind) have even been implemented by third-party libraries even better.

Or at least, that was true back in the Java 1.0 days. Today, for example, Iterator is also integral to the language, since it's automatically used by for-each loops, right? But backwards-compatibility was always a big thing with Java, so we get to live with this inconsistency forever.

Ken
  • 1,066
  • 1
  • 10
  • 17
  • This doesn't really explain things. They could have just autoimported nothing or autoimported more than just java.lang. Autoimports are a convenience feature. – Antimony Nov 24 '12 at 05:04
0

@gameover, May be every java program needs the class that comes from java.lang,

but the java.util class contains the class that my be need or not that is depended on programmer. So the java have the default configuration for java.lang but we need to import to java.util classes according to our program.

0

java.lang package provides fundamental classes to built java programs. Object is the root of class hierarchy so it needs to be available for every programmer whether the programmer is beginner or expert.

While if we talk about other packages they are used for enlarging the programs. For example, java.util package which is only used when Collection classes are needed. While in every program Collection classes are not used while basic datatypes are essential for every java program.

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

P Sharma
  • 184
  • 9
0

There are different reason for to become java.lang package is default 1) Actually ,whatever we are declaring variable in java program.that will be stored in object,Object class is available in java.lang package.it is super class of class hierarchy,Many we have performed operation in object class method like thread programming. 2) Many time it is required class for developing program ,that class is available in java.lang package, So, it is necessary to import,when java people developed jdk, it was default package,

0

I came across with namespace collision even with java.lang.System (our application contains a System named class). An explicit import solved my problem, but it took some minutes until I pointed out that com.mycompany.classes.System isn't imported automatically for identifier System by Eclipse because it already exists in java.lang.

Anyways, it isn't a good idea to pollute the class scope with too much identifiers, because your code will org.classes.**look** com.application.**like** com.classes.**this**.

Auto-importing java.lang is a good idea because it contains the very core classes and interfaces used in java.

bofredo
  • 2,348
  • 6
  • 32
  • 51
gyorgyabraham
  • 2,550
  • 1
  • 28
  • 46
-2

Auto importing also leads some memory issue and some times it may conflict. Eg: Date Type in java & SQL .

Also whatever the base core java objects are defined in Java.lang package . Eg: any datatype & return type all are declared in java.lang package so to do some basic program also we need this package imports.

-4

Without java.lang package, development is not possible. But without java.util, java.io or any other packages, we can still write any number of programs. That is the reason why java.lang package is being imported by default.

Matthieu
  • 2,736
  • 4
  • 57
  • 87