The use of conditionals is not an uncommon occurrence; however, the syntax is a little verbose for such a common occurrence. For these cases, the Elvis operator, ?:, can be used to make the code a little more concise. The Elvis operator allows you to return a non-null value in an expression if the left-hand side of the expression evaluates to null. We'll find an example of this in the following snippet:
//if args?.size is non null, use args.size, otherwise return 0
fun parseArgs(args: Array<String>?) {
val argCount2 = args?.size ?: 0 // return 0 if args?.size is null
}
In this code, if the null-safe call, args?.size, evaluates to null, then the Elvis operator will provide the value specified on the right-hand side, in this case, 0.
After sorting this mistake, let's finally see how to integrate Kotlin with Java.