JIYIK CN >

Current Location:Home > Learning > PROGRAM > Java >

Switching on enumeration types in Java

Author:JIYIK Last Updated:2025/04/14 Views:

This article explains how to use enum in Java . We will use statement switchwith enum in two ways .switch


In Java, we use traditional switch and case to perform enumeration operations.switch

In this example, we SwitchEnumcreate an enumeration in the class and name it Days. It has seven constants, which are the days of the week. We use the switch and case methods to display a different message for each day.

We get the value from the enumeration using the name of the constant, like Days.MONDAYwill get the constant MONDAY, which will be stored dayin the enumeration object. We can use it to switch different cases. switch()gets the value to switch, that is day. Finally, we specify each case and the output it should produce.

We have to switchbreak each case in so that it interrupts execution once it completes.

public class SwitchEnum {
  enum Days { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY }

  public static void main(String[] args) {
    Days day = Days.MONDAY;

    switch (day) {
      case SUNDAY:
        System.out.println("Sundays are wonderful");
        break;

      case MONDAY:
        System.out.println("Mondays are boring");
        break;

      case TUESDAY:
        System.out.println("Tuesdays are OK");
        break;

      case WEDNESDAY:
        System.out.println("Wednesdays are tiring");
        break;

      case THURSDAY:
        System.out.println("Thursdays are even more boring");
        break;

      case FRIDAY:
        System.out.println("Fridays means work work and work");
        break;

      case SATURDAY:
        System.out.println("Saturdays makes everybody happy");
        break;
    }
  }
}

Output:

Mondays are boring

Using enhanced switch and case in Java 12 to switchoperate on Enum

In Java 12, enhanced switch and case are introduced to overcome the shortcomings of traditional switch and case. The biggest disadvantage of the traditional switch statement is that we have to specify the break keyword in each case.

Now with the enhanced switch and case, we can use enumerations in a shorter code. In the new switch and case, we use arrows instead of colons. Since we only want to print one statement, we don't have to use any braces.

public class SwitchEnum {
  enum Days { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY }

  public static void main(String[] args) {
    Days day = Days.SATURDAY;

    switch (day) {
            case SUNDAY -> System.out.println("Sundays are wonderful");
            case MONDAY -> System.out.println("Mondays are boring");
            case TUESDAY -> System.out.println("Tuesdays are OK");
            case WEDNESDAY -> System.out.println("Wednesdays are tiring");
            case THURSDAY -> System.out.println("Thursdays are even more boring");
            case FRIDAY -> System.out.println("Fridays means work work and work");
            case SATURDAY -> System.out.println("Saturdays makes everybody happy");
        }

    }
}

Output:

Saturdays makes everybody happy

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

How to delay for a few seconds in Java

Publish Date:2025/04/14 Views:130 Category:Java

This tutorial explains how to create program delays in Java and gives some sample code to understand it. There are several ways to create a time delay, such as TimeUnit.sleep() ,, ScheduleAtFixedRate() etc. Thread.sleep() Let's look at an e

How to Convert Hashmap to JSON Object in Java

Publish Date:2025/04/14 Views:112 Category:Java

This article explains how to convert a Hashmap to a JSON object in Java. We will see a detailed example of creating a hashmap and then converting it to a JSON object. Hashmap and JSON are both very commonly used by developers as they help u

How to sort a Map by value in Java

Publish Date:2025/04/14 Views:170 Category:Java

This tutorial explains how to Mapkey, value sort pairs by value in Java and lists some sample codes to understand it. There are several ways to Map sort . Here we use sort() the , sorted() method, and Comparator interface. Let's look at an

How to Print a HashMap in Java

Publish Date:2025/04/14 Views:84 Category:Java

This tutorial explains how to print HashMap an element in Java and also provides some sample code to understand the topic. HashMap It is Map an implementation class of the interface and is used to collect elements into key and value pairs.

Updating Hashmap Values in Java

Publish Date:2025/04/14 Views:88 Category:Java

This article explains how to use the two methods contained in the HashMap class in Java - update put() and replace() update the values ​​in a HashMap. hashmap.put() Update the value in Hashmap in Java using When we want to HashMap inser

Difference between hashmap and map in Java

Publish Date:2025/04/14 Views:193 Category:Java

This tutorial explains the key differences between Map and in Java HashMap . In Java, Map is an interface for storing data in key-value pairs, and HashMap is Map an implementation class of the interface. Java has several classes ( TreeHashM

Get user home directory in Java

Publish Date:2025/04/14 Views:161 Category:Java

This tutorial explains how to get the user's home directory in Java and lists some sample code to guide you through the topic. For a multi-user operating system, each user has a file system directory; this directory is called the user's hom

Difference between size and length in Java

Publish Date:2025/04/14 Views:114 Category:Java

This tutorial explains the difference between size and length in Java. We have also listed some sample codes to help you understand the topic. Java has a size() method and a length property. Beginners may think that they are interchangeable

Mutex Locks in Java

Publish Date:2025/04/14 Views:198 Category:Java

In the field of computer science, mutual exclusion or mutex is known as the property of concurrency control. Every computer uses a minimum sequence of program instructions called a thread. At a time, the computer works on one thread. For be

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial