有一个Java等同于空在C#合并运算符(??)? [英] is there a Java equivalent to null coalescing operator (??) in C#?

查看:670
本文介绍了有一个Java等同于空在C#合并运算符(??)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
  如何获得Java中的第一个非空值?

是否有可能做类似于Java以下code

Is it possible to do something similar to the following code in Java

int y = x ?? -1;

<一个href=\"http://stackoverflow.com/questions/446835/what-do-two-question-marks-together-mean-in-c/446839#446839\">more有关??

推荐答案

可悲的是 - 没有。你能做的最接近的是:

Sadly - no. The closest you can do is:

int y = (x != null) ? x : -1;

当然,你可以在图书馆的方法包装这件事,如果你觉得有必要(这是不太可能减少长多),但在语法层面没有什么更简洁的使用。

Of course, you can wrap this up in library methods if you feel the need to (it's unlikely to cut down on length much), but at the syntax level there isn't anything more succinct available.

这篇关于有一个Java等同于空在C#合并运算符(??)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆