Java Switch

The Java switch is covered to better meet learners’ learning needs. Switch statements are more efficient than writing multiple if-else statements.

java switch statements

Java Switch Statements

Using Java switch, you can execute one of many code blocks by using the switch statement – Basically, it is a ladder statement with if-else-if conditions.

The switch statement accepts bytes | shorts | ints | longs | enums | strings and some wrapper types.

Switch Syntax:

switch(expression) {
case x:
// code block to be executed if case x value matches
break;
case y:
// code block to be executed if case y value matches
break;
default:
// code block if not case value matches
}

Strings are now allowed in switch statements in Java 7.

Here’s how it works:

  • Switch expressions are analyzed once.
  • Expression values are compared with case values.
  • An associated block of code is executes if there is a match.
  • Default and break keywords are optional, and will be addressed later.

Below is an example of how the month name can be calculated using the month number:

Example: 

public class Main { public static void main(String[] args) { int month_number = 7; switch (month_number) { case 1: System.out.println("January"); break; case 2: System.out.println("February"); break; case 3: System.out.println("March"); break; case 4: System.out.println("April"); break; case 5: System.out.println("May"); break; case 6: System.out.println("June"); break; case 7: System.out.println("July"); break; case 8: System.out.println("August"); break; case 9: System.out.println("September"); break; case 10: System.out.println("October"); break; case 11: System.out.println("November"); break; case 12: System.out.println("December"); break; } // Outputs Monthname="July" as month_number=7 } }

Example: 

public class Main { public static void main(String[] args) {char alphabet = 'b'; switch (alphabet) { case 'a': System.out.println("This is an alphabet a"); break; case 'b': System.out.println("This is an alphabet b"); break; case 'c': System.out.println("This is an alphabet c"); break; case 'd': System.out.println("This is an alphabet d"); break; case 'e': System.out.println("This is an alphabet e"); break; case 'f': System.out.println("This is an alphabet f"); break;} // Output: This is an alphabet b } }

Switch statements test a variable against multiple values to determine its equality.



Java break Keyword

Java breaks from a switch block when it reaches the break keyword.

As a result, more code and case testing will be stopped inside the block.

Once a match is found, and the job is complete, it’s time for a break. More testing is not necessary.

Because a break ignores the rest of the switch block’s code, it can save a lot of execution time.


Java default Keyword

If no case matches are found, the default keyword runs the following code:

Example: 

public class Main { public static void main(String[] args) { int month_number = 4; switch (month_number) { case 1: System.out.println("January"); break; case 2: System.out.println("February"); break; case 3: System.out.println("March"); break; default: System.out.println("No Case matched !! Default code is executed "); } // Output = No Case matched !! Default code is executed } }

Example: 

public class Main { public static void main(String[] args) {String day_name = "Satunday"; // We have assigned the string day_name the wrong value 'Satunday' to check the working of the default boxswitch (day_name) { case "Monday": System.out.println("Today is Monday"); break; case "Tuesday": System.out.println("Today is Tuesday"); break; case "Wednesday": System.out.println("Today is Wednesday"); break; case "Thursday": System.out.println("Today is Thursday"); break; case "Friday": System.out.println("Today is Friday"); break; case "Saturday": System.out.println("Today is Saturday"); break; case "Sunday": System.out.println("Today is Sunday"); break; default: System.out.println("No name matched !! Wrong day name "); } } }

A switch block does not require a break when the default statement is specified as the last statement.


What you need to know?

  1. Case values can have optional default labels.
  2. There must be a unique value for each case. An error will be thrown at compile time if the value is duplicated.
  3. Java switch expression can have one or N case values.
  4. Only switch expressions can be used as case values. Case values must be literals or constants. Variables are not allowed.
  5. Break statements are optional in case statements. As control reaches the break statement, it jumps to control after the switch expression. It executes the next case if there is no break statement.
  6. In Java, byte, short, int, long (with wrapper), enums, and strings must be used as switch expressions.
  7. Java switch statements can be placed between other switch statements. A nested switch statement is what it is called.

 

We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

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