Friday, July 29, 2011

COIN project of JDK7

One language level change is in Switch statement. Now switch statement supports string. For e.g.

String str1 = "hello";
switch(str1) {
case "hello": System.out.println("Hello!!!!");break;
default: break;

Earlier switch only supports integer, character, byte and short.


  1. But what was the reason that earlier they are not supporting this feature.
    Also we know that there are things like immutability and string pool. How this is going to affect the same things.

  2. In earlier switch statement implementation, switch instructions are prepared using tableswitch or lookupswitch instructions. Both of these uses zero to three bytes offset padding and four bytes offset for case branches.
    In case of int, byte, short or character, case branch offset is in range whereas for long, float, double or string, it is out of range and hence give compilation error.
    The types used for switch are the one compatible with an efficient bytecode for control flow, and it may have been an important part for the reason of this choice.