Raji A C
Raji A C

Reputation: 2351

Switch statement in Java

How many cases are possible for a switch statement in Java? For example if we are checking an integer how many case blocks are possible?

Upvotes: 16

Views: 5520

Answers (8)

Priyank Doshi
Priyank Doshi

Reputation: 13151

Infinite!! There is no such restriction.

Upvotes: -2

Dzinx
Dzinx

Reputation: 57784

16377. At least for a simple code like:

public class SwitchLimit {

    public static void main(String[] args) {
        int x = 0;
        switch(x) {
        case 0:
        ...
        case 16376:
        default:
        }
        System.out.println("done.");
    }

}

You can have 16377 case statements in this example (not counting default) and if you add a case 16377:, the code won't compile with the following error:

The code of method main(String[]) is exceeding the 65535 bytes limit

As others pointed out, this number will probably be significantly lower if your method actually does anything that makes sense.

Upvotes: 5

Igor F.
Igor F.

Reputation: 2699

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.

Upvotes: 1

x4u
x4u

Reputation: 14077

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.

See: Limitations of the Java virtual machine and The Constant Pool in the Java Virtual Machine Specification

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.

Upvotes: 15

Chandra Sekhar
Chandra Sekhar

Reputation: 19492

It depends on your requirement. you can have that many cases of range int type. As the range of int type is finite and after that concept of integer cycle will come into the picture.

As the size of int ranges from -2,147,483,648 to 2,147,483,647, so you can have a case for each number of them. So there is a limited number of case in case of integer.

But if you want to use String in case, then you can have unlimited number of cases as said by Bohemian.

Upvotes: 4

manurajhada
manurajhada

Reputation: 5380

No limit of case statements in a switch. At worst you can get heap space but not in easy way.

Upvotes: 2

Felix Christy
Felix Christy

Reputation: 2193

The total number of cases will be maximum number that int can take depending on the hardware. Have a look at datatypes in java

So, you will have the entire range as possible number of case blocks.

Upvotes: 2

Bohemian
Bohemian

Reputation: 424973

There is no limit, except the size of your JVM to accommodate all the bytecode

Upvotes: 7

Related Questions