What Is the Convention for Word Separator in Java Package Names

What is the convention for word separator in Java package names?

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.

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

  • java.sun.com - Code Conventions/Naming

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:

  • 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

  • JLS 6.1 Package Names

Naming conventions of composed package names

From the documentation on package naming convention:

Package names are written in all lower case to avoid conflict with the names of classes or interfaces

So this would leave you with the following two possibilities:

form_validator
formvalidator

Actually the documentation also makes it clear that underscore plays a special role when it appears in package names:

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" ... the suggested convention is to add an underscore.

So, underscore is suggested only in special cases, into which your naming problem does not seem to fall. So I would recommend formvalidator as the package name.

Underscore in package name in Java - is it bad?

Code Conventions could be found here!

I think code conventions are nothing to vary, so stick to such conventions if there exist any...

Can we use camel case in java package naming?

Please just answer the question

OK, then taking your question as a "a xor b", the answer is

com.google.payrolldivision;

as per https://docs.oracle.com/javase/tutorial/java/package/namingpkgs.html.

However, your title and post ask two very different questions, so it's hard to "just answer the question".

Java code convention for package names

From Java package naming conventions @ Wikipedia (emphasis added):

Packages are usually defined using a hierarchical naming pattern, with
levels in the hierarchy separated by periods (.) (pronounced "dot").
Although packages lower in the naming hierarchy are often referred to
as "subpackages" of the corresponding packages higher in the
hierarchy, there is almost no semantic relationship between packages.
The Java Language Specification establishes package naming conventions
to avoid the possibility of two published packages having the same
name. The naming conventions describe how to create unique package
names, so that packages that are widely distributed will have unique
namespaces. This allows packages to be separately, easily and
automatically installed and catalogued.

In general, a package name begins with the top level domain name of
the organization and then the organization's domain and then any
subdomains, listed in reverse order. The organization can then choose
a specific name for its package. Package names should be all lowercase
characters whenever possible.

For example, if an organization in Canada called MySoft creates a
package to deal with fractions, naming the package ca.mysoft.fractions
distinguishes the fractions package from another similar package
created by another company. If a German company named MySoft also
creates a fractions package, but names it de.mysoft.fractions, then
the classes in these two packages are defined in a unique and separate
namespace.

Complete conventions for disambiguating package names and rules for
naming packages when the Internet domain name cannot be directly used
as a package name are described in section 7.7 of the Java Language
Specification.


See also:

  • The Java Language Specification §7.7 Unique Package Names
  • Do you really use your reverse domain for package naming in java?

Java package naming legal syntax rules

It is a Java identifier, followed by N (periods+identifier)s.

PackageDeclaration:
{PackageModifier} package Identifier {. Identifier} ;

https://docs.oracle.com/javase/specs/jls/se16/html/jls-7.html#jls-7.4

An identifier is (incl. some nested definitions):

Identifier:
IdentifierChars but not a Keyword or BooleanLiteral or NullLiteral

IdentifierChars:
JavaLetter {JavaLetterOrDigit}

JavaLetter:
any Unicode character that is a "Java letter"

JavaLetterOrDigit:
any Unicode character that is a "Java letter-or-digit"

A "Java letter" is a character for which the method
Character.isJavaIdentifierStart(int) returns true.

A "Java letter-or-digit" is a character for which the method
Character.isJavaIdentifierPart(int) returns true.

https://docs.oracle.com/javase/specs/jls/se16/html/jls-3.html#jls-Identifier

IOS and Android package name

Following Java package naming conventions the reason for underscores for the Android package name is:

Naming Conventions

Package names are written in all lower case to avoid conflict with the names of classes or interfaces.

For iOS, many people use camelCase. The same case was used your iOS bundle ID or package name but there is no strict rule which case to use. This is only a convention and not a compiler rule.

Valid names consist of letters (lower or upper case), digits, underscores and start from a letter or underscore.

Should they need to be different?

Each platform follows its own conventions but there is no need for them to be different. As aforementioned - you can rename package as you wish using letters, digits and underscores.

What is the practice for package names for cross platform?

If possible - you can use the same package name but it will violate conventions, which is not desirable.

If not possible (e.g. project does not compile) - follow the rules.

IMHO, following conventions (as the also follow the rules) is the best solution.

You can find more information here about Java package naming conventions (this is a different resource).

Why should java package name be lowercase?

Directly from Oracle Docs

Package names are written in all lower case to avoid conflict with the
names of classes or interfaces.



Related Topics



Leave a reply



Submit