Java包名称中的单词分隔符的约定是什么?

本文翻译自:What is the convention for word separator in Java package names?

How should one separate words in package names? 如何在包名中单独添加单词? Which of the following are correct? 以下哪项是正确的?

  1. com.*.my_package (underscore) com.*.my_package (下划线)
  2. com.*.my-package (hyphens) com.*.my-package (连字符)
  3. com.*.MyPackage (CamelCase) com.*.MyPackage (CamelCase)

What is the general standard? 什么是通用标准?


#1楼

参考:https://stackoom.com/question/DL3g/Java包名称中的单词分隔符的约定是什么


#2楼

All three are not the conventions. 这三个都不是惯例。

Use com.*.mypackage . 使用com.*.mypackage

The package names do not follow camel casing or underscores or hyphens package naming convention . 包名称不遵循驼峰套管或下划线或连字符包命名约定

Also, Google Java Style Guide specifies exactly the same (ie com.*.mypackage ) convention: 此外, Google Java Style Guide指定了完全相同的(即com.*.mypackage )约定:

5.2.1 Package names 5.2.1包名称

Package names are all lowercase, with consecutive words simply concatenated together (no underscores). 包名称全部小写,连续的单词简单连接在一起(没有下划线)。 For example, com.example.deepspace , not com.example.deepSpace or com.example.deep_space . 例如, com.example.deepspace而不是 com.example.deepSpacecom.example.deep_space

Google Java Style Guide: 5.2 Rules by identifier type: 5.2.1 Package names . - Google Java样式指南:5.2按标识符类型的规则:5.2.1包名称


#3楼

The official naming conventions aren't that strict, they don't even 'forbid' camel case notation except for prefix ( com in your example). 官方命名约定并不严格,除了前缀(在您的示例中为com )之外,它们甚至不会“禁止”骆驼案例表示法。

But I personally would avoid upper case letters and hyphenations , even numbers. 但我个人会避免使用大写字母 和连字符 ,甚至数字。 I'd choose com.*.mypackage like Bragboy suggested too. 我会选择com.*.mypackage像Bragboy提出过。

(hyphenations '-' are not legal in package names) (连字符' - '在包名中不合法)

EDIT 编辑

Interesting - the language specification has something to say about naming conventions too. 有趣 - 语言规范也有关于命名约定的说法。

In Chapter 7.7 Unique Package Names we see examples with package names that consist of upper case letters (so CamelCase notation would be OK) and they suggest to replace hyphonation by an underscore ("mary-lou" -> "mary_lou") and prefix java keywords with an underscore ("com.example.enum" -> "com.example._enum") 第7.7章唯一包名称中,我们看到包含大写字母的包名称的示例(因此CamelCase表示法可以正常),他们建议用下划线(“mary-lou” - >“mary_lou”)和前缀java替换hyphonation带下划线的关键字(“com.example.enum” - >“com.example._enum”)

Some more examples for upper case letters in package names can be found in chapter 6.8.1 Package Names . 有关包名称中大写字母的更多示例,请参见章节6.8.1包名称


#4楼

Here's what the official naming conventions document prescribes: 以下是官方命名约定文档规定的内容:

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. 唯一包名称的前缀始终以全小写ASCII字母书写,应该是*域名之一,目前是comedugovmilnetorg ,或者是英文双字母代码之一识别ISO标准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

References 参考


Note that in particular, anything following the top-level domain prefix isn't specified by the above document. 请注意,特别是上述文档未指定*域前缀后面的任何内容。 The JLS also agrees with this by giving the following examples: JLS也同意这一点,给出以下例子:

  • com.sun.sunsoft.DOE
  • gov.whitehouse.socks.mousefinder
  • com.JavaSoft.jag.Oak
  • org.npr.pledge.driver
  • uk.ac.city.rugby.game

The following excerpt is also relevant: 以下摘录也是相关的:

In some cases, the internet domain name may not be a valid package name. 在某些情况下,互联网域名可能不是有效的包名。 Here are some suggested conventions for dealing with these situations: 以下是处理这些情况的一些建议惯例:

  • If the domain name contains a hyphen, or any other special character not allowed in an identifier, convert it into an underscore. 如果域名包含连字符或标识符中不允许的任何其他特殊字符,请将其转换为下划线。
  • If any of the resulting package name components are keywords then append underscore to them. 如果任何生成的包名称组件是关键字,则将下划线附加到它们。
  • If any of the resulting package name components start with a digit, or any other character that is not allowed as an initial character of an identifier, have an underscore prefixed to the component. 如果任何生成的包名称组件以数字或任何其他不允许作为标识符的初始字符的字符开头,则在组件前面加上下划线。

References 参考


#5楼

Anyone can use underscore _ (its Okay) 任何人都可以使用下划线_ (可以)

No one should use hypen - (its Bad practice) 没有人应该使用hypen - (其不良做法)

No one should use capital letters inside package names (Bad practice) 没有人应该在包名内使用大写字母(不良做法)

NOTE: Here "Bad Practice" is meant for technically you are allowed to use that but conventionally its not in good manners to write. 注意:这里的“不良实践”是指从技术上讲,你可以使用它,但传统上它不能以良好的方式书写。

Java包名称中的单词分隔符的约定是什么?

Source: Naming a Package(docs.oracle) 来源: 命名一个包(docs.oracle)


#6楼

Underscores look ugly in package names. 下划线在包名中看起来很难看。 For what is worth, in case of names compound of three or more words I use initials (for example: com.company.app.ingresoegresofijo (ingreso/egreso fijo) -> com.company.app.iefijo ) and then document the package purpose in package-info.java . 值得一提的是,如果三个或更多单词的名称复合,我使用缩写(例如: com.company.app.ingresoegresofijo (ingreso/egreso fijo) -> com.company.app.iefijo )然后记录包package-info.java用途。