Java com.* 包命名空间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2661698/
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
Java com.* package namespace
提问by Stan
It's quite often to see com.* package namespace. What does 'com' mean? Thanks.
经常会看到 com.* 包命名空间。“com”是什么意思?谢谢。
采纳答案by polygenelubricants
The naming convention for packages is specified in the JLS. Here is the relevant snippet (there's a lot more in the section):
JLS 中指定了包的命名约定。这是相关的片段(该部分还有很多):
JLS 7.7 Unique Package Names
JLS 7.7 唯一包名称
You form a unique package name by first having (or belonging to an organization that has) an Internet domain name, such as
sun.com
. You then reverse this name, component by component, to obtain, in this example,com.sun
, and use this as a prefix for your package names, using a convention developed within your organization to further administer package names.
您首先拥有(或属于拥有)Internet 域名(例如
sun.com
. 然后,您可以逐个组件地反转此名称,在本示例中,com.sun
使用组织内制定的约定来获取并将其用作包名称的前缀,以进一步管理包名称。
It's also given in Naming Conventionssection of Sun's code conventions document:
Sun 的代码约定文档的命名约定部分也给出了它:
Packages: The prefix of a unique package name is always written in all-lowercase ASCII letters and should be one of the top-level domain names, currently
com
,edu
,gov
,mil
,net
,org
, or one of the English two-letter codes identifying countries as specified in ISO Standard 3166, 1981.Subsequent components of the package name vary according to an organization's own internal naming conventions. Such conventions might specify that certain directory name components be division, department, project, machine, or login names.
Examples:
com.sun.eng
,com.apple.quicktime.v2
,edu.cmu.cs.bovik.cheese
Packages: 唯一的包名称的前缀总是用全小写的 ASCII 字母书写,并且应该是顶级域名之一,目前
com
,edu
,gov
,mil
,net
,org
, 或标识指定国家的英文双字母代码之一ISO 标准 3166, 1981。包名称的后续组成部分根据组织自己的内部命名约定而有所不同。此类约定可能指定某些目录名称组件是部门、部门、项目、机器或登录名。
例子:
com.sun.eng
,com.apple.quicktime.v2
,edu.cmu.cs.bovik.cheese
So com.
prefix in package names means the same as .com
suffix in domain names: "commercial".
所以com.
包名中的前缀与.com
域名中的后缀含义相同:“commercial”。
回答by T.J. Crowder
Java packages other than the standard libraries are typically named in reverse order of the domain of the vendor, e.g. Microsoft uses com.microsoft
, Apache may use org.apache
, etc. (See this linkfor details on naming conventions in Java.) This is to prevent naming conflicts. So the com
is just part of the domain name, it has no special meaning to Java.
除了标准库之外的 Java 包通常以供应商域的相反顺序命名,例如 Microsoft 使用com.microsoft
,Apache 可能使用org.apache
等。(有关Java 中命名约定的详细信息,请参阅此链接。)这是为了防止命名冲突。所以com
只是域名的一部分,它对 Java 没有特殊意义。
There is no requirementthat you name your packages that way, it's just a very good idea for any package that may remotely end up being used outside your organization.
没有要求您以这种方式命名您的包,对于可能最终在您的组织之外远程使用的任何包来说,这只是一个非常好的主意。
回答by Carl Smotricz
When it created Java, Sun established a convention that package names should be constructed starting with the reversed domain names of the company or individual creating the package. Because DNS guarantees that domain names are unique (i.e. given to only one organization or person), this avoids duplication.
在创建 Java 时,Sun 建立了一个约定,即包名应该从创建包的公司或个人的反向域名开始构建。因为 DNS 保证域名是唯一的(即只给一个组织或个人),这就避免了重复。
So Java packages by Microsoft have names starting with com.microsoft
, those from Sun are com.sun
and so on.
因此,Microsoft 的 Java 包名称以 开头com.microsoft
,Sun 的包名称com.sun
以此类推。
回答by bruno conde
comis derived from commercial. Like orgis derived from organization.
com源自于商业. 像org源自组织。
回答by trzewiczek
If you didn't do that before - download some source code of some Java application (jEditfor example) and look and the files structure in the sources. It will clarify everything a bit probably. in jEdit there are several different packages, so you'll see how everything works.
And by the way - if you never read anyone's code - go and do it ASAP!! Even if you don't understand everything or just a small parts - do it as it's a great school of good programming practices!!!
如果您之前没有这样做 - 下载一些 Java 应用程序的源代码(例如jEdit)并查看源代码中的文件结构。它可能会澄清一切。在 jEdit 中有几个不同的包,所以你会看到一切是如何工作的。
顺便说一句 - 如果你从来没有读过任何人的代码 - 尽快去做吧!即使您不了解所有内容或仅了解一小部分 - 也请这样做,因为这是一所优秀的编程实践学校!!!
回答by Mahee yadav
Package represent a directory that contain related group of class and interface. package are two type. 1. Built in package. 2. user define package. note. a package can contain sub package. eg. java.awt(here awt is a package); java.awt.event(here event is a sub package which present in java.awt)
包表示包含相关类和接口组的目录。包有两种类型。1. 内置封装。2. 用户定义包。笔记。一个包可以包含子包。例如。java.awt(这里awt是一个包); java.awt.event(这里的event是java.awt中的一个子包)