O'Reilly logo

Java 2™ Programmer Exam Cram™ 2 (Exam CX-310-035) by Marcus Green, Bill Brogden

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The switch-case Structure

Java provides the extremely handy switch-case construct to facilitate selecting between multiple alternatives. This is such a common programming situation that the Java Virtual Machine (JVM) has bytecodes designed to speed this operation. The parameter in a switch statement is evaluated to int and this value determines what happens next.

Within the block of code controlled by a switch statement, the programmer can place case statements and (optionally) a default statement. In the following example, a String variable gets set according to the value of the integer variable x:

switch( x ) {
  case 0 : str = "none" ; break ;
  case 1 :
    str = "single" ; break ;
  case 2 :
    str = "pair" ;
    break ;
  default :  str = "many" ;
}

Each ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required