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

What is the use of java enumeration

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly shows you "what is the use of java enumeration", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "what is the use of java enumeration" this article.

I. basic concepts

Enumeration is a new feature introduced by Java1.5. Enumeration classes are defined by the keyword enum. An enumerated class is a special class that, like ordinary classes, can use constructors, define member variables and methods, and implement one or more interfaces, but enumerated classes cannot inherit other classes.

Advantages and disadvantages of enumerations 1. Advantages and disadvantages

Enumerations are recommended instead of all constant Code in Effctive Java for the following reasons:

(1) Type check, validity check

(2) enumeration, as a class, can have its own properties (usually a constant, which I have never encountered) and its own methods (otherwise it can only be written in switch, which actually violates the principle)

(3) compared with constants, you can know all possible return values directly without looking at the document and source code, which is convenient for coding.

However, the problem here lies in the first point, in fact, (1) is not inevitable in a distributed environment. If the return value of an interface is allowed to have undefined content in the business process, an exception should not be thrown in deserialization and does not have to stick to (1). At the same time, from the point of view of points (2) and (3), the impact of this limiting the scope of enumeration is great. Rewrite the enumerations that have their own properties and methods into the cooperation of code and other methods, the amount of code required increases a lot, and the code decay increases greatly.

2. Shortcomings

(1) because single inheritance is supported in Java, enumerated types can no longer inherit other classes

(2) the problem that may be caused by using enumerations as return values is that it will cause deserialization exceptions if the client and server versions are inconsistent, so Alibaba JAVA Development Manual takes measures to avoid exceptions as far as possible, so it is forbidden to define enumerations as return values.

Third, solve the ifelse

For business development, the complexity of business logic is inevitable, with the development of business, the requirements will only become more and more complex, in order to take into account a variety of situations, there will inevitably be a lot of if-else in the code.

Once there is too much if-else in the code, it will greatly affect its readability and maintainability, and the code appears to be very low.

Enumeration can solve this problem.

About enumeration and switch is a relatively simple topic, when using switch to judge the condition, the condition parameters can only be integer, character type. The enumerated type is indeed supported by switch, and switch also supports strings after java 1.7. Here we take a brief look at the use of switch and enumerated types

Static void testSwitch (Week week) {switch (week) {case MONDAY: System.out.println (week.getMeaning ()); break; case TUESDAY: System.out.println (week.getMeaning ()); break; case WEDNESDAY: System.out.println (week.getMeaning ()); break Case THURSDAY: System.out.println (week.getMeaning ()); break; case FRIDAY: System.out.println (week.getMeaning ()); break; case SATURDAY: System.out.println (week.getMeaning ()); break; case SUNDAY: System.out.println (week.getMeaning ()) Break; default: System.out.println ("you typed it incorrectly"); break;}} IV. Common methods of enumeration

The above is all the content of the article "what is the use of java enumeration?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to 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

Development

Wechat

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

12
Report