i have a code as
public class BooleanTest {
public BooleanTest() {
super();
}
public static void main(String args[]){
BooleanTest bt = new BooleanTest();
bt.doProcess();
}
private boolean method() {
return false;
}
private void doProcess() {
Boolean obj = (Boolean)method();
System.out.println(obj.booleanValue());
}
}
the question is can line System.out.println(obj.booleanValue());
throw NullPointerException
in any situation?
To convert String to Boolean, use the parseBoolean() method in Java. The parseBoolean() parses the string argument as a boolean. The boolean returned represents the value true if the string argument is not null and is equal, ignoring case, to the string "true".
You cannot cast a BOOLEAN to a DECIMAL . You cannot cast a STRING value to BOOLEAN , although you can cast a BOOLEAN value to STRING , returning '1' for true values and '0' for false values. Although you can cast a TIMESTAMP to a BOOLEAN or a BOOLEAN to a TIMESTAMP , the results are unlikely to be useful.
To convert Boolean Primitive to Boolean object, use the valueOf() method in Java. Firstly, let us take a boolean primitive. boolean val = false; To convert it into an object, use the valueOf() method and set the argument as the boolean primitive.
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.
No, when you box a primitive value into its equivalent wrapper type, the result is never null.
It will never throw a NPE and also if you are using java >= 1.5, you don't need to cast it. It is called autoboxing which is introduced from JDK 1.5.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With