官术网_书友最值得收藏!

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 valuesThe 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:

主站蜘蛛池模板: 苏州市| 长武县| 绵竹市| 邛崃市| 新乐市| 临猗县| 乾安县| 揭阳市| 宜宾县| 临澧县| 吴江市| 周至县| 合作市| 黔南| 疏附县| 桓台县| 鄂伦春自治旗| 龙南县| 鸡西市| 杭锦旗| 广宗县| 惠来县| 阿合奇县| 马山县| 兴义市| 峨眉山市| 连江县| 乌海市| 射洪县| 沙雅县| 日土县| 河北省| 长乐市| 嘉鱼县| 新和县| 崇州市| 铜梁县| 马公市| 安多县| 佛教| 施甸县|