Java:导入语句与完全限定名称?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11907245/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 06:52:38  来源:igfitidea点击:

Java: import statement vs fully qualified name?

javaimport

提问by Jonathan Pitre

I've tried finding an answer to this both online and in my own set of knowledge, but I cannot seem to find a definitive, clear answer.

我已经尝试在网上和我自己的知识范围内找到这个问题的答案,但我似乎无法找到一个明确、明确的答案。

Suppose I'm using only one class from another package only once which needs to be imported, say myPack.anotherPackage.ClassName.

假设我只使用另一个包中的一个类只需要导入一次,例如myPack.anotherPackage.ClassName.

What is the difference, if any, between using an import statement:

使用import 语句之间有什么区别(如果有):

import myPack.anotherPackage.ClassName;

versus using a fully qualified name:

与使用完全限定名称相比

myPack.anotherpackage.ClassName classInst = new myPack.anotherpackage.ClassName();

?

?

Obviously this question only applies if ClassNameis only used once.

显然,这个问题仅适用于ClassName仅使用一次的情况。

回答by sundar

Import statementsmake your code more readable, since you are not cluttering the code with the complete package.

导入语句使您的代码更具可读性,因为您不会用完整的包来弄乱代码。

In case if there is a conflict of ClassNames, then only in that case it is advisable to go for fully qualified names.

如果存在 冲突ClassNames,那么只有在这种情况下才建议使用完全限定名称

回答by Eugene

None, in my opinion. Look at the bytecode - there will be no difference.

没有,在我看来。看看字节码 - 没有区别。

 javap -c <your class>

回答by Kumar Vivek Mitra

1.Its more about the readabilityvalue thanof functionalimportance.

1.它更多的readability价值functional重要性。

2.importstatement provides a cleaner codethan using the full package name along with theclassor interfacename.

2.import语句提供了一个cleaner codethan 使用完整的包名和classorinterface名称。

3.But you must take care during importing packages which are ambiguous like.

3.但是你在导入包的时候必须小心,比如像这样不明确的包。

  `java.awt.List` and `java.util.List`

Then you need to import one fully and one with full package name, like below

然后你需要导入一个完整的包名和一个完整的包名,如下所示

import java.util.List;

and 

java.awt.List l = new java.awt.List();

回答by RamKr

There is no difference after code is compiled. In both case byte code will have fully qualified names i.e. class names are replaced with qualified names by compiler automatically. Its just to make programmers life easy, where we don't have to write fully qualified name to refer to a class.

代码编译后没有区别。在这两种情况下,字节码都将具有完全限定名称,即编译器自动将类名称替换为限定名称。它只是为了让程序员的生活更轻松,我们不必编写完全限定的名称来引用类。

回答by Gabe Correa

There is no real difference, especially for your specific simple case cited here. See Java: Use import or explicit package / class name?

没有真正的区别,尤其是对于此处引用的特定简单案例。请参阅Java:使用导入或显式包/类名?

回答by RNJ

From my knowledge no difference. The only difference is the verbosity of your code. Generally the more verbose the harder it is to read. Perhaps looks at the bytecode?

据我所知没有区别。唯一的区别是代码的冗长。一般来说,越冗长越难阅读。也许看字节码?

回答by amitmah

The import and package are used for naming and access protection (default) Simple reason for this is there can be many classes with same name.

import 和 package 用于命名和访问保护(默认) 原因很简单,可以有许多具有相同名称的类。

In order to differentiate between them we need to separate them under packages and import these when required.

为了区分它们,我们需要在包下将它们分开并在需要时导入它们。

There is no difference in importing a class or directly typing fully qualified name, however there are some minute differences. 1. Most importantly if you import all classes in package (i.e. import xyz.*;) The classes can still have naming problem for example if there are multiple general imports you may be using wrong class at runtime.

导入类或直接键入完全限定名称没有区别,但存在一些细微差别。1. 最重要的是,如果您导入包中的所有类(即 import xyz.*;),这些类仍然可能存在命名问题,例如,如果有多个通用导入,您可能在运行时使用了错误的类。

  1. In case of dynamically loading classes at runtime for example JDBC driver registration at runtime, you need to provide full qualified classname as parameter so it can be loaded dynamically.
  1. 在运行时动态加载类的情况下,例如在运行时注册 JDBC 驱动程序,您需要提供完全限定的类名作为参数,以便它可以动态加载。

In general use following best practice: 1. Never use import abc.*; format of import and always import individual classes (eclipse or some IDE can auto correct imports to * and need to disable this) 2. You have to use fully qualified class name when you have already imported a class of same name and want to use another class with same name under same program. 3. Hope it helps :)

通常使用以下最佳实践: 1. 永远不要使用 import abc.*; 导入格式并始终导入单个类(eclipse 或某些 IDE 可以自动更正导入到 * 并且需要禁用此功能) 2. 当您已经导入了一个同名的类并想要使用另一个类时,您必须使用完全限定的类名同一个程序下同名的类。3.希望它有帮助:)