web123456

Conversion of java enum and int type

For:

enum Color { RED,BLUE,BLACK YELLOW,GREEN};


(1) ordinal() method: Returns the order in which enum values ​​are in the enumeration class. This order depends on the order in which the enum value is declared.
();  //Return result: 0
();  //Return result: 1
(2) compareTo() method: Enum implements the interface, so the order of objects and specified objects can be compared. compareTo in Enum returns the difference in the order of the two enum values. Of course, the premise is that the two enum values ​​must belong to the same enum class, otherwise a ClassCastException() exception will be thrown. (For details, see the source code)
();  //Return result -1
(3) values() method: Static method, returning an array containing all enum values.
                 Color[] colors=();
                 for(Color c:colors){
                        (c+","); 

(4) toString() method: Returns the name of the enumeration constant.
                 Color c=;
(c);//Return result: RED
(5) valueOf() method: This method corresponds to the toString method, returning an enum constant of the specified enum type with the specified name.
("BLUE");   //Return result:

(6) equals() method: Compare the references of two enumeration class objects.

Summarize:

1.  enum<->int

enum -> int: int i ();

int -> enum: enumType b= ()[i];

2.  enum<->String

enum -> String: ()

String -> enum: (name);