Java Tutorial

What is Java


Control Statements


Java Object Class


Java Inheritance


Java Polymorphism


Java Abstraction


Java Encapsulation


Java Array

Java OOPs Misc


Switch Statement in Java

The Java switch statement executes one statement from multiple conditions. there are several options and we have to choose only one option from the available ones, we can use switch statement.

If several option are available then its not recommend to used nested if else because of its reduces reusability to handle this requirement.

Syntax:


switch(expression){
case 1:
//write to code be executed;
break; //optional
case 2:
//write to code be executed;
break; //optional
case 3:
//write to code be executed;
break; //optional
......
default:
default is executed if all cases are not matched;
}


Important Point

1. The Allowed Argument type for Switch statement are byte ,short, char, int, until 1.4 version, but from 1.5V onword coresponding wrraper class and Enum allowed from 1.8V onword String type allowed.
2. In Switch Statement Boolean, Long, Float, Double are Not Allowed.
3. Carly bress "{ }" are Mendatery except Switch Statement, Everywhere "{}" are Optional.
4. Inside a Switch every Statement should be under some case of default that is independent Statements are not Allowed inside a Switch.
5. Every case Lavel should be compile time contant( that is contant expression).
6. Every case lebel should be in the Range of Switch Argument types.
7. In Switch statement Duplicate case label are not allowed.

Example:


public class JavaBitSwitch {
public static void main(String[] args) {
int a=5 ,b=10;
switch(a & b){
case 1:
System.out.println("Print a");
break;
case 2:
System.out.println("Print b");
break;
default:
System.out.println("Not Bot Print is a and b");
}
}
}


Output: Not Bot Print is a and b

Examples:


public class JavaBitSwitch
{
public static void main(String[] args)
{
int day = 7;
String dayString;
switch (day)
{
case 1: dayString = "Sunday";
break;
case 2: dayString = "Monday";
break;
case 3: dayString = "Tuesday";
break;
case 4: dayString = "Wednesday";
break;
case 5: dayString = "Thursday";
break;
case 6: dayString = "Friday";
break;
case 7: dayString = "Saturday";
break;
default: dayString = "Invalid day we can read java64bit";
break;
}
System.out.println(dayString);
}
}


Output: Saturday

Example:without Break


public class JavaBitSwitch{
public static void main(String[] args)
{
int day = 7;
String dayString;
switch (day)
{
case 1: dayString = "Sunday";
// break;
case 2: dayString = "Monday";
// break;
case 3: dayString = "Tuesday";
// break;
case 4: dayString = "Wednesday";
// break;
case 5: dayString = "Thursday";
// break;
case 6: dayString = "Friday";
// break;
case 7: dayString = "Saturday";
// break;
default: dayString = "Invalid day we can read java64bit";
// break;
}
System.out.println(dayString);
}
}


Output: Invalid day we can java64bit

Fall-through inside Switch Statement

The Fall-through. It means Within Switch if any case is Matched from that case onword all Statement will be Executed until break or end of the switch this is called fall - through inside swtich.

Advantage

1. Fall through inside switch is we can Define Common Action for Multiple cases.(code reusability).

Example:


public class JavaBitSwitch {
public static void main(String[] args) {
int number=5;
switch(number){
// without break statements
case 5:
System.out.println("5");
case 15:
System.out.println("15");
case 20:
System.out.println("20");
default:System.out.println("We can not print 5, 15 or 20");
}
}
}


Output: 5,15,20, We can not print 5, 15 or 20

Advertisement

Copyright 2017 Design& Development