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.

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.

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.

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.
  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.
  4. The values used in case labels must be assignable to the argument you are passing as an expression to the switch statement.
  5. null can’t be used as a case label. Any attempt to do so will make the code compilation fail.

    .

Other Rules Related to a switch statement :

  1. A switch block with no body will compile fine.
  2. It is fine if you choose to define a single code block for multiple case labels.
  3. Any attempt to define two case labels with the same value will lead to code compilation failure.

     

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.

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

Be the First to comment.

Leave a Comment

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