Switch statement in Java

Switch statement in Java is a statement used to check the value of a variable, which can have different values, depending on the value of this variable we can define a different set of statements.

For example, I have n variable that can be valued at 1,2,3. I want that in case the value of this n variable is 1, I will print the word “Khanh”, the value of 2 will print the word “Huong Dan Java”, the value of 3 will print “Lap Trinh Java “, using a switch statement, I will write the following code:

Here, we use the case label to handle for each different value of n variable. To execute only the code for each value of n variable, we must use the break keyword so that after executing the code, our program will exit the switch statement. As you can see, we also have a default label, this label is used to handle for cases where the value of this n variable is not in the case where the switch statement has been processed.

The result when executing the above code will look like this:

Switch statement in Java

As you can see, because the value of n variable, in this case, is 1, the case for value 1 will be executed and print “Khanh”. After printing the text, to the break statement, our program will exit the switch statement.

Before Java 7, you can pass the variable of type char, byte, short, int or expression data types like Enum, Character, Byte, Integer, and Short in a switch statement. From Java 7 onwards, the switch statement also supports String data type.

Result:

Switch statement in Java

You can pass into switch statement an expression as long as the return value of this expression is the allowable type of the switch statement.

Result:

Switch statement in Java

When working with a switch statement, you can encounter some following problems:

  • The first problem is that we forget to declare the break statement: this means that our code will not exit the switch statement after matching the first case but will continue the next case, for example:

Result:

Switch statement in Java

  • The second problem is if we want in case the value of the variable with different values a and b, we will handle the same code. We will write the following code:

Or:

These ways of writing are inconvenient because duplicating the code or we sometimes can’t control it, where should we put a break statement?

  • The third problem is that if you want to use a switch statement to determine the value of a variable, you must write the following:

To solve these problems, from Java 12, you can write a switch statement with the following improvements:

  • For the first problem, Java 12 now supports us how to write a switch statement like lambda expression, for example. as:

Result:

Switch statement in Java

  • For the second problem, you can rewrite the switch statement as follows:

Result:

Switch statement in Java

  • As for the third problem, now Java 12 has supported us to return value with a switch statement as follows:

Result:

Switch statement in Java

Chia sẽ bài viết này ...Share on Facebook
Facebook
0Tweet about this on Twitter
Twitter
Share on LinkedIn
Linkedin

Add Comment