8 Answers
8

The bound you will most likely meet first is that of the maximum number of entries in the constant pool per class which is 65535. This will allow for a few thousand case blocks of small complexity. The constant pool contains one entry for each numeric or string literal that is used at least once in the class but also one or more entries for all field, method and/or class reference as these entries are composed on behalf of other constants that must be present in the constant pool as well. I.e. a method reference entry consists of a reference to a string entry for the signature of the method and a reference to the class entry of the declaring class. The class entry itself again references a string entry for the class name.

The absolute upper bound for a switch ignoring or reusing the code in the case blocks is slightly less than 2^30 cases since each case has a jump target which is a signed 32 bit integer (see tableswitch and lookupswitch instructions) and thus needs 4 bytes per case and the byte code size for each method is limited to slightly less than 2^32 bytes. This is because the byte code is wrapped in a code attribute and the length of a attribute is given as a unsigned 32 bit integer. This size is fruther reduced because the code attribute has some header information, the method needs some entry and exit code and the tableswitch statement needs some bytes for itself with its min/max values and at most 3 bytes of padding.

Interesting answer (thanks for the links). However, does it apply to switch? The constant pool is used for the "layout of classes, interfaces, class instances, or arrays". A switch statement is likely to be compiled into a jump table (an array!) so the theoretical limit is 2 G-cases per switch and the constant pool limits only the number of arrays, i.e. the switch statements. Of course, in practice you'll run out of memory much earlier.
–
Igor F.Jun 14 '12 at 8:46

Yes, the case constants are located directly in the byte code and not in the constant pool but the code in each case block is likely to require some number of constant pool entries. You could of course try to minimize the use of constant pool entries, i.e. by compiling 5 as 4 + 1 or by invoking the same methods with the same arguments but in different order which would allow to work around the limited size of the constant pool.
–
x4uJun 14 '12 at 10:10

But that limit applies to any Java class, not only switch statements, right?
–
Igor F.Jun 14 '12 at 10:22

2

You're answering a question about limitations of the Java language by referring to limitations of bytecode which is plain wrong. There are plenty of Java compilers targeting other languages than bytecode. That should highlight the fact that Java spec != JVM spec.
–
aioobeJun 14 '12 at 12:53

1

Actually the question does not ask for a Java language based answer explicitly and I guess it is reasonable to assume that a question about Java relates to a environment that is bound to the restrictions of the byte code format at some point if nothing else is specified in the question.
–
x4uJun 14 '12 at 13:50

@Nicktar: :D Can you please share your research or something, As it is hard to believe!!! Personally I have seen 1800 lines method and dam sure that has many more characters then 65535. stackoverflow.com/questions/6570343/…
–
manurajhadaJun 14 '12 at 9:02

@manurajhada bugs.sun.com/view_bug.do?bug_id=4262078 The 65535 is the maximum size of the generated bytecode (not source code). There must be at least one byte of generated bytecode per switch case. Thus the limit must be less than 65535 cases.
–
emoryJun 14 '12 at 13:35

Yeah, i saw that bug when i was searching for the related information, and thats bytecode not the characters. Nicktar wrote about characters. Thanks Emory.
–
manurajhadaJun 14 '12 at 13:48

@manurajhada Yeah, characters is wrong. It has been a while since I researched the exception and memory failed me... It's bytes in the bytecode but that applies here too...
–
NicktarJun 15 '12 at 8:54

Reading the question, the answers, and the comments, I don't see why it is relevant. You can certainly have more cases than you can manually write. And, in the improbable case that you machine-generate your code, there are better choices than switches in Java.

Not entirely true. There is an upper bound.
–
BlenderJun 14 '12 at 5:54

upper bound is the number of cases u want right ?
–
NamalakJun 14 '12 at 5:57

No, upper bound is defined by the maximum number of characters per method, which is 65535. Methods longer than that wouldn't compile, stating "Code too large to compile" or "code too large for try statement".
–
NicktarJun 14 '12 at 8:47

3

@Nicktar : Thats rubbish. There is not any logical upper bound. The maximum number of character in a method is another issue.
–
Priyank DoshiJun 14 '12 at 8:53

1

@PriyankDoshi then could you please show me a switch statement with let's say 75000 cases (generated code would be perfectly ok)...
–
NicktarJun 15 '12 at 8:46