środa, 7 września 2011

FLOW CONTROL

switch

1.
          legal types, that can be used in switch: byte, short, char, int, enum, Byte, Short, Character, Integer. In case of wrappers - unboxing works.

          e.g.
          Integer i = 4;
          switch(i) {}

2.
        Notice the following example:


        final int y = 3;

        switch (2) {
            case y - 1:
                System.out.println("OK");
        }

       It is compiling, unless the y is not final.

3.
   case expressions must be final!
 
   static int x = 4;
 
   switch(4) {
        case x: {}
    }

    is incorrect. x should be final int x = 4;/final static int x = 4;

4.


        byte b = 3;
        switch(b) {
            case 128 :{}    //incorrect - possible loss of precission
        }


        byte b = 3;
        final int i = 128;
        switch(b) {
            case i :{}            //incorrect - possible loss of precission - if i <=127 && i >=-128 compilation is ok
        }


        char c = 3;
        final int i = Integer.MAX_VALUE;
        switch(c) {
            case i :{}          //incorrect
        }

        the case values must be within the range of switch type!




Brak komentarzy:

Prześlij komentarz