Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As said <a href="http://java.sun.com/docs/books/tutorial/java/nutsandbolts/switch.html" rel="noreferrer">here</a>, switch works with the byte, short, char, and int primitive data types. It also works with enumerated types and a few special classes that "wrap" certain primitive types: Character, Byte, Short, and Integer. </p> <p>The Java switch statement is compiled into the <a href="http://www.artima.com/underthehood/flowP.html" rel="noreferrer">JVM bytecode tableswitch or lookupswitch</a>. Both of these bytecodes require that the case values be unique, 32-bit, integer, compile-time constants.</p> <p>The tableswitch and lookupswitch instructions both include one default branch offset and a variable-length set of case value/branch offset pairs.<br> Both instructions pop the key (the value of the expression in the parentheses immediately following the switch keyword) from the stack.<br> The key is compared with all the case values:</p> <ul> <li>If a match is found, the branch offset associated with the case value is taken. </li> <li>If no match is found, the default branch offset is taken. </li> </ul> <p>Even though the above represents implementation details, I believe <strong>the types used for switch are the one compatible with an efficient bytecode for control flow</strong>, and it may have been an important part for the reason of this choice.</p> <p>As said in this <a href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4276296" rel="noreferrer">Java Bug</a>:</p> <blockquote> <p>I suppose you could allow Java switch statement cases to include other types of values, and compile a switch statement that uses one of those kinds of values into other bytecode, such as a sequence of ifs and gotos. But then we'd have to examine all switch statements carefully in order to determine if it will be compiled into a fast switch bytecode or a slow sequence of if-elses.</p> </blockquote> <p>As said in this other <a href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4401321" rel="noreferrer">Java bug</a></p> <blockquote> <p>'switch' statements can use function-table dispatch and/or binary-tree search to match the case statements faster than just testing them one by one in order.</p> </blockquote>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload