In this blog we will learn switch case java in details. We will discuss Java Switch Multiple Case. We will check java switch syntax,use of default case,how do exit switch case and create nested switch statement in java in details..
Table Of Content:
1. switch case in java/java switch
2. java switch enum
3. java switch string
4.Is switch case faster than if else Java?
5. Can we use float in switch case in Java?
6. Does a switch statement need a default Java?
7. Can we create a nested switch statement in Java?
8.How do you exit a switch in Java?
9.Java Wrapper in Switch Statement
Switch case in java/java switch:
Switch case is like if-else-if statement. Switch case java execute one statement from multiple conditions.We can give expression of these type byte, short, int, long, enum types, String and some wrapper class like Integer,Byte,Long,Short. From Java 7 it accept String also.
Key Points:
- Case value must be unique otherwise java compiler throw error.
- The case value must be literal or constant. It doesn't allow variables.
- Switch case type must be byte, short, int, long, enum types, String and some wrapper class like Integer,Byte,Long,Short types.
- Switch case can have default case that is used if no case found.It is optional.
- After each case we can put break keyword but it is optional.
Syntax:
switch(expression){
case value1:
//code to be executed;
break; //optional
case value2:
//code to be executed;
break; //optional
case value3:
break;
......
default:
code to be executed if all cases are not matched;
}
Sample Code Without Break Keyword:
Java Switch case execute all the statement after first match if break keyword not available.
package youtubeClass;
public class Test {
public static void main(String[] args) {
String color="red";
switch(color)
{
case "red":System.out.println("This is Red color.");
case "green":System.out.println("This is Green color.");
case "blue":System.out.println("This is Blue color.");
default:System.out.println("This is default color.");
}
}
}
OutPut:
This is Red color.
This is Green color.
This is Blue color.
This is default color.
Sample Code With Break Keyword:
It execute that particular case if break keyword is available.
package youtubeClass;
public class Test {
public static void main(String[] args) {
String color="red";
switch(color)
{
case "red":System.out.println("This is Red color.");
break;
case "green":System.out.println("This is Green color.");
break;
case "blue":System.out.println("This is Blue color.");
break;
default:System.out.println("This is default color.");
}
}
}
Output:
This is Red color.
Is switch case faster than if else Java?
Switch case is faster for multiple choice and complex conditions. Switch Case also increase readability. If Switch acse are very less than it will not impact performance. In that situation we can use if-else also.
Can we use float in switch case in Java?:
We can't use float in switch case. We will get complie time error if we try to use float in switch case.
Does a switch statement need a default Java?/Default Case Executions:
Default case is optional in java switch case.Default case will be executed if switch expression value is not matching to any case.
package youtubeClass;
public class Test {
public static void main(String[] args) {
String color="yellow";
switch(color)
{
case "red":System.out.println("This is Red color.");
break;
case "green":System.out.println("This is Green color.");
break;
case "blue":System.out.println("This is Blue color.");
break;
default:System.out.println("This is default color.");
}
}
}
Output:
This is default color.
Java Enum in Switch Case/Java switch enum:
In java switch case we can use Enum also.
package youtubeClass;
public class Test {
public enum Colors { red,blue }
public static void main(String[] args) {
Colors[] colors = Colors.values();
for(Colors color:colors) {
switch(color)
{
case red:System.out.println("This is Red color.");
break;
case blue:System.out.println("This is Blue color.");
break;
default:System.out.println("This is default color.");
}
}
}
}
Output:
This is Red color.
This is Blue color.
Java switch string:
From Java 5 we can use String in Switch Case.
package youtubeClass;
public class SwitchCase {
public static void main(String[] args) {
String name="anil";
switch(name)
{
case "sunil":
System.out.println("My Full name is Sunil Sharma");
break;
case "anil":
System.out.println("My Full name is Anil Sharma");
break;
}
}
}
Output:
My Full name is Anil Sharma
Can we create a nested switch statement in Java?
Yes we ca create nested switch statement in java.
package youtubeClass;
public class SwitchCase {
public static void main(String[] args) {
int x=1,y=3;
switch(x)
{
case 1:
switch(y)
{
case 3:
System.out.println("Value is 3");
break;
}
break;
case 2:
System.out.println("Value is 2");
}
}
}
Java Wrapper in Switch Statement:
We can use wrapper classes in Switch statement.
package youtubeClass;
public class Test {
public static void main(String[] args) {
Integer age = 18;
switch(age)
{
case 17:System.out.println("Sorry you are not eligable for DL.");
break;
case 18:System.out.println("you are eligable for DL.");
break;
default:System.out.println("Please provide valid age.");
}
}
}
Output:
you are eligable for DL.