filmov
tv
advance-Java Enumeration in Java #26

Показать описание
#java #coreJava #Enumeration #enums
@kingoftechniques6864
Enumerations serve the purpose of representing a group of named constants in a programming language. For example, the 4 suits in a deck of playing cards may be 4 enumerators named Club, Diamond, Heart, and Spade, belonging to an enumerated type named Suit. Other examples include natural enumerated types (like the planets, days of the week, colors, directions, etc.).
Enums are used when we know all possible values at compile-time, such as choices on a menu, rounding modes, command-line flags, etc. It is not necessary that the set of constants in an enum type stay fixed for all time.
In Java (from 1.5), enums are represented using enum data type. Java enums are more powerful than C/C++ enums. In Java, we can also add variables, methods, and constructors to it. The main objective of enum is to define our own data types(Enumerated Data Types).
Declaration of enum in Java: Enum declaration can be done outside a Class or inside a Class but not inside a Method.
Every enum constant represents an object of type enum.
Enum and Inheritance:
enum can implement many interfaces.
values(), ordinal() and valueOf() methods:
values() method can be used to return all values present inside the enum.
Order is important in enums.By using the ordinal() method, each enum constant index can be found, just like an array index.
valueOf() method returns the enum constant of the specified string value if exists.
@kingoftechniques6864
Enumerations serve the purpose of representing a group of named constants in a programming language. For example, the 4 suits in a deck of playing cards may be 4 enumerators named Club, Diamond, Heart, and Spade, belonging to an enumerated type named Suit. Other examples include natural enumerated types (like the planets, days of the week, colors, directions, etc.).
Enums are used when we know all possible values at compile-time, such as choices on a menu, rounding modes, command-line flags, etc. It is not necessary that the set of constants in an enum type stay fixed for all time.
In Java (from 1.5), enums are represented using enum data type. Java enums are more powerful than C/C++ enums. In Java, we can also add variables, methods, and constructors to it. The main objective of enum is to define our own data types(Enumerated Data Types).
Declaration of enum in Java: Enum declaration can be done outside a Class or inside a Class but not inside a Method.
Every enum constant represents an object of type enum.
Enum and Inheritance:
enum can implement many interfaces.
values(), ordinal() and valueOf() methods:
values() method can be used to return all values present inside the enum.
Order is important in enums.By using the ordinal() method, each enum constant index can be found, just like an array index.
valueOf() method returns the enum constant of the specified string value if exists.
Комментарии