Back to questions
JuniorTheoryCommon
What does the program print?
public class Main {
public static void main(String[] args) {
Level level = Level.HIGH;
System.out.println(level.ordinal() + " " + level);
}
}
enum Level { LOW, MEDIUM, HIGH }2 HIGH
- Expecting
3 HIGH:ordinal()numbers the constants from zero, so the third one,HIGH, gives2. - Expecting a number when the constant itself is printed: an enum constant prints its name,
HIGH, the same textname()returns.
- What does
Level.values().lengthreturn for this enum? - Why is it risky to save
ordinal()to a file as the code of a value?