Switch Case with Enum in Java
From some conditions, the java switch statement executes one statement. Similar to the If-Else-If ladder statement, this can be Byte, short, int, long, enum, string, and some wrapper types like Byte, Short, Int, and Long are all supported in a switch statement.
Starting with Java 7, you can now use strings in switch statements. In other words, the switch statement compares variables for equality with different sets of values. Remarks A switch expression can contain 1 or N case values. A case value can only be of the switch expression type. Case values must be either literals or constants. He doesn't allow factors. A unique case value is required. A compile-time error occurs if there are duplicate values.
Java switch expressions must use byte, short, int, long (including wrapper types), enums, and strings. An optional break statement can accompany each case statement. After the switch expression, the control jumps when the break statement is reached. If there is no break statement, the following cases are executed: Optionally, you can assign standard designations to case values.
Syntax:
switch(expression){
case value1:
//code to be executed;
break; //optional
case value2:
//code to be executed;
break; //optional
default:
code to be executed if all cases are not matched;
}
public class SwitchExample {
public static void main(String[] args) {
//Declaring a variable for switch expression
int number=20;
//Switch expression
switch(number){
//Case statements
case 10: System.out.println("10");
break;
case 20: System.out.println("20");
break;
case 30: System.out.println("30");
break;
//Default case statement
default:System.out.println("Not in 10, 20 or 30");
}
}
}
Output:
20
Finding Month Example:
//Java Program to demonstrate the example of Switch statement
//where we are printing month name for the given number
public class SwitchMonthExample {
public static void main(String[] args) {
int month=7;
String monthString="";
switch(month){
case 1: monthString="1 - January";
break;
case 2: monthString="2 - February";
break;
case 3: monthString="3 - March";
break;
case 4: monthString="4 - April";
break;
case 5: monthString="5 - May";
break;
case 6: monthString="6 - June";
break;
case 7: monthString="7 - July";
break;
case 8: monthString="8 - August";
break;
case 9: monthString="9 - September";
break;
case 10: monthString="10 - October";
break;
case 11: monthString="11 - November";
break;
case 12: monthString="12 - December";
break;
default:System.out.println("Invalid Month!"); }
//Printing month of the given number
System.out.println(monthString); } }
Output:
1 January
Program to check Vowels or Consonants:
public class SwitchVowelExample {
public static void main(String[] args) {
char ch='O';
switch(ch)
{
case 'a':
System.out.println("Vowel");
break;
case 'e':
System.out.println("Vowel");
break;
case 'i':
System.out.println("Vowel");
break;
case 'o':
System.out.println("Vowel");
break;
case 'u':
System.out.println("Vowel");
break;
case 'A':
System.out.println("Vowel");
break;
case 'E':
System.out.println("Vowel");
break;
case 'I':
System.out.println("Vowel");
break;
case 'O':
System.out.println("Vowel");
break;
case 'U':
System.out.println("Vowel");
break;
default:
System.out.println("Consonant");
}
}
}
Output:
Vowel
Enum can be used in a switch statement in Java. The Java enum class represents the constants group. Change less like the last factors).The constants are separated by a comma and enclosed in curly braces by the keyword enum.
/Java Program to demonstrate the use of Enum
public class JavaSwitchEnumExample {
public enum Day { Sun, Mon, Tue, Wed, Thu, Fri, Sat }
public static void main(String args[]) {
Day[] DayNow = Day.values();
for (Day Now : DayNow)
{
switch (Now)
{
case Sun:
System.out.println("Sunday");
break;
case Mon:
System.out.println("Monday");
break;
case Tue:
System.out.println("Tuesday");
break;
case Wed:
System.out.println("Wednesday");
break;
case Thu:
System.out.println("Thursday");
break;
case Fri:
System.out.println("Friday");
break;
case Sat:
System.out.println("Saturday");
break;
}
}
}
}
Output:
Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
In this Switch case study, we created an enumeration of the days of the week representing all days. For example, use a loop to iterate over each enum instance. Additionally, strings have been supported in the Switch case since Java7. Similarly, using Java, you can cast an enum to a string.
I used an Enum in Java in the circle collection to create a switch-case declaration. This switches the current enum instance, and each CASE statement is its enum instance. DAY.MONDAY or DAY.TUESDAY. Since we declared the Enum in the same class, we can use the instance without using the class name, like "Monday".
Enumerations are unique data types in Java that are generally collections (sets) of constants. More specifically, Java enums are a unique kind of Java class. Enums can contain constants, methods, etc. The enum keyword can be used in if statements, switch statements, iterations, etc
By default, enum constants are public, static, and final. Enum constants are accessed using dot syntax. In addition to constants, enum classes can also have attributes and methods. You cannot create objects of enum classes or extend other classes. Enum classes can only implement interfaces.