API / Apache FreeMarker Interview questions
What is the difference between FreeMarker's #switch/#case and a chain of #if/#elseif directives?
<#switch value> tests a single expression for equality against each <#case constant> in order and renders the matching block, with an optional <#default> for no match; unlike a Java switch, each case implicitly stops at the next #case/#default with no fall-through, though an explicit <#break> can still exit a case early before its natural end.
<#switch user.role> <#case "ADMIN">Administrator<#break> <#case "EDITOR">Editor<#break> <#default>Guest </#switch>
A chain of <#if>/<#elseif> can test entirely different, arbitrary boolean expressions in each branch - ranges, multiple combined conditions, method calls - not just equality of one value against constants, which makes it strictly more flexible. #switch/#case trades that flexibility for readability: when the logic really is "pick one branch based on this single value," it communicates that intent more clearly than an equivalent stack of #elseif value == "X" comparisons.
More Related questions...