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-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 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.
The expression used in switch statement must evaluate to one of the following types :
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"); }
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 }
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 }
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 }
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 }
.
//code compiles fine int age = 25; switch(age){ }
// 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!"); }
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 }
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