Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to cast Object to boolean?

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.

like image 323
Ravi Gupta Avatar asked Feb 05 '10 10:02

Ravi Gupta


People also ask

How do you cast a boolean to an object?

boolean val = false; To convert it into an object, use the valueOf() method and set the argument as the boolean primitive. Boolean res = Boolean. valueOf(val);

Can you cast a boolean?

You can use CAST() to convert any integer or floating-point type to BOOLEAN : a value of 0 represents false , and any non-zero value is converted to true . You can cast DECIMAL values to BOOLEAN , with the same treatment of zero and non-zero values as the other numeric types. You cannot cast a BOOLEAN to a DECIMAL .

Can you cast int to boolean?

You cannot cast an integer to boolean in java. int is primitive type which has value within the range -2,147,483,648 to 2,147,483,647 whereas boolean has either true or false. So casting a number to boolean (true or false) doesn't makes sense and is not allowed in java.

How do you cast boolean to boolean?

Use the valueOf() method to convert boolean value to Boolean. Firstly, let us take a boolean value. boolean val = false; Now, to convert it to Boolean object, use the valueOf() method.

Is it possible to use the as operator to cast to bool?

The as operator may only be used with reference types, so it is not possible to do use as to try a safe-cast to bool: // This does not work: "The as operator must be used with a reference type ('bool' is a value type)" object rawValue = map.GetValue (key); bool value = rawValue as bool;

How do I convert an object to a boolean value?

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:

What does new Boolean(STR) return in Java?

CJ J.: It's worth noting that new Boolean isn't a boolean but rather an instance of Boolean. Primitives are cheaper and should be preferred over the object type. CJ J.: new Boolean (str) returns an object type.

When should I use the constructor Boolean instead of the Boolean?

If a new Boolean instance is not required, this method should generally be used in preference to the constructor Boolean (boolean), as this method is likely to yield significantly better space and time performance.


2 Answers

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?

like image 63
Jon Skeet Avatar answered Sep 27 '22 20:09

Jon Skeet


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

boolean b = Boolean.valueOf(yourObject.toString()) 
like image 20
chburd Avatar answered Sep 27 '22 20:09

chburd