Java switch statement

Java Switch statement

Java switch statement executes a single statement out of all the statements in the block. It is just similar to the if-else block in java. Switch statements can work with a number of data types such as int, long, short, byte etc. It also works with wrapper classes such as Long, Int etc.  The switch statement is also called when there is a need to test the quality of the variable. It can be done against multiple values. It is also called a multi-way branch statement.

A switch statement works in a manner in which it evaluates the value of the variable and then compares it with the block in the sequence priority. The section is executed if both the values match; otherwise the section is not executed. Switch case jumps to the next block to compare the values again.

Switch Statement in Java

  • It is not defined whether a switch statement will have single statement of multiple statements in the program.
  • The switch case doesn’t allow the use of duplicate values. The value should always be unique otherwise it shows compile time error.
  • Switch case does not allow the use of variables. It can only use constant or a literal.
  • It is not mandatory to use break while using switch. If break is used, the execution will be stopped and moved to next section.
  • The use of break statement is always done inside the switch case. Break statement does not execute when used outside the switch case.
  • If the break statement is not found, the section is jumped to another case.

Java switch statement String

In java, it takes a lot of time to execute a string. However, switching is comparatively easily possible by using primitive data types. String switching should only be done when there is data already present in the string form. The string should never be null while performing a switch case on it.

Java Switch multiple cases

As we know the switch case reduces the complexity of the if-else loop. We can have multiple cases in the switch. The code can also be written in combined forms. It is the user’s wish as it might happen that he wants to execute different inputs on the same code. This is what we call as combining multiple cases in the switch.

Switch statements can have multiple cases in java using different methods. They can either be using a break statement. They are also executed using a nested switch case in which there is a switch statement inside a switch statement. As we have discussed that a switch statement has its own block of execution, there can be no issue between the cases of inner and the outer switch.

Switch Case Statement Example in Java

public class DeveloperHelps { 
public static void main(String[] args) 
{ 
int weekday = 2; 
String dayString;   
// switch statement with int data type 
switch (weekday) { 
case 1: 
dayString = " Day is Monday"; 
break; 

case 2: 
dayString = " Day is Tuesday"; 
break; 

case 3: 
dayString = " Day is Wednesday"; 
break; 

case 4: 
dayString = " Day is Thursday"; 
break; 

case 5: 
dayString = " Day is Friday"; 
break; 
     
case 6: 
dayString = " Day is Saturday"; 
break; 
        
case 7: 
dayString = "Day is Sunday"; 
break; 
        
default: 
dayString = "Invalid day"; 
break; 
} 
System.out.println(dayString); 
} 
}

The output of the program will be:

Day is Tuesday

Java switch statement without break

A switch statement has a feature in java. It is also called fall-through in which it executes all the sections when one switch statement is passed. This can only happen in a case where the break statement is not present.

The switch statement is also executed in the wrapper class. Below is a program to understand the concept better:

Switch Case Example using Wrapper Class

public class DeveloperHelps {       
public static void main(String args[])  
{         
Integer age = 21;        
switch (age)  
{  
case (16):            
System.out.println("You are under 21");  
break;  

case (18):                
System.out.println("You are eligible for marriage now");  
break;  
                
case (65):                
System.out.println("You are over age");  
break;  
                
default:  
System.out.println("Invalid age");  
break;  
}             
}  
}

The output of the above code using switch statement in wrapper class is:

Invalid age

Leave a comment

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