Home > Java > javaTutorial > Usage of enum in java

Usage of enum in java

下次还敢
Release: 2024-04-25 21:48:21
Original
1036 people have browsed it

Enumerations are used in Java to define a limited set of constants, providing features such as singleton, immutability, and comparability. Specific usage methods include: defining enumerations, obtaining enumeration constant names and order, obtaining constants by name, and comparing constants. Enumerations are often used to represent a limited set of options for colors, seasons, file types, etc.

Usage of enum in java

Usage of enumerations in Java

Enumerations are used in Java to represent fixed and limited set values. of a type. It provides a safe and reliable way to deal with a predefined set of constants.

How to use enumerations

To define an enumeration, use the enum keyword, followed by the name of the enumeration:

<code class="java">enum Color {
  RED,
  GREEN,
  BLUE
}</code>
Copy after login

This creates three enumeration constants: RED, GREEN, and BLUE.

Characteristics of enumerations

  • Single case: Each enumeration constant is a singleton object, which means that it There is only one instance.
  • Immutable: Once an enumeration constant is created, it cannot be modified.
  • Comparability: Enumeration constants can be safely compared using the == and != operators.

Uses of enumerations

Enumerations are often used to represent a limited set of options, for example:

  • Color
  • Season
  • File Type
  • HTTP Status Code

Enumeration Methods

Enumeration provides The following useful methods:

  • #name(): Returns the name of the enumeration constant.
  • ordinal(): Returns the order of the enumeration constants in the enumeration (starting from 0).
  • valueOf(String name): Get the enumeration constant by name.

Example

The following example shows how to use enumerations:

<code class="java">enum Color {
  RED,
  GREEN,
  BLUE
}

public class Main {
  public static void main(String[] args) {
    Color color = Color.RED;

    // 打印枚举常量的名称
    System.out.println(color.name()); // 输出:RED

    // 打印枚举常量的顺序
    System.out.println(color.ordinal()); // 输出:0

    // 通过名称获取枚举常量
    Color otherColor = Color.valueOf("GREEN");

    // 比较两个枚举常量
    if (color == otherColor) {
      System.out.println("两个颜色相等");
    }
    else {
      System.out.println("两个颜色不相等");
    } // 输出:两个颜色不相等
  }
}</code>
Copy after login

The above is the detailed content of Usage of enum in java. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template