I am learning a java language.I have a question , how enum is type safe in java.
public enum Direction {EAST,WEST,NORTH,SOUTH;}
How enum Direction is type safe? Please help me.
I am learning a java language.I have a question , how enum is type safe in java.
public enum Direction {EAST,WEST,NORTH,SOUTH;}
How enum Direction is type safe? Please help me.
Let's compare your Direction enum with a plain String alternative.
A user can only pass a valid enum or null into any method. You're guaranteed to get NORTH, SOUTH, EAST, or WEST.
If you use a String instead you can get blank, "Foo", or any other thing that the user decides to pass.
An enum spells out the correct values and limits what you get. MUCH safer.