[英]Is there a Java equivalent to C#'s 'checked' keyword?
是的,这是一段很简单的代码,但我仍然想知道是否有内置的替代品。
这是代码:
/**
* Cast x to int, throw an exception if there's loss of information
*/
public static int safeLongToInt(long x)
{
int result = (int) x;
if (result != x)
throw new RuntimeException("long doesn't fit in an int: " + x);
return result;
}
C#中的代码是:
int foo;
long bar = ...;
checked
{
foo = bar;
}
不,没有相应的,请查看此关键字图表 。
(预发布)开源Guava库具有您寻求的方法:
不,Java没有像C#那样自动溢出或丢失信息检查。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.