Java Enum usage example and javaenum usage example
Public enum MyEnum {Monday, Tuesday, Wednesday, Thursady, Friday, Saturday, Sunday; public static void main (String [] args) {// Enum object MyEnum mye; mye = MyEnum. sunday; MyEnum mye1 = MyEnum. monday;/*** enum convert to int * int java. lang. enum. ordinal () */System. out. println (mye. ordinal (); System. out. println (mye1.ordinal ();/*** toString () * String java. lang. enum. name () */System. out. println (mye); System. out. println (mye. name ();/*** int convert to enum */MyEnum mye2 = MyEnum. values () [1]; MyEnum mye3 = mye2.values () [2]; System. out. println (mye2); System. out. println (mye3);/*** is equal or not * boolean java. lang. enum. equals (Object other) */System. out. println (MyEnum. monday. equals (MyEnum. monday);/*** foreach out */System. out. println (mye. values (). length); for (MyEnum e: MyEnum. values () {System. out. println (e );}}}
Running result:
6
0
Sunday
Sunday
Tuesday
Wednesday
True
7
Monday
Tuesday
Wednesday
Thursady
Friday
Saturday
Sunday