Java – Import wildcards do you always import everything?

I'm working on a small program and using arrays, so I did:

import java.util.Arrays;

Later, I began to expand what I had done before and decided that I wanted to get input from the user, so I added:

import java.util.Scanner;

Now an idea has emerged I know I can do this:

import java.util.*

Then I only need one import line instead of two (or how much I finally need), but does the wildcard in the import mean that it will import all content from the package, whether it is required or not, or only the selective function is pulled?

My intuition is to write more code, including only the packages I know I need, but if there is no difference, why would anyone import more levels / packages? I'd rather be lazy than write less code

Solution

Know what the import is doing It does not mean loading Class file and byte code

All imports allow you to save your typing with a short class name

Therefore, if you use Java. Net in your code sql. Preparedstatement, when importing Java sql. Preparedstatement can be used when preparing a Preparedstatement You can write java code permanently without using a single import statement You just need to spell out all the fully resolved class names

And the class loader will still be used for the first time at run time Bytecode of class file

It saves you keystrokes That's it.

It has nothing to do with class loading

Personally, I prefer to avoid using the * notation I spell every export I think it better records my intentions My IDE is IntelliJ, so I ask it to insert and import dynamically

Laziness is usually a virtue of developers, but not in this case Spell them out and let your ide insert them separately

If you enter

import java.util.*;

You can refer to scanners and lists by short name

But if you want to do the same thing for futuretask and linkedblockingqueue, you will have to do this:

import java.util.concurrent.*;
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>