Home > Java > javaTutorial > 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

黄舟
Release: 2017-09-19 10:15:41
Original
1847 people have browsed it

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();
 }
}
Copy after login

Run output:


3
0
Copy after login

However, such code is still not so elegant.

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


user?.getUsername()?.toUpperCase();
Copy after login

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
}
Copy after login

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

Clear at a glance.

There is also orElse provided by Java 8 Optional


s.orElse("").length();
Copy after login

This stuff is the most common Elvis operator in Kotlin:


s?.length ?: 0
Copy after login

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

Star symbols in Kotlin


??????????????????????????????????????
?: ?: ?: ?: ?: ?: ?: ?: ?: ?: ?: ?: ?: ?: ?: ?: ?: ?: ?: ?: ?: ?: ?: ?: ?: ?: 
?. ?. ?. ?. ?. ?. ?. ?. ?. ?. ?. ?. ?. ?. ?. ?. ?. ?. ?. ?. ?. ?. ?. ?. ?. ?. ?. ?. ?. ?.
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template