Does an import wildcard import everything all the time?

后端 未结 3 1606
无人及你
无人及你 2021-01-14 12:57

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

3条回答
  •  星月不相逢
    2021-01-14 13:33

    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.*;
    

提交回复
热议问题