OCAJP – How to use a switch sentence?

This post will focus on the OCAJP exam objective, “Use a Switch Statement”. The exam will test your knowledge about the allowed data types variables for switch. If you are asked a question that contains a switch statement with compile-time errors, you will need to correct them by selecting the appropriate options.
We will explain all the important points about switch statement. Please leave any comments regarding this article.
TRY NOW – 25 Free OCAJP Mock Examination Questions
How to use a switch statement?
Here are some important facts about Java switch statement.
A switch statement is a complex structure for making decisions. It evaluates a single value and redirects flow to the first matching branch, known as a case statement.
If there is no case statement that matches the value, an option default statement will be called.
If there is no default option, the entire switch statement will not be executed.
Switch statement is a simpler way to handle complex decision logic in many cases.
Let’s look at an example to see the difference between if/else and switch statements.

Example:
int x=3;if(x==1) System.out.println(“x equals 1”); else if(x==2) System.out.println(“x equals 2”);else if(x==3) System.out.println(“x equals 3”);else System.out.println(“Don’t know x value”);The same functionality using switch statement.
Example:
int x=3;switch(x) case 1 : System.out.println(“x equals 1”); break; case 2 : System.out.println(“x equals 2”); break; case 3 : System.out.println(“x equals 3”); break; default : System.out.println(“Don’t know x value”); The main difference between if-else and switch is ” if-else statement checks each and every condition but switch directly executes the particular condition.
The most common form of a switch statement is:
switch(expression){ case constant1 : code block case constant2 : code block case constant3 : code block . . . default : code block}It may contain zero or more case branches.
Curly braces must be worn, even though there aren’t any statements.
There is no need to write case branches in one order. You can write in any order.
Because some people may test with invalid syntax, it is important to use case branch syntax
A switch statement should only contain one default branch.
You can write default branches at any location in a switch statement.
Allowed data types to be used for the value of the switch parameter
The switch statement cannot be used to compare all types, such as primitives and objects.
There are limits on the type of arguments that a switch sentence can accept.
A switch statement accepts arguments type char (byte), short, int and string (starting with Java version 7).
It also accepts arguments or expressions of types enum. Character. Byte. Integer. Short. However, these aren’t on the OCAJP exam objectives so we won’t cover them in these articles.
The switch statement does not accept arguments of type long or float, double,boolean, or any other object than String.
These examples don’t compile due to the fact that they do not allow certain data types.
Examples :
long x = 1;switch x //compile error at this line since long is not permitted.
float x = 2.0ff;switch x //compile error at this line since float is not permitted.
Double x = 5.0;switch x //compile error at this line since double is not permitted.
boolean = true;switch x = true. //compile error at this line due to boolean not allowed.
You can pass a variable to a switch sentence, but you can also pass an expression. As long as it returns one the allowed types,
The following code is valid
Example:
byte a = 3, and x = 6;switch a+x //..codeCompile time Constant Values
Each case label value must contain compile-time constants of the same data type and switch value.
This means that you can only use literals, enum constants or final constant variables of a similar data type as the case label value.
We mean that the variable must have the final constant and be initialized with a literal value within the same expression it is declared.
A compile-time constant value must be used to determine the value of a case label. This means that the value must be known at the time code compilation.
The variable value of the switch parameter need not be compile time constant. However, case label must be a compile time constant value.
Example:
{int a = 5, b = 12, c = 4;switch (a) int a = 5, 12 c = 4, and switch (a); // causes compile time error as it is evaluated at run-time. so it is not compile time constant System.out.println(b + c); break; case 2 * 7:System.out.println(“14”); }You can use variables in an expression if they’re marked final because the value