如何将Java对象转换为布尔型原语
我试过下面的方法,但不起作用
boolean di = new Boolean(someObject).booleanValue();
构造函数布尔值(对象)未定义
请给我建议。
发布于 2010-02-05 18:48:09
如果对象实际上是一个Boolean
实例,那么只需对其进行强制转换:
boolean di = (Boolean) someObject;
显式强制转换将执行到Boolean
的转换,然后是对原始值的自动拆箱。或者,您可以显式地这样做:
boolean di = ((Boolean) someObject).booleanValue();
但是,如果someObject
没有引用布尔值,那么您希望代码做什么?
发布于 2010-02-05 18:54:00
假设yourObject.toString()返回"true“或"false",您可以尝试
boolean b = Boolean.valueOf(yourObject.toString())
发布于 2021-11-27 03:21:34
使用条件运算符"?“如下所示:
int a = 1; //in case you want to type 1 or 0 values in the constructor call
Boolean b; //class var.
b=(a>0?true:false); //set it in the constructor body
https://stackoverflow.com/questions/2206598
复制相似问题