Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Length limit for a variable name in Java [duplicate]

Possible Duplicate:
Max name length of variable or method in Java

I was reading the java docs and it says “A variable's name can be any legal identifier — an unlimited-length sequence of Unicode letters and digits … “ in c++ the variable name length is around 255 characters depending on the compiler, so how is this handled in java does the compiler truncate the variable name after x number of characters, and if this is true what would be x ?

like image 798
Mike G Avatar asked Jan 08 '12 23:01

Mike G


People also ask

Is there a limit to variable name length?

There is no limit on the length of the variable name. A variable name cannot contain spaces.

What can be the length of a variable name?

ANSI standard recognizes a length of 31 characters for a variable name. However, the length should not be normally more than any combination of eight alphabets, digits, and underscores.

Is there any limitation for identifier length in Java?

An identifier should be of length 4-15 letters only. However, there is no limit on its length. But, it is good to follow the standard conventions. We can't use the Java reserved keywords as an identifier such as int, float, double, char, etc.

How many maximum characters can be used for naming variables?

Variable names are up to 64 characters long and can only contain letters, digits and nonpunctuation characters (except that a period (.) is allowed.


1 Answers

According to the class file format spec (under section 4.11):

The length of field and method names, field and method descriptors, and other constant string values 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.

This applies to local variables as well because of the LocalVariableTable pointing to CONSTANT_Utf8_info values for the variable names.

like image 111
prunge Avatar answered Nov 14 '22 21:11

prunge