What is Elvis operator(?:) in Kotlin in Ubuntu 20.04:
In Kotlin, the Elvis operator is used to obtain the nullable value. If the conditional expression is null, it can be used to return the non-null value. We can use the operator ?: known as the Elvis operator that will check the nullability of a given variable. It can be utilized if we have surety that we are specifically referring to a non-nullable value. The Elvis operator is used in Kotlin to avoid the null pointer exception. It can be used to check whether a variable is null or not. If it is, then it allows us to utilize a single default value.
Syntax of the Elvis operator(?:) in Kotlin in Ubuntu 20.04:
The general syntax of the Elvis operator used in Kotlin is given below:
The Elvis operation takes two parameters and returns either the first if it’s not null or the second if it’s not null. It’s a null safety check variation of the ternary conditional operator. To check whether the Elvis operator returns the expression to the left”?:”, If it is not null, the expression is returned to the right side. It’s important to note that the rightmost expression is only executed if the left side is null.
How to use Lambda function in Kotlin in Ubuntu 20.04:
To understand the basics of using the Elvis operator in the Kotlin language, we should take a look at the examples given below:
Example # 1: Program without Elvis operator(?:)in Kotlin in Ubuntu 20.04:
We have the conventional method for checking null safety in kotlin. We are using the if-else statement implementation in the below example program.
The main function is declared in the preceding code. We’ve used the “val” keyword to define a variable in our main function. The variable is defined as “str1” and we have performed a safety check before initializing it with the value. To check whether or not the variable “str1” has some value or not. Then, we have assigned the null value to a variable “str1”. We have created another variable that defines “str2” and also applies a null safety check operator to it. The variable this time is initialized with the string value. Next, we use one line if-else statements in the variables “length_1” and “length_2”. The “if” condition is taking “str1” which is kept with a not equal sign and should be not equal to null. If the string is not null, it returns the length of the specified string; otherwise, it returns -1. The kotlin println statement will print the length of the string upon the condition.
Upon execution, we have length -1 of “str1” as it has a null value; on the other hand, we have “str2” length “24” as it contains a string. The result is shown on the screen.
Example # 2: Program with Elvis operator(?:)in Kotlin in Ubuntu 20.04:
The above if-else statements are quite time-consuming. We can avoid this by using the Elvis Operator, which works the same as the if-else statement. When trying to assign the value of a nullable variable to a non-nullable variable, the kotlin Elvis operator”?:” is used to construct a shorter conditional assignment.
We defined the main function in the above code. The main function will help to execute the implementation of the Elvis operator. Firstly, we have created a variable as a “val” keyword in our main function. The variable “val” is defined as “myStr1” and initialized the nullable string. Here, we have another variable defined as val and named as “myStr2”. We have initialized the variable “myStr2” with the string value. Then, we have a variable “myLength_1” which will print the length of the string. We have used the Elvis operator with the null checking operator”?.”. This will check if the null value exists or not. We have defined another variable as “myLength_2” for fetching the length of another string. The kotlin println function will print the strings and the lengths of the string on the console screen.
The output from using the Elvis operator is displayed on the terminal screen.
Example # 3: Program of exception in Elvis operator(?:)in Kotlin in Ubuntu 20.04:
We’ll use throw and return to the Elvis operator’s right side. We will throw an exception to the Elvis operator in the example below.
We have the main function declaration in the above code, which has the kotlin println function. The println function prints the value which is defined in the other function. The other function is declared with the keyword “fun” and named as “strLength.” The function “strLength” takes an argument of the string with the null checking operator. Then, we have a variable defined as “val.” The variable “myLength” uses the Elvis operator to confirm if a null value exists or not for the string. We have thrown an exception, “IllegalArgumentException.” If the left side of the operator fails, this exception message will occur. We have a returned value of the string in the end.
The exception message is shown along with the return value of the string in the below image.
When you have a circumstance where you take a null value and would like to manage it nicely, the Elvis operator comes in handy. The coding will be simpler to understand by others due to the Elvis operator. We aimed to give you clear information about Elvis operators in kotlin with the basic examples. Hence, the Elvis operator in kotlin will be a simple task for you when it comes to writing scripts.