For example, the if statement in Java allows to make the control flow depend on a certain condition:
1 List<Integer> ints = null; 2 if (checkCondition()) { 3 ints = generateList(); 4 } else { 5 ints = Collections.emptyList(); 6 }
Since if statements don’t return any values, we have to define a variable and put the assignments in both branches. In Kotlin, if is an expression, so ints can be defined as a constant and initialized immediately: