How to cast Object to boolean?
Asked Answered
D

3

93

How can I cast a Java object into a boolean primitive

I tried like below but it doesn't work

boolean di = new Boolean(someObject).booleanValue();

The constructor Boolean(Object) is undefined

Please advise.

Dann answered 5/2, 2010 at 10:47 Comment(0)
N
153

If the object is actually a Boolean instance, then just cast it:

boolean di = (Boolean) someObject;

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();

If someObject doesn't refer to a Boolean value though, what do you want the code to do?

Neighboring answered 5/2, 2010 at 10:48 Comment(7)
No its not a Boolean instance but has value as true or falseDann
I'm curious about what type is the variable... :)Domination
Assuming true / false are Strings you could use: boolean b = Boolean.parseBoolean(String.valueOf(someObject)); Be aware that this will return false for any String value other than "true" (case insensitive) and hence will return false if someObject is null.May
@Jon Answer for the question you asked in last line, We can use instanceOf method before casting it to Boolean.This will not give the cast exception.Clew
@vikiiii: I'm aware of instanceof, but that's a matter of how we can check it - that doesn't answer the question I asked, which is the desired behaviour.Neighboring
@Jon If someObject doesn't refer to a Boolean value though, Lets say be a String .Ex: "true" . Then I will like to parse that String as a boolean and I will use parseBoolean method to convert the string to Boolean. Does that answers your question? If not then please answer back and let me know.Clew
@vikiiii: Well, it answers part of it. It doesn't answer what you'd want to happen if the value actually referred to an Integer though. Or a Button. Or a FileInputStream. And of course I was asking the OP...Neighboring
P
38

Assuming that yourObject.toString() returns "true" or "false", you can try

boolean b = Boolean.valueOf(yourObject.toString())
Prevost answered 5/2, 2010 at 10:54 Comment(3)
Have trouble understanding why toString() is required. If it's not a string then it's just an object ?Candleberry
because yourObject is an instance of type Object and the valueOf method accepts a String.Innumerable
This should probably be Boolean.parseBoolean to avoid boxing and unboxing a Boolean.Convergent
M
-2

use the conditional operator "?" like this below:

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
Murmuration answered 27/11, 2021 at 3:21 Comment(2)
Yes. But this is not doing what the OP asked. 1) int is not an object type, and certainly not an arbitrary object type, 2) this is not a cast. The real problem is that the original question didn't make sense, and the OP never bothered to clarify it.Chervonets
not the real issuePottle

© 2022 - 2024 — McMap. All rights reserved.