Home  >  Article  >  Java  >  Comparison details of the use of Optional in Java8 and nullable types in Kotlin

Comparison details of the use of Optional in Java8 and nullable types in Kotlin

黄舟
黄舟Original
2017-09-19 10:15:411782browse

This article mainly introduces to you the comparison of the use of Optional types in Java8 and nullable types in Kotlin. The article introduces it to you in great detail through example code, which has certain reference learning value for everyone's study or work. Friends who need it, please follow the editor to learn together.

This article mainly introduces to you the relevant content about the use of Optional types in Java8 and nullable types in Kotlin. It is shared for your reference and study. I won’t say much below, let’s take a look at the detailed introduction:

In Java 8, we can use the Optional type to express nullable types.


package com.easy.kotlin;

import java.util.Optional;

import static java.lang.System.out;

/**
 * Optional.ofNullable - 允许传递为 null 参数
 * Optional.of - 如果传递的参数是 null,抛出异常 NullPointerException
 * Optional<String> b = Optional.of(s);
 */
public class Java8OptionalDemo {

 public static void main(String[] args) {
 out.println(strLength(Optional.of("abc")));
 out.println(strLength(Optional.ofNullable(null)));
 }

 static Integer strLength(Optional<String> s) {
 return s.orElse("").length();
 }
}

Run output:


3
0

However, such code is still not so elegant.

In this regard, Groovy provides a safe property/method access operator?.


user?.getUsername()?.toUpperCase();

Swift also has similar syntax, which only works on Optional on the type.

Nullable types in Kotlin

The above Java 8 example is simpler and more elegant when written in Kotlin:


package com.easy.kotlin

fun main(args: Array<String>) {
 println(strLength(null))
 println(strLength("abc"))
}

fun strLength(s: String?): Int {
 return s?.length ?: 0
}

Among them, we use String? which also expresses the meaning of Optionalf7e83be87db5cd2d9a8a0b8117b38cd4. In comparison, which one is simpler?

Clear at a glance.

There is also orElse provided by Java 8 Optional


s.orElse("").length();

This stuff is the most common Elvis operator in Kotlin:


s?.length ?: 0

In contrast, what reasons are there to continue using Java 8’s Optional?

Star symbols in Kotlin


??????????????????????????????????????
?: ?: ?: ?: ?: ?: ?: ?: ?: ?: ?: ?: ?: ?: ?: ?: ?: ?: ?: ?: ?: ?: ?: ?: ?: ?: 
?. ?. ?. ?. ?. ?. ?. ?. ?. ?. ?. ?. ?. ?. ?. ?. ?. ?. ?. ?. ?. ?. ?. ?. ?. ?. ?. ?. ?. ?.

The above is the detailed content of Comparison details of the use of Optional in Java8 and nullable types in Kotlin. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn