How to pass an object to boolean?

How can I pass a Java object to a boolean primitive

I tried as below, but it does not work.

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

The constructor of Boolean (Object) is undefined

Please inform.

+60
java casting primitive
Feb 05 '10 at
source share
3 answers

If the object is actually an instance of Boolean , then simply enter it:

 boolean di = (Boolean) someObject; 

An explicit conversion will do the conversion to Boolean , and then be automatically-unboxing to a primitive value. Or you can do it explicitly:

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

If someObject does not refer to a Boolean value, what do you want the code to do?

+104
Feb 05 '10 at 10:48
source share

Assuming yourObject.toString () returns true or false, you could try

 boolean b = Boolean.valueOf(yourObject.toString()) 
+30
Feb 05 2018-10-10 at
source share

Just use:

Boolean.parseBoolean(someObject.toString())

The same for others:

  • Integer Integer.parseInt(someObject.toString())
  • Float Float.parseFloat(someObject.toString())
  • Long Boolean.parseLong(someObject.toString())
  • ...
-one
Dec 17 '17 at 20:00
source share



All Articles