Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use enumerated types in Java

2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article is to share with you about how to use enumerated types in Java. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

1Universe / define enumeration type

2enum Day {

3 MONDAY, TUESDAY, WEDNESDAY

4 THURSDAY, FRIDAY, SATURDAY, SUNDAY

5}

Let's first take a look at his concept. Enumerated types are a new feature of Java5. It is a special data type because it is not only a class type, but also has more special constraints than ordinary types.

1. Why use it?

For example, if we don't have to enumerate types, just define constants, as shown in the following figure:

But there are shortcomings in doing so, such as I wrote a wrong word in it, as shown in the following picture:

He doesn't prompt me, but if I use enumerations, he can prompt me to repeat the data, as shown in the following figure:

In addition, there is no need to enumerate types, the code is not concise enough, and there is too much redundant code.

two。 What is an enumeration?

We first compile a Day.java file written above, and then decompile it to see what the result is, as shown in the following figure:

From the figure above, we can see that the decompiled Day is a final type, that is, it cannot be inherited by other classes, and it inherits from the Enum class. If we define an enumeration type, Java actually does the job of defining constants for us, which is the area in figure 1 above. Area 3pr 4 is inserted by the compiler, and the picture below is borrowed by someone else.

It is worth noting that these two methods are compiled and inserted, so it only stays at the level of this class. If you don't believe me, let's try.

3. Read the source code

Let's find the Enum class and see what methods are in it and what they do.

CompareTo: compare the difference between my ordinal and yours.

Equals: compare enumerated types for equality.

GetDeclaring: the class that corresponds to the enumerated type value.

Ordinal: returns the ordinal of the enumeration constant.

ValueOf: returns the enumeration constant of the specified enumeration type with the specified name. If it does not exist, it will directly report an error, such as the red area in the following figure.

Empty talk is no fun. Let's practice.

This is how enumerated types are used in Java. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report