如何将对象转换为布尔值? [英] How to cast Object to boolean?
本文介绍了如何将对象转换为布尔值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将一个Java对象转换为一个布尔原语
How can I cast a Java object into a boolean primitive
我试过下面但是不工作
boolean di = new Boolean(someObject).booleanValue();
构造函数Boolean(Object)未定义
The constructor Boolean(Object) is undefined
请告知。
推荐答案
em> c 实例
boolean di = (Boolean) someObject;
显式转换将转换为 Boolean
,然后是原始值的自动取消装箱。或者你可以明确地做到:
The explicit cast will do the conversion to Boolean
, and then there's the auto-unboxing to the primitive value. Or you can do that explicitly:
boolean di = ((Boolean) someObject).booleanValue();
如果 someObject
布尔值虽然,你想让代码做什么?
If someObject
doesn't refer to a Boolean value though, what do you want the code to do?
这篇关于如何将对象转换为布尔值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文