Enumerated (enum) types are data types that comprise a static, ordered set of values. They are equivalent to the Show 8.7.1. Declaration of Enumerated TypesEnum types are created using the CREATE TYPE command, for example: CREATE TYPE mood AS ENUM ('sad', 'ok', 'happy'); Once created, the enum type can be used in table and function definitions much like any other type: CREATE TYPE mood AS ENUM ('sad', 'ok', 'happy'); CREATE TABLE person ( name text, current_mood mood ); INSERT INTO person VALUES ('Moe', 'happy'); SELECT * FROM person WHERE current_mood = 'happy'; name | current_mood ------+-------------- Moe | happy (1 row) 8.7.2. OrderingThe ordering of the values in an enum type is the order in which the values were listed when the type was created. All standard comparison operators and related aggregate functions are supported for enums. For example: INSERT INTO person VALUES ('Larry', 'sad'); INSERT INTO person VALUES ('Curly', 'ok'); SELECT * FROM person WHERE current_mood > 'sad'; name | current_mood -------+-------------- Moe | happy Curly | ok (2 rows) SELECT * FROM person WHERE current_mood > 'sad' ORDER BY current_mood; name | current_mood -------+-------------- Curly | ok Moe | happy (2 rows) SELECT name FROM person WHERE current_mood = (SELECT MIN(current_mood) FROM person); name ------- Larry (1 row) 8.7.3. Type SafetyEach enumerated data type is separate and cannot be compared with other enumerated types. See this example: CREATE TYPE happiness AS ENUM ('happy', 'very happy', 'ecstatic'); CREATE TABLE holidays ( num_weeks integer, happiness happiness ); INSERT INTO holidays(num_weeks,happiness) VALUES (4, 'happy'); INSERT INTO holidays(num_weeks,happiness) VALUES (6, 'very happy'); INSERT INTO holidays(num_weeks,happiness) VALUES (8, 'ecstatic'); INSERT INTO holidays(num_weeks,happiness) VALUES (2, 'sad'); ERROR: invalid input value for enum happiness: "sad" SELECT person.name, holidays.num_weeks FROM person, holidays WHERE person.current_mood = holidays.happiness; ERROR: operator does not exist: mood = happiness If you really need to do something like that, you can either write a custom operator or add explicit casts to your query: SELECT person.name, holidays.num_weeks FROM person, holidays WHERE person.current_mood::text = holidays.happiness::text; name | num_weeks ------+----------- Moe | 4 (1 row) 8.7.4. Implementation DetailsEnum labels are case sensitive, so Although enum types are primarily intended for static sets of values, there is support for adding new values to an existing enum type, and for renaming values (see ALTER TYPE). Existing values cannot be removed from an enum type, nor can the sort ordering of such values be changed, short of dropping and re-creating the enum type. An enum value occupies four bytes on disk. The length of an enum value's textual label is limited by the The translations from internal enum values to textual labels are kept in the system catalog
Enum was introduced in Java 1.5 as a new type whose fields consists of a fixed set of constants. For example, we can create directions as Java Enum with fixed fields as EAST, WEST, NORTH, and SOUTH. Java EnumIn this tutorial, we will learn know how to create an Enum. We will also look into the benefits of using enums in java and features of enum types. We will also learn using Java EnumvalueOf , enum values , EnumSet and EnumMap with examples.Java Enum ExampleJava enum keyword is used to create an enum type. Let’s have a look at the java enum example program.
In above example, ThreadStates is the enum with fixed constants fields START, RUNNING, WAITING and DEAD. Java Enum vs ConstantsNow let’s see how java enum is better than normal constants fields in Java classes. Let’s create a similar constants class in java.
Now let’s see how both enum and constants are used in a java program:
If we look at the above example, we have two risks with using constants that are solved by the enum.
Java Enum MethodsNow let’s see more features of java enum with an example.
Java Enum Important PointsBelow are some of the important points for Enums in Java.
Java EnumSet, EnumMap, valueOf()Now we know most of the features of Enum, let’s have a look at Java Enum example program. Then we will learn some more features of an enum.
Before explaining other important features of enum, let’s see the output of the above program.
Important Points
You can checkout all the examples from our GitHub Repository. Reference: Oracle Doc |