1

I'm curious to know if there is any limit on maximum number of characters or maximum number of dots(.) in a package name in Java? I referred this oracle doc but couldn't find the answer there. I also referred this thread which gave me another doubt that whether the name limits are OS(XP, Windows 7) dependent?

Thanks in Advance!!

Community
  • 1
  • 1
Chaitanya
  • 336
  • 2
  • 5
  • 13

2 Answers2

5

No, not in any sensible meaning. Java does not impose atrificial limit on package names. The underlying system, however, is another matter. Packages on Windows are represented as folders, and Windows limits path size to 260 characters: http://msdn.microsoft.com/en-us/library/aa365247.aspx

On a more theoretical note, even if we had a system that allowed arbitrary lengths in names, one could not possibly have a name longer than the maximum memory of the computer.

So Java does not limit names, but rather the JDK implementation is forced to impose system limits.

Jakub Zaverka
  • 8,816
  • 3
  • 32
  • 48
3

It's the class file format that has the limit in this case. The package name is a component of the internal binary name of a class. The internal binary name of the class is stored as a constant in the constant pool.

Regarding CONSTANT_Class_info:

The value of the name_index item must be a valid index into the constant_pool table. The constant_pool entry at that index must be a CONSTANT_Utf8_info (§4.4.7) structure representing a valid binary class or interface name encoded in internal form (§4.2.1).

Regarding individual values stored in the constants pool,

The length of field and method names, field and method descriptors, and other constant string values (including those referenced by ConstantValue (§4.7.2) attributes) is limited to 65535 characters by the 16-bit unsigned length item of the CONSTANT_Utf8_info structure (§4.4.7).

Note that the limit is on the number of bytes in the encoding and not on the number of encoded characters. UTF-8 encodes some characters using two or three bytes. Thus, strings incorporating multibyte characters are further constrained.

If a CONSTANT_Utf8_info can't exceed 65535 bytes, and it has to contain the package name followed by a slash followed by the class name, with a one character class name you could have a package name that's 65533 characters long.

You'd probably need to write your own classloader in order to load the class since it exceeds the limits of any file system or OS that I'm aware of.

Community
  • 1
  • 1
John R
  • 2,066
  • 1
  • 11
  • 17