Bits of Java – Episode 7: The switch Statement
This week I will try to describe the switch
statement, focusing in particular to which kind of variables are allowed to be used with such statement.
This is one of the Java features that changed a lot over the different releases, thus I hope also some of the most experienced developers can benefit from this post.
But let’s start from the beginning. What is a switch
statement? You are probably all familiar with the fact that, when you have a lot of possible conditions that could be met, instead of writing a lot of if
-else if
conditions, you can use the switch
statement. The variable you want to evaluate goes in the switch
block, and all the possible values that it can take and for which you wish to take some actions go in the case
blocks.
String name = "Ilenia";
int number = 3;
switch(name) {
case "Andrea":
number++;
break;
case "Ilenia":
number--;
break;
default:
System.out.println("Name is unknown");
break;
}
System.out.println(number);
A lot of things are happening here. Let’s see them in detail. We want to evaluate the variable name
and we wish to do different things depending on its actual value. If the value is "Andrea"
we want to increment by one unit the value of number
; if, instead, the value of name
is "Ilenia"
we want to decrement by one unit the value of number
. If the value of name is neither "Andrea"
nor "Ilenia"
we just wish to print a message saying that the name is unknown.
So, what the switch
statement does is the following: it evaluates the variable in the switch
block (name
in our example) and then it reaches the first case
statement which corresponds to the actual value of the variable and it executes the corresponding action. If none is found, it reaches the default
block and it executes the corresponding action. If no default
block is provided it simply exists the switch
statement without doing anything.
The break
is also important. It is not mandatory, but if you do not put it the actual flow can change, and so the final result. Without break
, in fact, also all the case
blocks which are after the one satisfying the actual condition would be executed.
Now that we have reviewed how the switch
statement works, let’s focus on the type of variables that can actually be used in it. For this purpose, we have to distinguish between variables that can be used in the evaluation part of the statement (in the brackets immediately after the switch
keyword) and variables that can be used in the case
blocks.
- evaluation variable: in different releases of Java new types of variables have been allowed for the
switch
evaluation block. In the current stable version Java SE-11, the allowed types are:byte
andByte
short
andShort
char
andCharacter
int
andInteger
String
enum
var
(provided its type resolves to one of the previous types)
case
variables: the first rule here is that variables in thecase
block must be of the same type as the one which is being evaluating. If numeric promotion to the type of the variable being evaluated is possible without explicit casting then also that type is allowed (if you are not familiar with the concept of numeric promotion or you wish to revisit it, take a look at our post here). In addition to the type of the variable, there are also other restrictions. In particular, a variable in acase
statement can be a literal, anenum
constant or it has to befinal
and its value has to be evaluated at compile time, meaningfinal
variables passed as method parameters and used in aswitch
statement inside the method are not allowed.
Let’s see some examples to familiarize with all these rules.
public void doSomething(final String word) {
String name = "Ilenia";
int number = 3;
final String another_name = "Vania";
String not_a_final_string = "Lucio";
final short a_different_type = 4;
switch(name) {
case "Andrea": //this is a literal, so is OK
number++;
break;
case "Ilenia": //this is another literal, so is OK
number--;
break;
case word: //this is final but is passed as method parameter --> DOES NOT COMPILE
number = 6;
break;
case another_name: //this is a final constant variable, so is OK
number = 23;
break;
case not_a_final_string: //this is NOT final --> DOES NOT COMPILE
number = 44;
break;
case a_different_type: //this is a different type --> DOES NOT COMPILE
number = 77;
break;
default:
System.out.println("Name is unknown");
break;
}
}
I hope you have learned something new, and that the switch
statement will be now clearer for you as it is for me!
Next topic of the series will be the use of labels in loops!
by Ilenia Salvadori