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){casevalue1:// code blockbreak;casevalue2:// code blockbreak;// 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 no case matches.
Examples
Example 1: Basic Usage
publicclassSwitchExample{publicstaticvoidmain(String[] args){
int day =3;switch(day){case1:
System.out.println("Monday");break;case2:
System.out.println("Tuesday");break;case3:
System.out.println("Wednesday");break;case4:
System.out.println("Thursday");break;case5:
System.out.println("Friday");break;case6:
System.out.println("Saturday");break;case7:
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
publicclassSwitchStringExample{publicstaticvoidmain(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
publicclassFallThroughExample{publicstaticvoidmain(String[] args){
int num =2;switch(num){case1:
System.out.println("One");case2:
System.out.println("Two");case3:
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
publicclassNestedSwitchExample{publicstaticvoidmain(String[] args){
int outer =1;
int inner =2;switch(outer){case1:switch(inner){case1:
System.out.println("Inner 1");break;case2:
System.out.println("Inner 2");break;}break;case2:
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 the break 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 with byte, short, int, char, String, and enum 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 with null values in switch expressions to prevent NullPointerException.
switch(expression){caseconstant1:// code blockbreak;caseconstant2:// code blockbreak;// more cases...default:// default code block}제공
The
casekeyword in Java is used within aswitchstatement to define a block of code that executes when the switch expression matches a specific value. Eachcaselabel must be unique within the switch statement and is followed by a colon.Usage
The
casekeyword is used to handle multiple conditions in a more readable and efficient manner than multipleif-elsestatements. It is particularly useful when you need to execute different code based on the value of a variable.Syntax
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 nocasematches.Examples
Example 1: Basic Usage
In this example, the
switchstatement evaluates the value ofday. Sincedayis 3, the code block forcase 3executes, printing "Wednesday".Example 2: Using Strings in Switch
This example demonstrates the use of a
switchstatement with aString. Theswitchstatement evaluates the value offruit. Sincefruitis "Apple", it prints "Apple is red."Example 3: Fall-through Behavior
In this example, the
switchstatement evaluates the value ofnum. Sincenumis 2, it prints "Two" and then continues to execute "Three" because there is nobreakstatement aftercase 2.Example 4: Nested Switch Statements
This example demonstrates the use of a nested
switchstatement. The outerswitchstatement evaluates the value ofouter, while the innerswitchstatement evaluates the value ofinner.Tips and Best Practices
break: Use thebreakstatement to avoid fall-through behavior unless explicitly desired.defaultcase to handle unexpected values.caselabels are constants and unique within the switch statement.switchstatements can be used withbyte,short,int,char,String, andenumtypes.switchstatements to simplify code that involves multiple conditions, making it more readable and maintainable.nullin Switch: Be cautious withnullvalues in switch expressions to preventNullPointerException.