An Enumeration is a list of named constants. We can create constants in Java using final and static keywords as well. But whenever we want a variable to hold a value from a set of valid values, [...]
An enum is created using ‘enum‘ keyword in Java. When we define an enum, we are implicitly extending java.lang.Enum. The compiler converts our enum into a class which extends [...]
Even though enum is a class type, we do not use new keyword but create a variable just like a primitive type as follows. PizzaSize size; (Or) PizzaSize size = PizzaSize.LARGE; The main advantage [...]
We can use either == or equals() method to compare enum values. Example: Both the ‘if’ statements in the below code are equivalent. SIZE == LARGE SIZE EQUALS LARGE Pizza Size is LARGE
Just like how we used enums for comparison in ‘if’ statement, we can also use enums in switch case as follows. The enum variable is specified as switch expression and all the case [...]
If we want to iterate all the values in the enum, we can use the values() method of enum which returns an array of enum values as enum type. This method values() is actually inserted by the [...]