Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Because they didn't implement the necessary instructions in the bytecode and you <em>really</em> don't want to write that many cases, no matter how "production ready" your code is...</p> <p><em>[EDIT: Extracted from comments on this answer, with some additions on background]</em></p> <p>To be exact, 2³² is a <strong>lot</strong> of cases and any program with a method long enough to hold more than that is going to be utterly horrendous! In any language. (The longest function I know of in any code in any language is a little over 6k SLOC – yes, it's a big <code>switch</code> – and it's really unmanageable.) If you're really stuck with having a <code>long</code> where you should have only an <code>int</code> or less, then you've got two real alternatives.</p> <ol> <li><p>Use some variant on the theme of hash functions to compress the <code>long</code> into an <code>int</code>. The simplest one, only for use when you've got the type wrong, is to just cast! More useful would be to do this:</p> <pre><code>(int) ((x&amp;0xFFFFFFFF) ^ ((x &gt;&gt;&gt; 32) &amp; 0xFFFFFFFF)) </code></pre> <p>before switching on the result. You'll have to work out how to transform the cases that you're testing against too. But really, that's still horrible since it doesn't address the real problem of lots of cases.</p></li> <li><p>A much better solution if you're working with very large numbers of cases is to change your design to using a <code>Map&lt;Long,Runnable&gt;</code> or something similar so that you're looking up how to dispatch a particular value. This allows you to separate the cases into multiple files, which is much easier to manage when the case-count gets large, though it does get more complex to organize the registration of the host of implementation classes involved (annotations might help by allowing you to build the registration code automatically).</p> <p>FWIW, I did this many years ago (we switched to the newly-released J2SE 1.2 part way through the project) when building a custom bytecode engine for simulating massively parallel hardware (no, reusing the JVM would not have been suitable due to the radically different value and execution models involved) and it enormously simplified the code relative to the big <code>switch</code> that the C version of the code was using.</p></li> </ol> <p>To reiterate the take-home message, wanting to <code>switch</code> on a <code>long</code> is an indication that either you've got the types wrong in your program or that you're building a system with that much variation involved that you should be using classes. Time for a rethink in either case.</p>
    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