Prefer switch over if-else

posted 29 Dec 2010, 06:20 by Sanjeev Kumar   [ updated 11 Nov 2011, 06:11 ]
In most cases switch will be lighter and performs faster than an if/else ladder. The compiler is able to optimize switch statements into a lookup table and perform compile-time checking for literals when dealing with enumerations, so I'd suggest that it's usually preferable to use switch over if/else if if you're dealing with numeric or enum types in Java.