Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 223
Raji A C Avatar asked Jun 14 '12 05:06

Raji A C


People also ask

What is a switch statement in Java?

The switch statement or switch case in java is a multi-way branch statement. Based on the value of the expression given, different parts of code can be executed quickly. The given expression can be of a primitive data type such as int, char, short, byte, and char.

What is switch statement example?

So, printf(“2+3 makes 5”) is executed and then followed by break; which brings the control out of the switch statement. Other examples for valid switch expressions: switch(2+3), switch(9*16%2), switch(a), switch(a-b) etc.

What is switch case in Java with example?

The switch statement allows us to execute a block of code among many alternatives. The syntax of the switch statement in Java is: switch (expression) { case value1: // code break; case value2: // code break; ... ... default: // default statements }

What is a switch () statement?

The switch statement evaluates an expression, matching the expression's value against a series of case clauses, and executes statements after the first case clause with a matching value, until a break statement is encountered.


1 Answers

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.

like image 128
x4u Avatar answered Oct 04 '22 05:10

x4u