Core Java

OCA Java 8 Preparation : Selection Constructs

I know you are already a Java programmer and you might think–  ‘I already know about Java selection constructs. I have been using it since long.’  But still, I would suggest you to go through these points before you plan to give your OCA Java certification exam.

After preparing on these points, you will be in a position to quickly identify valid and invalid selection constructs in the code blocks presented to you during your OCA exam. It will in turn reduce your turnaround time in answering such questions.

if, if-else, if-else-if Constructs :

if-else statements need no introduction to a Java programmer as you use it in your day to day code.

//if statement
if(condition)
   statement

//if-else statement
if(condition)
   statement1
else
   statement2

Still, always remember a few points :

  • The ‘condition’ that you use in an if-construct must always evaluate to a boolean or Boolean type.
  • The if-else statements can be nested, so watch out for such code blocks with a little more care in your OCA exam.
  • Using an expression of the form (a=b) as a condition will not compile, until and unless both a & b are boolean.

‘switch’ Statement :

The switch statement can help you compare a value of a variable with multiple values.The syntax of a switch statement looks similar to the one mentioned below. The switch statement can have at most 1 default label.

switch(expression){
  case label1 : statement1
  case label2 : statement2
  case label3 : statement3
  default : <em>statement4
}</em>

The OCA examiners love to test you on your understanding of switch statements. So, we choose to discuss it in a very detailed fashion. Stick to the end and you’ll add some good gems to your understanding of switch statements.

Rules related to switch expression :

The expression used in switch statement must evaluate to one of the following types :

  • char, byte, short, or int
  • Character, Byte, Short, or Integer
  • String or an enum type. For a String expression which evaluates to null, a NullPointerExpression will be thrown at runtime.

If the expression evaluates to a type – other than the ones mentioned above, the code will fail to compile.

long val=10;
switch(val){ //compile-time error
case 10 : System.out.println("I am here 1");
case 20 : System.out.println("I am here 2");
}

Rules related to case labels :

  1. The value of case labels must be compile-time constants i.e their values must be well-known at the time of code compilation.
    int q=10,w=20;
    final int r = 10;
    switch(10){
    case q : System.out.println("I am 10"); break;  //fails to compile
    case q+w : System.out.println("I am 30"); break; //fails to compile
    case 2+5 : System.out.println("I am 7 as a compile time constant"); break; //compiles fine
    case r : System.out.println("I am a final variable with value 10");//works fine
    }
  2. As demonstrated in the last line of above piece of code, you can use a final variable as a value of case label  as its values will not change once it is initialized.
  3. I know that it is a strange fact that if you don’t assign a value to a final variable at the time of its declaration, it isn’t considered to be a compile-time constant. But, that’s how it is. Try the below piece of code yourself on your favourite IDE.
    final int q = 10;
    final int w;
    w = 20;
    switch(10){
    case q : System.out.println("I am a compile time constant");
    case w : System.out.println("Nobody considers me as a compile time constant!"); //fails to compile
    }
  4. The values used in case labels must be assignable to the argument you are passing as an expression to the switch statement.
    short val = 10;
    switch(val){
    case true : System.out.println("I am a boolean value"); //fails to compile
    case 10.0 : System.out.println("I am a float value"); //fails to compile
    case 10 : System.out.println("I am an int value"); //compiles fine
    }
  5. null can’t be used as a case label. Any attempt to do so will make the code compilation fail.
    String val = "Hey";
    switch(val){
    case "Ray" : System.out.println("I am Ray"); break;
    case null : System.out.println("null is not allowed as a case label"); //fails to compile
    }

    .

Other Rules Related to a switch statement :

  1. A switch block with no body will compile fine.
    //code compiles fine
    int age = 25;
    switch(age){
    }
  2. It is fine if you choose to define a single code block for multiple case labels.
    // Below code simply works fine
    int age = 25;
    switch(age){
    case 25 : case 30 : case 40: System.out.println("You're eligible"); break;
    case 45 : case 50 : System.out.println("You aren't eligible!");
    }
  3. Any attempt to define two case labels with the same value will lead to code compilation failure.
    int age = 25;
    switch(age){
    case 25 :  System.out.println("You're eligible"); break;
    case 5+20 : System.out.println("You're eligible"); break; //fails to compile, duplicate case label
    }

     

switch Statement Fall-through :

I am sure you are aware of this behaviour of switch statement. Still, let us quickly revise it.

In the absence of a break statement, the control will fall-through and executes code in all the case labels that are below the matched case label.(It’ no surprise, it will execute the one in matched case label first!).

Try and guess the output of the code block below. I am sure, you’ll guess it right.

int val = 10;
switch(val){
case 5 : System.out.println("I am 5");
case 10 : System.out.println("I am 10");
case 20 : System.out.println("I am 20");
case 30 : System.out.println("I am 30"); break;  //first break encountered
case 40 : System.out.println("I am 40");
}

Yes, you’re right! The output will be :

I am 10
I am 20
I am 30
Be the First to comment.

Leave a Comment

Your email address will not be published. Required fields are marked *