导入通配符是否一直导入所有内容? [英] Does an import wildcard import everything all the time?

查看:262
本文介绍了导入通配符是否一直导入所有内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个小程序,并且正在使用数组,所以我已经完成了:

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

然后我只需要1个导入行而不是2个(或者我最终需要多个),但导入中的通配符是否意味着它将从该包中导入所有,无论是否需要,或者只提取选择性功能?

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)

推荐答案

明确导入正在做什么。它并不意味着加载 .class 文件和字节代码。

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.

因此,如果在代码中使用 java.sql.PreparedStatement ,则可以使用导入 java.sql.PreparedStatement 时, PreparedStatement 。您可以永久编写Java代码,而无需使用单个 import 语句。你只需要拼出所有完全解析的类名。

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.

类加载器仍然会带来 .class中的字节代码首次在运行时使用的文件。

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.

它有没有与类加载。

个人,我更喜欢避免使用*表示法。我拼写每一次导入。我认为它更好地记录了我的意图。我的IDE是IntelliJ,所以我要求它动态插入导入。

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.

懒惰通常是开发人员的一种美德,但在这种情况下并非如此。拼出它们并让你的IDE单独插入它们。

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

如果你输入

import java.util.*;

你可以参考扫描仪列出的短名称。

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

但是如果你想为做同样的事情FutureTask LinkedBlockingQueue 你必须拥有:

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

import java.util.concurrent.*;

这篇关于导入通配符是否一直导入所有内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆