Enum in Switch Case in Java

Introduction

Java 5 introduces a new feature known as enums. We can also do switching using enums. The Switch case works only with int, short, char and byte values but using enums this problem is eliminated.

Enums

The following describes enums:

  • Enums are introduced in Java 5.
     
  • These are data types in Java that contain fixed values in Java.
     
  • These can be used for months, categories, names and so on.
     
  • Type safety is also improved with the introduction of enums.
     
  • Functions, methods and interfaces are also implemented by enums.
     
  • Enums can also be used in switch case in Java.

Example :

package pkgenum;

public class Enum

{

    public enum Friends { raj, ram, aj, nick, mike, mj, jj}

    public static void main(String[] args)

    {

        for(Friends f : Friends.values())

            System.out.println(f);

    }

}

 

Output:

enum.jpg

 

Switch Case


The following describes Switch Case:

  • Supports int, short, char, byte data types only.
     

  • Is a substitute for the else-if ladder.
     

  • Is used to control the flow of our program.

Syntax :

 

switch (expression)

{

    case 1:

    {

        statements...

        break;

    }

    case 2:

    {

        statements...

        break;

    }

    default:

    {

        statements...

        break;

    }

}

 

Example :

 

package pkgswitch;

public class Switch

{

    public static void main(String[] args)

    {

        int age = 21;

        switch(age)

        {

            case 20:

            {

                System.out.println("twenty");

                break;

            }

            case 21:

            {

                System.out.println("twenty one");

                break;

            }   

            case 22:

            {

                System.out.println("twenty two");

                break;

            }   

            case 23:

            {

                System.out.println("twenty three");

                break;

            }

            default:

            {

                System.out.println("out of range");

            }   

        }

    }

}

 

Output:

switch case.jpg

 

Enum in Switch Case

 

Enums can be used in switch case as follows:

 

package demo;

public class Demo

{

   public enum Month

   {

        JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC

   }

   public static void main(String args[])

   {

       Month[] MonthNow = Month.values();

       for (Month Now : MonthNow)

       {

            switch (Now)

            {

                case JAN:

                {   

                    System.out.println("JANUARY");

                    break;

                }

                case FEB:

                {

                    System.out.println("FEBRUARY");

                    break;

                }

                case MAR:

                {

                    System.out.println("MARCH");

                    break;     

                }

                case APR:

                {

                    System.out.println("APRIL");

                    break;

                }

                case MAY:

                {

                    System.out.println("MAY");

                    break;

                }

                case JUN:

                {

                    System.out.println("JUNE");

                    break;

                }

                case JUL:

                {

                    System.out.println("JULY");

                    break;

                }

                case AUG:

                {

                    System.out.println("AUGUST");

                    break;

                }

                case SEP:

                {

                    System.out.println("SEPTEMBER");

                    break;

                }   

                case OCT:

                {

                    System.out.println("OCTOBER");

                    break;

                }

                case NOV:

                {

                    System.out.println("NOVEMBER");

                    break;

                }

                case DEC:

                {

                    System.out.println("DECEMBER");

                    break;

                }   

            }

        }

    }

}

 

Output:

enum in switch case.jpg

 

Summary

 

This article will introduce you to enums and switch cases in Java. This also provided an example of an enum in a switch case.

Up Next
    Ebook Download
    View all
    Learn
    View all