4

I was working on a little Java program and was using arrays so I had done:

import java.util.Arrays;

Later I started expanding on what I had previously done and decided I wanted to get input from the user, so at that point I added:

import java.util.Scanner;

Now a thought occurred. I know that I could just do:

import java.util.*

Then I'd just need 1 import line instead of two (or however many I end up needing), but does the wildcard in the import mean that it will import everything from that package regardless of if it's needed or not, or will only the selective functionality be pulled?

My instinct here is to write more code and only include the packages I know I need, but if it doesn't make a difference why would anyone import more levels/packages then they need to? (I'd rather just be lazy and write less code)

Mike
  • 47,263
  • 29
  • 113
  • 177
  • 8
    See also http://stackoverflow.com/questions/147454/why-is-using-a-wild-card-with-a-java-import-statement-bad – Mark Meyer Dec 20 '12 at 21:17
  • @NuclearGhost - Thanks for the link, so (answering the second half of the question) it sounds like it's useful to explicitly name packages/classes to import incase there are naming conflicts. Is that the idea of that thread? – Mike Dec 20 '12 at 21:26
  • definitely the case. It also makes things more maintainable in the long run. Future developers will know exactly which class you're using from the package – Mark Meyer Dec 20 '12 at 21:27

3 Answers3

10

Be clear about what import is doing. It does NOT mean loading .class files and byte code.

All import does is allow you to save typing by using short class names.

So if you use java.sql.PreparedStatement in your code, you get to use PreparedStatement when you import java.sql.PreparedStatement. You can write Java code forever without using a single import statement. You'll just have to spell out all the fully-resolved class names.

And the class loader will still bring in byte code from .class files on first use at runtime.

It saves you keystrokes. That's all.

It has nothing to do with class loading.

Personally, I prefer to avoid the * notation. I spell each and every import out. I think it documents my intent better. My IDE is IntelliJ, so I ask it to insert imports on the fly.

Laziness is usually a virtue for developers, but not in this case. Spell them out and have your IDE insert them for you individually.

if you type

import java.util.*;

you'll get to refer to Scanner and List by their short names.

But if you want to do the same for FutureTask and LinkedBlockingQueue you'll have to have this:

import java.util.concurrent.*;
duffymo
  • 305,152
  • 44
  • 369
  • 561
  • OK, *that's* what I was trying to ask. I didn't have the correct "java" terminology. So it doesn't matter if you use the wildcard or not (from a final package size) as it's not loading the classes that are imported. Just need to be careful when using it in case of naming conflicts. That it? – Mike Dec 20 '12 at 21:28
  • 2
    +1, an `import` is not the same thing as a `#include`. All it does is allow the compiler to properly resolve everything. Having an `import` statement doesn't result in the class actually being loaded. The only situations where the class is actually *loaded* are outlined in [JLS §12.4.1](http://docs.oracle.com/javase/specs/jls/se7/html/jls-12.html#jls-12.4.1). Note that imports aren't mentioned anywhere. – Brian Dec 20 '12 at 21:30
  • The word "link" makes me think of linking C/C++. I don't think you mean that. I wouldn't use the phrase "compiler to properly link everything." It's as I said: short names instead of fully-resolved ones. That's it. – duffymo Dec 20 '12 at 21:34
  • `Spell them out and have your IDE insert them for you individually` yeah, my "IDE" has been gedit and javac, so no auto completes for me... I guess I should change that – Mike Dec 20 '12 at 21:55
  • IntelliJ is having a Mayan apocolypse sale today, so you can pick up the best IDE on the market cheap. Or go with the community edition for free: http://www.jetbrains.com – duffymo Dec 20 '12 at 22:09
3

The wildcard imports everything in that package. However, use an IDE like Eclipse and it offers you the possibility to organize the imports.

asgoth
  • 35,552
  • 12
  • 89
  • 98
1

The wildcard imports all classes and interfaces in that package.

However, the wildcard import does not import other packages that start with the same name.

For example, importing java.xml.* does not import the java.xml.bind package.

Aaron Kurtzhals
  • 2,036
  • 3
  • 17
  • 21
  • er... sorry, I don't follow... `import java.xml.*` doesn't give you `java.xml.bind` functionality? I thought that was the point of the `*` so you don't have to explicitly ask for the individual ones. Like in my example, with `java.utils` in the question. – Mike Dec 20 '12 at 21:36
  • I updated my answer to clarify. The * imports all classes and interfaces in the package. What * does not do is import other packages. In your question, java.util.Scanner is a class, not a package. – Aaron Kurtzhals Dec 20 '12 at 21:47