Java Reference
In-Depth Information
Ternary Expression
Conditional expressions come in two different flavors. The first one is the ter-
nary expression, with this syntax:
condition ? thenExpression : elseExpression
For example:
name != null ? name : 'unknown'
switch
The second conditional expression is the switch expression, with this syntax:
switch (expression) {
(case expression : thenExpression)*
default : catchAllExpression
}
The default part is mandatory because switch is an expression; therefore, it
needs to evaluate to something in any case. This is an example from GMF, found
in GenModelUtils.ext:
String getClassifierAccessorName(genmodel::GenClassifier gc) :
switch (gc.getEcoreClassifier().name) {
case "Class" : "Class_"
case "Name" : "Name_"
default : gc.getEcoreClassifier().name
}
;
Boolean expressions have an abbreviation:
switch {
case booleanExpression : thenExpression
default : catchAllExpression
}
Chain Expression
Expressions and functional languages should be as free of side effects as possible.
But sometimes side effects are necessary. In some cases, expressions don't have a
Search WWH ::




Custom Search