Defined behavior on switch-case fall-through

David Holmes david.holmes at oracle.com
Thu Feb 26 00:40:24 UTC 2015


On 25/02/2015 7:33 PM, Noctarius wrote:
> Hey guys,
>
> Sorry for crashing into the mailinglist but by looking at the Java
> specification we're not really sure about it. I guess it might be
> well defined but we want to make sure.

You are asking a question about the language rather than the libraries, 
but we don't have a specific mailing list for language discussions.

> Looking at the following example we see a non natural ordered case
> for fall-through. Looking at the spec there is an example which
> defines the fall-through behavior, unfortunately the example uses a
> natural ordered list of numbers.
>
> switch(age){
> case 0:
> case 2:
> case 4:
> case 6: println("even");break;
> case 1:
> case 3:
> case 5: println("uneven");break;
> default: ....
> }
>
> In our example we do not have this behavior. We are sure the Java
> compiler will compile it as it is above but Hotspot applies, as far
> as I know, reordering on cases depending on the frequency of usage.
> I expect it to only reorder fall-through blocks or let's call it
> blocks without any order-dependency but is this how it works? Is
> this case covered by the spec? Maybe it needs another example to
> make it clear.

Ordering is irrelevant. The code generated will execute the logic as 
specified:

if (age==0 || age==2 || age==4 || age==6)
   println("even");
else if (age==1 || age==3 || age==5)
   println("uneven");
else
   // default ...

David
-----

> Thanks
>
> Chris
>



More information about the core-libs-dev mailing list