case Keyword in Java
The case
keyword in Java is used within a switch
statement to define a block of code that executes when the switch expression matches a specific value. Each case
label must be unique within the switch statement and is followed by a colon.
Usage
The case
keyword is used to handle multiple conditions in a more readable and efficient manner than multiple if-else
statements. It is particularly useful when you need to execute different code based on the value of a variable.
Syntax
switch (expression) {
case value1:
// code block
break;
case value2:
// code block
break;
// more cases...
default:
// default code block
}
expression
: The variable or expression evaluated once at the beginning of the switch statement.value1
,value2
, etc.: The constant values that the expression is compared against.break
: Terminates the current case block to prevent fall-through.default
: Optional; executes if nocase
matches.
Examples
Example 1: Basic Usage
public class SwitchExample {
public static void main(String[] args) {
int day = 3;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
case 7:
System.out.println("Sunday");
break;
default:
System.out.println("Invalid day");
}
}
}
In this example, the switch
statement evaluates the value of day
. Since day
is 3, the code block for case 3
executes, printing "Wednesday".
Example 2: Using Strings in Switch
public class SwitchStringExample {
public static void main(String[] args) {
String fruit = "Apple";
switch (fruit) {
case "Apple":
System.out.println("Apple is red.");
break;
case "Banana":
System.out.println("Banana is yellow.");
break;
case "Orange":
System.out.println("Orange is orange.");
break;
default:
System.out.println("Unknown fruit.");
}
}
}
This example demonstrates the use of a switch
statement with a String
. The switch
statement evaluates the value of fruit
. Since fruit
is "Apple", it prints "Apple is red."
Example 3: Fall-through Behavior
public class FallThroughExample {
public static void main(String[] args) {
int num = 2;
switch (num) {
case 1:
System.out.println("One");
case 2:
System.out.println("Two");
case 3:
System.out.println("Three");
break;
default:
System.out.println("Default");
}
}
}
In this example, the switch
statement evaluates the value of num
. Since num
is 2, it prints "Two" and then continues to execute "Three" because there is no break
statement after case 2
.
Example 4: Nested Switch Statements
public class NestedSwitchExample {
public static void main(String[] args) {
int outer = 1;
int inner = 2;
switch (outer) {
case 1:
switch (inner) {
case 1:
System.out.println("Inner 1");
break;
case 2:
System.out.println("Inner 2");
break;
}
break;
case 2:
System.out.println("Outer 2");
break;
default:
System.out.println("Default Outer");
}
}
}
This example demonstrates the use of a nested switch
statement. The outer switch
statement evaluates the value of outer
, while the inner switch
statement evaluates the value of inner
.
Tips and Best Practices
- Always Use
break
: Use thebreak
statement to avoid fall-through behavior unless explicitly desired. - Default Case: Always include a
default
case to handle unexpected values. - Constant Values: Ensure that the
case
labels are constants and unique within the switch statement. - Data Types:
switch
statements can be used withbyte
,short
,int
,char
,String
, andenum
types. - Simplify Code: Use
switch
statements to simplify code that involves multiple conditions, making it more readable and maintainable. - Avoid
null
in Switch: Be cautious withnull
values in switch expressions to preventNullPointerException
.
switch (expression) {
case constant1:
// code block
break;
case constant2:
// code block
break;
// more cases...
default:
// default code block
}