Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-1240397

'switch' statement allows statements without 'case' in switch block

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P3 P3
    • None
    • 1.0, 1.1
    • tools
    • None
    • x86, sparc
    • solaris_2.4, windows_95

      Syntax of switch Statement (see Java Language Specification, section 13.9)
      does not allow using statements without 'case' in switch block:

              SwitchStatement:
                  switch ( Expression ) SwitchBlock

              SwitchBlock:
                  { SwitchBlockStatements_opt }

              SwitchBlockStatements:
                   SwitchBlockStatement
                   SwitchBlockStatements SwitchBlockStatement
            
              SwitchBlockStatement:
                   SwitchLabels BlockStatements

              SwitchLabels
                   SwitchLabel
                   SwitchLabels SwitchLabel

              SwitchLabel
                   case ConstantExpression :
                   default :


      But Java compiler don't consider this as an error and
      compiles such switch statement successfully.
      See example below:

      > cat stmt107.java
      public class stmt107
      {
        public static void main(String args[])
        {
           int b = 2;
           for ( b = 1; b < 4; b++ )
           {
              switch ( b )
              {
                    System.out.println("nothing");
              }
              System.out.println("PASS " + b);
           }
        }
      }


          
      > /usr/local/java/1.0/bin/javac stmt107.java
      > /usr/local/java/1.0/bin/java stmt107
      PASS 1
      PASS 2
      PASS 3
      >

            tturnidgsunw Todd Turnidge (Inactive)
            leosunw Leo Leo (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: