java包名中是否可以包含下划线?
提问:在开发的过程中经常遇这样一种情况,一个包名是由多个单词组成的,这是时候该不该用下划线分割呢?
例如,my package,com.example.mypackage or com.example.my_package ?
来看一下官方文档怎么说的:
Package names are written in all lower case to avoid conflict with the names of classes or interfaces.
包的名称都是用小写字母写的,以避免与类或接口的名称发生冲突。
回答:所以,一个包名由多个单词组成,全部需要小写。
URL地址:https://docs.oracle.com/javase/tutorial/java/package/namingpkgs.html
提问:java包名中不允许出现下划线吗?
官方是这样说的:
Companies use their reversed Internet domain name to begin their package names—for example, com.example.mypackage
for a package named mypackage
created by a programmer at example.com
.
公司使用他们的反向域名开始他们的包名-例如 域名 example.com 需要一个mypackage包,完整包名应该是 com.example.mypackage
但是有些情况下,域名可能不是一个有效的包名。例如包含-、包含java 关键字、数字开头等。这种情况下,官方建议我们使用下划线作为包名前缀(Package Name Prefix),但仅限于域名部分(Domain Name)。
Naming a Package (The Java™ Tutorials > Learning the Java Language > Packages)
The Java Tutorials have been written for JDK 8. Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.
See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.
教程是为 JDK 8编写的。本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。参见 Java 语言的变化,了解 javase 9和后续版本中更新的语言特性的摘要。
有关所有 JDK 发行版的新特性、增强、删除或弃用选项的信息,请参阅 JDK 发行说明。
Naming a Package
命名软件包
With programmers worldwide writing classes and interfaces using the Java programming language, it is likely that many programmers will use the same name for different types. In fact, the previous example does just that: It defines a Rectangle
class when there is already a Rectangle
class in the java.awt
package. Still, the compiler allows both classes to have the same name if they are in different packages. The fully qualified name of each Rectangle
class includes the package name. That is, the fully qualified name of the Rectangle
class in the graphics
package is graphics.Rectangle
, and the fully qualified name of the Rectangle
class in the java.awt
package is java.awt.Rectangle
.
随着全世界的程序员都在使用 Java 编程语言编写类和接口,很多程序员可能会对不同的类型使用相同的名称。实际上,前面的示例就是这样做的: 当 java.awt 包中已经有一个 Rectangle 类时,它定义了一个 Rectangle 类。尽管如此,如果两个类位于不同的包中,编译器允许它们具有相同的名称。每个 Rectangle 类的完全限定名都包含包名。也就是说,图形包中的 Rectangle 类的完全限定名称是 graphics。Rectangle,而 java.awt 包中 Rectangle 类的完全限定名是 java.awt。长方形。
This works well unless two independent programmers use the same name for their packages. What prevents this problem? Convention.
除非两个独立的程序员对他们的软件包使用相同的名称,否则这种方法很有效。
Naming Conventions
命名约定
Package names are written in all lower case to avoid conflict with the names of classes or interfaces.
为了避免与类或接口的名称发生冲突,包名称都是用小写写的。
Companies use their reversed Internet domain name to begin their package names—for example, com.example.mypackage
for a package named mypackage
created by a programmer at example.com
.
公司使用他们反向的互联网域名来开始他们的包名称ー例如,com.example.mypackage 用于一个名为 mypackage 的包,这个包是由 example. com 的一个程序员创建的。
Name collisions that occur within a single company need to be handled by convention within that company, perhaps by including the region or the project name after the company name (for example, com.example.region.mypackage
).
单个公司内部发生的名称冲突需要通过该公司内部的约定来处理,也许可以在公司名称后面包含区域或项目名称(例如,com.example.region.mypackage)。
Packages in the Java language itself begin with java.
or javax.
Java 语言本身中的包以 Java.or javax 开头。
In some cases, the internet domain name may not be a valid package name. This can occur if the domain name contains a hyphen or other special character, if the package name begins with a digit or other character that is illegal to use as the beginning of a Java name, or if the package name contains a reserved Java keyword, such as "int". In this event, the suggested convention is to add an underscore. For example:
在某些情况下,internet 域名可能不是有效的包名。如果域名包含连字符或其他特殊字符,如果包名以非法用作 Java 名称开头的数字或其他字符开头,或者如果包名包含保留的 Java 关键字,如“ int”,则可能发生这种情况。在这种情况下,建议的约定是添加一个下划线。例如:
Domain Name 域名 | Package Name Prefix 包名称前缀 |
---|---|
hyphenated-name.example.org |
org.example.hyphenated_name |
example.int |
int_.example |
123name.example.com |
com.example._123name |