- Kotlin Programming By Example
- Iyanu Adelekan
- 617字
- 2021-08-27 20:00:11
The do…while loops
The do…while loop is similar to the while loop, with the exception that in the conditional test for the reiteration of the loop, it is carried out after the first iteration. It takes the following form:
do {
...
} while (condition)
The statements within the block are executed while the condition tested holds true:
var i = 0
do {
println("I’m in here!")
i++
} while (i < 10)
println("I’m out here!")
Nullable values: The NullPointerException is one thing that individuals who have first-hand experience writing Java code are certain to have encountered. The Kotlin type system is null-safe—it attempts to eliminate the occurrence of null references within code. As a result, Kotlin possesses nullable types and non-nullable types (types that can hold a null value and those that can't).
To properly explain the NullPointerException, we will consider the following Java program:
class NullPointerExample {
public static void main(String[] args) {
String name = "James Gates";
System.out.println(name.length()); // Prints 11
name = null; // assigning a value of null to name
System.out.println(name.length()); // throws NullPointerException
}
}
The preceding program performs the simple task of printing the length of a string variable to the standard system output. There is only one problem with our program. When we compile and run it, it throws a null pointer exception and terminates midway through execution, as we can see the following screenshot:

Can you spot the cause of the NullPointerException? The exception arises as a result of the String#length method being used on a null reference. As such, the program stops executing and throws the exception. Clearly, this is not something we want to occur in our programs.
We can prevent this in Kotlin by preventing the assignment of a null value to the name object:
var name: String = "James Gates"
println(name.length)
name = null // null value assignment not permitted
println(name.length)
As can be seen in the following screenshot, Kotlin's type system detects that a null value has been inappropriately assigned to the name variable and swiftly alerts the programmer of this blunder so it can be corrected:

At this point, you may be wondering what happens if a scenario arises in which the programmer intends to permit the passing of null values. In that scenario, the programmer simply declares the value as nullable by appending ? to the type of the variable:
var name: String? = "James"
println(name.length)
name = null // null value assignment permitted
println(name.length)
Regardless of the fact that we have declared the variable name to be nullable, we'll still get an error upon running the program. This is because we must access the length property of the variable in a safe way. This can be done with ?.:
var name: String? = "James"
println(name?.length)
name = null // null value assignment permitted
println(name?.length)
Now that we have used the ?. safe operator, the program will run as intended. Instead of a null pointer exception being thrown, the type system recognizes that a null pointer has been referenced and prevents the invocation of length() on the null object. The following screenshot shows the type-safe output:

An alternative to using the ?. safe operator would be to use the !! operator. The !! operator allows the program to continue execution and throws a KotlinNullPointerException once a function invocation is attempted on a null reference.
We can see the effects by replacing ?. with !!. in our written program. The following screenshot shows the output of the program when run. A KotlinNullPointerException is thrown as a result of the utilization of the !! operator:

- Advanced Quantitative Finance with C++
- 深度實踐OpenStack:基于Python的OpenStack組件開發
- JavaScript前端開發模塊化教程
- 數字媒體應用教程
- x86匯編語言:從實模式到保護模式(第2版)
- Visual Basic程序設計教程
- 信息安全技術
- Java Web基礎與實例教程
- Mastering Rust
- 零基礎輕松學SQL Server 2016
- Teaching with Google Classroom
- Selenium Testing Tools Cookbook(Second Edition)
- jQuery Mobile移動應用開發實戰(第3版)
- ArcGIS for Desktop Cookbook
- Vue.js 3應用開發與核心源碼解析