- 1Java Exceptions
- 2Java Keywords
- 3Java abstract Keyword
- 4Java assert Keyword
- 5Java boolean Keyword
- 6Java break Keyword
- 7Java byte Keyword
- 8Java case Keyword
- 9Java catch Keyword
- 10Java char Keyword
- 11Java class Keyword
- 12Java const Keyword
- 13Java continue Keyword
- 14Java default Keyword
- 15Java do Keyword
- 16Java double Keyword
- 17Java else Keyword
- 18Java enum Keyword
- 19Java extends Keyword
- 20Java final Keyword
- 21Java finally Keyword
- 22Java float Keyword
- 23Java for Keyword
- 24Java goto Keyword
- 25Java if Keyword
- 26Java implements Keyword
- 27Java import Keyword
- 28Java instanceof Keyword
- 29Java int Keyword
- 30Java interface Keyword
- 31Java long Keyword
- 32Java native Keyword
- 33Java new Keyword
- 34Java null Keyword
- 35Java package Keyword
- 36Java private Keyword
- 37Java protected Keyword
- 38Java public Keyword
- 39Java return Keyword
- 40Java short Keyword
- 41Java static Keyword
- 42Java strictfp Keyword
- 43Java super Keyword
- 44Java switch Keyword
- 45Java synchronized Keyword
- 46Java this Keyword
- 47Java transient Keyword
- 48Java try Keyword
- 49Java void Keyword
- 50Java volatile Keyword
- 51Java while Keyword
- 52Java String Methods - Syntax and Description
- 53Java String
charAt()
method - 54Java String
codePointAt()
method - 55Java String
codePointBefore()
method - 56Java String
codePointCount()
method - 57Java String
compareTo()
method - 58Java String
compareToIgnoreCase()
method - 59Java String
concat()
method - 60Java String
contains()
method - 61Java String
contentEquals()
method - 62Java String
copyValueOf()
method - 63Java String
endsWith()
method - 64Java String
equals()
method - 65Java String
equalsIgnoreCase()
method - 66Java String
format()
method - 67Java String
getBytes()
method - 68Java String
getChars()
method - 69Java String
hashCode()
method - 70Java String
indexOf()
method - 71Java String
intern()
method - 72Java String
isEmpty()
method - 73Java String
join()
method - 74Java String
lastIndexOf()
method - 75Java String
length()
method - 76Java String
matches()
method - 77Java String
offsetByCodePoints()
method - 78Java String
regionMatches()
method - 79Java String
replace()
method - 80Java String
replaceAll()
method - 81Java String
replaceFirst()
method - 82Java String
split()
method - 83Java String
startsWith()
method - 84Java String
subSequence()
method - 85Java String
substring()
method - 86Java String
toCharArray()
method - 87Java String
toLowerCase()
method - 88Java String
toString()
method - 89Java String
toUpperCase()
method - 90Java String
trim()
method - 91Java String
valueOf()
method - 92Java ArrayList Methods - Complete Reference with Syntax and Description
- 93Java LinkedList Methods - Complete Reference with Syntax and Description
- 94Java HashMap Methods - Syntax and Descriptions
Java switch Keyword
Usage and Examples
switch
Keyword in Java
The switch
keyword in Java is used for decision-making. It's an alternative to writing many if-else-if
statements, and it allows you to test a single expression against multiple possible values. When readability and clarity matter, switch
often makes your code cleaner and more elegant.
Basic Syntax of switch
switch (expression) {
case value1:
// code block
break;
case value2:
// code block
break;
...
default:
// default block
}
Here’s what each part means:
- expression: A variable or value evaluated once, usually
int
,char
,String
(since Java 7), orenum
. - case: Each
case
is checked against the value of the expression. - break: Exits the switch block. Without it, execution "falls through" to the next case.
- default: Optional. Executes if no match is found.
Example 1: Integer-Based switch
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;
default:
System.out.println("Another day");
}
Wednesday
Since day
is 3, the third case matches and prints "Wednesday". The break
prevents the next cases from being executed.
Example 2: What Happens Without break?
int rating = 4;
switch (rating) {
case 5:
System.out.println("Excellent");
case 4:
System.out.println("Good");
case 3:
System.out.println("Fair");
default:
System.out.println("Needs improvement");
}
Good
Fair
Needs improvement
Without break
, the switch statement "falls through" and executes all subsequent cases. That’s why it prints everything starting from case 4
.
Example 3: Using String in switch (Java 7+)
String role = "admin";
switch (role) {
case "admin":
System.out.println("Welcome Admin");
break;
case "editor":
System.out.println("Welcome Editor");
break;
default:
System.out.println("Role not recognized");
}
Welcome Admin
Java allows String
types in switch
statements starting with Java 7, making it useful for text-based conditions.
Example 4: switch with char
char grade = 'B';
switch (grade) {
case 'A':
System.out.println("Excellent!");
break;
case 'B':
System.out.println("Well done");
break;
case 'C':
System.out.println("You passed");
break;
default:
System.out.println("Invalid grade");
}
Well done
Example 5: switch with enum
enum Direction {
NORTH, SOUTH, EAST, WEST
}
Direction dir = Direction.EAST;
switch (dir) {
case NORTH:
System.out.println("You are heading North");
break;
case EAST:
System.out.println("You are heading East");
break;
}
You are heading East
enum
types make switch
more expressive and type-safe, often used in game dev or control systems.
Best Practices for switch
- Always include
break
unless fall-through is intentional. - Use
default
to handle unexpected input. - Consider using
enum
for finite state values to improve readability and safety. - Be cautious when using
String
—it’s case-sensitive.
When Not to Use switch
If you need to compare ranges or perform more complex conditions, if-else-if
blocks may be more appropriate.