简体   繁体   中英

Java: What is the difference (also in use) between an enumeration and a value class?

I fail to see the difference between a value class and an enumeration. It would be great if someone could tell me the difference between them and what the difference in their use is.

First of all, it is not a "value class". It is a "value-based class". Please try to use the correct terminology.

The "Value-based Classes" blog entry by Nicolai Parlog explains in considerable detail what these are all about. In essence, they are a predecessor of "value types" that are likely to be added in a future version of Java. (Probably Java 10 - see Project Valhalla .)

The main reason that enum types (in general) are not value-based classes is that they are not necessarily immutable. Immutability is one of the key requirements for a class to be value-based. (There are other aspects of enums that don't quite match up with the requirements, but the special nature of enum values probably mean that these would not matter.)

Right now, the "value-based class" characteristic has no direct use. But it may do in a future version of Java.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM