What is maximum length of java

Hi, I had a small question to ask. I had to find out what is the maximum length of a variable we declare in java. Like:

int some_name = 3126;

Here the length of the above "variable name"some_name is 9 characters. If I were to write a very long name instead like

int some_name_some_name_some_name_some_name_some_name_some_name_some_name_some_name_some_name_some_name_some_name = 3126;

java still doesn't give a compile error. So I wanted to confirm whether java allows unlimited length for a "variable name".

[615 byte] By [jdolphina] at [2007-11-27 11:22:20]
# 1

Why does it matter? Name your variables whatever you want--descriptive, but short enough to be reasonable to type and read--and if you hit the limit, the compiler will tell you. If you don't hit it, it doesn't matter.

jverda at 2007-7-29 14:54:14 > top of Java-index,Java Essentials,New To Java...
# 2

Yes you are right. I wouldn't be writing such long names for variables because that would confuse me :). But I just wished to know the limit in case its required for any interview.

jdolphina at 2007-7-29 14:54:14 > top of Java-index,Java Essentials,New To Java...
# 3

About 4 letters.

floundera at 2007-7-29 14:54:14 > top of Java-index,Java Essentials,New To Java...
# 4

> But I just wished to know the limit in case its

> required for any interview.

It won't be. And if it is, you don't want to work there.

jverda at 2007-7-29 14:54:14 > top of Java-index,Java Essentials,New To Java...
# 5

Alright, thanks.

Message was edited by:

jdolphin

jdolphina at 2007-7-29 14:54:14 > top of Java-index,Java Essentials,New To Java...
# 6

Have you tried it for the String also? what length String s="AAAAAAAAAAAAAA.............." could take?

javaqueuea at 2007-7-29 14:54:14 > top of Java-index,Java Essentials,New To Java...
# 7

http://java.sun.com/docs/books/jls/third_edition/html/lexical.html#3.8

ejpa at 2007-7-29 14:54:14 > top of Java-index,Java Essentials,New To Java...
# 8

> > But I just wished to know the limit in case its

> > required for any interview.

>

> It won't be. And if it is, you don't want to work

> there.

I agree. If they're asking stupid questions like that just tell them you'd do a search on the documentation and find the answer. Interviewers that think candidates should keep the whole APIs in memory just don't get the point.

BTW, a tip that could be useful to you. You didn't declare the variable in the proper way -- the Java standard is:

int someName;

or, for a constant:

final int SOME_NAME = 42;

java_knighta at 2007-7-29 14:54:14 > top of Java-index,Java Essentials,New To Java...
# 9

> BTW, a tip that could be useful to you. You didn't

> declare the variable in the proper way

Yes he did. Initializing at declaration is perfectly legitimate and common.

jverda at 2007-7-29 14:54:14 > top of Java-index,Java Essentials,New To Java...
# 10

>> BTW, a tip that could be useful to you. You didn't

>> declare the variable in the proper way

>Yes he did. Initializing at declaration is perfectly legitimate and common.

I think Java_Knight was refering to the OP not using camelCase, not the fact that he initialised at declaration (which is valid)

Vectorizeda at 2007-7-29 14:54:14 > top of Java-index,Java Essentials,New To Java...
# 11

> Yes you are right. I wouldn't be writing such long

> names for variables because that would confuse me :).

> But I just wished to know the limit in case its

> required for any interview.

If anyone asks you that in an interview, immediately call the interview to a halt and thank them for their time

georgemca at 2007-7-29 14:54:14 > top of Java-index,Java Essentials,New To Java...
# 12

There is no such boundary but pls ref sun naming convention

Prem_Sa at 2007-7-29 14:54:14 > top of Java-index,Java Essentials,New To Java...
# 13

> I think Java_Knight was refering to the OP not using camelCase

Yes, exactly ;-) I was just referring to the choice of the variable name.

java_knighta at 2007-7-29 14:54:14 > top of Java-index,Java Essentials,New To Java...
# 14

> There is no such boundary but pls ref sun naming

> convention

There is such a boundary, for practical reasons alone. From how I understand the JVM specs, it says in Sec. 4.5 under "name_index" that a field name has to be an entry in the constant_pool table, and Sec. 4.4.7 point out that the length for such an entry will be described by unsigned two bytes. So it's a maximum length of 65536 bytes in UTF8.

CeciNEstPasUnProgrammeura at 2007-7-29 14:54:14 > top of Java-index,Java Essentials,New To Java...
# 15

> >> BTW, a tip that could be useful to you. You

> didn't

> >> declare the variable in the proper way

>

> >Yes he did. Initializing at declaration is perfectly

> legitimate and common.

>

> I think Java_Knight was refering to the OP not using

> camelCase,

Ah, I see.

jverda at 2007-7-29 14:54:19 > top of Java-index,Java Essentials,New To Java...
# 16

> > There is no such boundary but pls ref sun naming

> > convention

>

> There is such a boundary, for practical reasons

> alone. From how I understand the JVM specs, it says

> in Sec. 4.5 under "name_index" that a field name has

> to be an entry in the constant_pool table, and Sec.

> 4.4.7 point out that the length for such an entry

> will be described by unsigned two bytes. So it's a

> maximum length of 65536 bytes in UTF8.

That would be a long variable name.

macrules2a at 2007-7-29 14:54:19 > top of Java-index,Java Essentials,New To Java...
# 17

> > There is no such boundary but pls ref sun naming

> > convention

>

> There is such a boundary, for practical reasons

> alone. From how I understand the JVM specs, it says

> in Sec. 4.5 under "name_index" that a field name has

> to be an entry in the constant_pool table, and Sec.

> 4.4.7 point out that the length for such an entry

> will be described by unsigned two bytes. So it's a

> maximum length of 65536 bytes in UTF8.

I am rather certain that neither local variable names nor member names end up in the constant pool.

jschella at 2007-7-29 14:54:19 > top of Java-index,Java Essentials,New To Java...
# 18

> http://java.sun.com/docs/books/jls/third_edition/html/

> lexical.html#3.8

Keep in mind that there are certain limitations to this.

For example method names are identifiers but there are definitely VM spec limits on a method name.

jschella at 2007-7-29 14:54:19 > top of Java-index,Java Essentials,New To Java...
# 19

> Have you tried it for the String also? what length

> String s="AAAAAAAAAAAAAA.............." could take?

This is limited by the constant pool size limitation specified by the VM specification.

jschella at 2007-7-29 14:54:19 > top of Java-index,Java Essentials,New To Java...
# 20

> I am rather certain that neither local variable names

> nor member names end up in the constant pool.

Actually, just about everything ends up there (in the .class file; you may be thinking of the interned strings pool).

Member names are referenced from the CONSTANT_Methodref_info structure: http://java.sun.com/docs/books/jvms/second_edition/html/ClassFile.doc.html#42041

While local variables -- assuming that you compiled with -g -- get there via the LocalVariableTable attribute: http://java.sun.com/docs/books/jvms/second_edition/html/ClassFile.doc.html#5956

kdgregorya at 2007-7-29 14:54:19 > top of Java-index,Java Essentials,New To Java...
# 21

> > I am rather certain that neither local variable

> names

> > nor member names end up in the constant pool.

>

> Actually, just about everything ends up there (in the

> .class file; you may be thinking of the interned

> strings pool).

No, I was just wrong.

jschella at 2007-7-29 14:54:19 > top of Java-index,Java Essentials,New To Java...
# 22

> > > I am rather certain that neither local variable

> > names

> > > nor member names end up in the constant pool.

> >

> > Actually, just about everything ends up there (in

> the

> > .class file; you may be thinking of the interned

> > strings pool).

>

> No, I was just wrong.

Seeing that was much less satisfying than I imagined it all these years.

(And yes, my wife did say the same thing on our wedding night.)

jverda at 2007-7-29 14:54:19 > top of Java-index,Java Essentials,New To Java...
# 23

I always thought that variable names were replaced with other symbols by the compiler, and that's why decompiling bytecode wouldn't give you the original variable names. Like myVar1 would be replaced by some symbol like a for example. Am I wrong? I'm mainly asking because I'm starting compiler construction in August, and we're going to build a java compiler, so I want to start picking up as much as I can.

hunter9000a at 2007-7-29 14:54:19 > top of Java-index,Java Essentials,New To Java...
# 24

> I always thought that variable names were replaced

> with other symbols by the compiler, and that's why

> decompiling bytecode wouldn't give you the original

> variable names. Like myVar1 would be replaced by some

> symbol like a for example. Am I wrong? I'm mainly

> asking because I'm starting compiler construction in

> August, and we're going to build a java compiler, so

> I want to start picking up as much as I can.

My thought as well.

But then as far as I can recall I only ever tried decompiling on non-debug builds.

And regardless of that I don't think it changes the point of this discussion because it still needs to be able to get them into those structures and that places a limit on them.

(Although C/C++ compilers when combined with linkers have no problem just truncating names so I suppose that is a possibility for a java compiler.)

jschella at 2007-7-29 14:54:19 > top of Java-index,Java Essentials,New To Java...
# 25

> I always thought that variable names were replaced

> with other symbols by the compiler, and that's why

> decompiling bytecode wouldn't give you the original

> variable names.

:; javac

Usage: javac <options> <source files>

where possible options include:

-g Generate all debugging info

-g:noneGenerate no debugging info

-g:{lines,vars,source}Generate only some debugging info

jverda at 2007-7-29 14:54:19 > top of Java-index,Java Essentials,New To Java...
# 26

public class Z {

void foo(int bar) {

int baz = bar;

}

}

:; javac Z.java<< No -g

jeff@shiro:/tmp 13:12:19

:; javap -c -l Z

Compiled from "Z.java"

public class Z extends java.lang.Object{

public Z();

Code:

0:aload_0

1:invokespecial#1; //Method java/lang/Object."<init>":()V

4:return

LineNumberTable:

line 1: 0

void foo(int);

Code:

0:iload_1

1:istore_2

2:return

LineNumberTable:

line 3: 0

line 4: 2

}

jeff@shiro:/tmp 13:12:23

:; javac -g Z.java<< -g

jeff@shiro:/tmp 13:12:28

:; javap -c -l Z

Compiled from "Z.java"

public class Z extends java.lang.Object{

public Z();

Code:

0:aload_0

1:invokespecial#1; //Method java/lang/Object."<init>":()V

4:return

LineNumberTable:

line 1: 0

LocalVariableTable:

Start Length Slot NameSignature

050thisLZ;

void foo(int);

Code:

0:iload_1

1:istore_2

2:return

LineNumberTable:

line 3: 0

line 4: 2

LocalVariableTable:

Start Length Slot NameSignature

030thisLZ;

031barI

212bazI

}

jverda at 2007-7-29 14:54:19 > top of Java-index,Java Essentials,New To Java...