简体   繁体   English

Java:访问包中的类,这是更好的方法吗?

[英]Java : Accessing a class within a package, which is the better way?

If I access a class within a package using fully qualified name, without importing it, whether it saves any memory? 如果我使用完全限定名称访问包中的类,而不导入它,是否可以保存任何内存?

Using fully qualified class name : 使用完全限定的类名:

java.lang.Math.sqrt(x);

Import package : 导入包:

import java.lang.Math;

Math.sqrt(x);

which is the better way : import the package or access using fully qualified name? 哪种方式更好:使用完全限定名称导入包或访问?

Thanking you.. 感谢您..

There is no performance difference between importing the package or using the fully qualified class name. 导入包或使用完全限定的类名之间没有性能差异。 The import directive is not converted to Java byte code, consequently there is no effect on runtime performance. import指令未转换为Java字节代码,因此对运行时性能没有影响。 The only difference is that it saves you time in case you are using the imported class multiple times. 唯一的区别是它可以节省您的时间,以防您多次使用导入的类。 This is a good read here 这是读好这里

No, it doesn't save you memory. 不,它不会挽救你的记忆。

Also note that you don't have to import Math at all. 另请注意,您根本不必导入Math Everything in java.lang is imported automatically. java.lang所有内容都会自动导入。

A better example would be something like an ArrayList 一个更好的例子就像ArrayList

import java.util.ArrayList;
....
ArrayList<String> i = new ArrayList<String>();

Note I'm importing the ArrayList specifically. 注意我是专门导入ArrayList I could have done 我本可以做到的

import java.util.*; 

But you generally want to avoid large wildcard imports to avoid the problem of collisions between packages. 但是您通常希望避免大型通配符导入以避免包之间发生冲突的问题。

They're equivalent. 他们是等同的。 The access is the same. 访问是一样的。

The import is just a convention to save you from having to type the fully-resolved class name each time. 导入只是一种约定,可以使您不必每次都输入完全解析的类名。 You can write all your Java without using import, as long as you're a fast touch typer. 只要你是一个快速触摸的东西,你就可以在不使用导入的情况下编写所有Java。

But there's no difference in efficiency or class loading. 但效率或类加载没有区别。

As already said, on runtime there is no difference (in the class file it is always fully qualified, and after loading and linking the class there are direct pointers to the referred method), and everything in the java.lang package is automatically imported, as is everything in the current package. 如前所述,在运行时没有区别(在类文件中它总是完全限定的,并且在加载和链接类之后有指向被引用方法的直接指针),并且java.lang包中的所有内容都被自动导入,就像当前包中的一切一样。

The compiler might have to search some microseconds longer, but this should not be a reason - decide for legibility for human readers. 编译器可能需要更长时间搜索几微秒,但这不应成为原因 - 决定人类读者的易读性。

By the way, if you are using lots of static methods (from Math , for example), you could also write 顺便说一下,如果你使用了很多静态方法(例如,来自Math ),你也可以写

import static java.lang.Math.*;

and then use 然后使用

sqrt(x)

directly. 直。 But only do this if your class is math heavy and it really helps legibility of bigger formulas, since the reader (as the compiler) first would search in the same class and maybe in superclasses, too. 但是只有当你的类数学很重并且它确实有助于更大的公式的易读性时才这样做,因为读者(作为编译器)首先会在同一个类中搜索,也可能在超类中搜索。 (This applies analogously for other static methods and static variables (or constants), too.) (这同样适用于其他静态方法和静态变量(或常量)。)

Import package is for better readability; 导入包是为了更好的可读性;

Fully qualified class has to be used in special scenarios. 必须在特殊情况下使用完全限定的类。 For example, same class name in different package, or use reflect such as Class.forName() . 例如,不同包中的相同类名,或使用反射如Class.forName()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM