Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there restriction about Java package name length?

Tags:

java

Is there a restriction for the name length of a Java package name?

Like package com.abc.xxx.xxxx.xxxx

like image 705
lanyun Avatar asked Mar 21 '14 09:03

lanyun


People also ask

How long can a Java package name be?

2 Answers. Show activity on this post. In class files there is a practical limit imposed by the fact that the full qualified class name is stored as a (modified) UFT-8 sequence which can have a maximum length of 65535 bytes as the length is store as an unsigned short.

Do Java package names matter?

Naming ConventionsPackage 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 .

Is hyphen allowed in Java package name?

While using hyphens in the names of repository items is a recommended practice in AEM development, hyphens are illegal within Java package names.

Should Java package names be plural?

Yes perfectly acceptable to have plurals, look at Collections for example, it is a class which has many static methods which help when dealing with different flavours of collection.


2 Answers

In class files there is a practical limit imposed by the fact that the full qualified class name is stored as a (modified) UFT-8 sequence which can have a maximum length of 65535 bytes as the length is store as an unsigned short.

Storing the signature of a Generic class might require an even shorter package name. And it will be even more limited if you try to create a method taking the type as a parameter multiple times as the signature will contain the qualified type name for every parameter and the return type in a single sequence, but it is still limited to 65535 UTF-8 bytes in total.

like image 92
Holger Avatar answered Oct 23 '22 05:10

Holger


According to the Java language specification (3.8):

"An identifier is an unlimited-length sequence of Java letters and Java digits, the first of which must be a Java letter."

However, as @Holger points out, the JVM specification places an upper limit of 2^16 - 1 on string values that can be represented in a classfile constant pool. (See JVM spec 4.4.7)

Package names are part of fully qualified class names, and the latter are represented as classfile constant pool entries. So, the practical upper limit is on the length of the fully-qualified classname, not just the package name.

Hypothetically, if someone were to create a Java implementation that didn't use the standard classfile format, there is another limit ... the length of a Java String, which is 2^31 - 1 characters. (Note that the maximum length of a String is not specified in the JLS. It is the Java class libraries and the javadocs that specify this.)

However, the reality is that these implementation limits are so large that they should not present practical concerns.

like image 5
Stephen C Avatar answered Oct 23 '22 05:10

Stephen C