Garhoogin Garhoogin - 22 days ago 4
Java Question

Java - How do if statements know the values of a Boolean object?

How do if statements recognize the

Boolean
object as a
boolean
? such as:

Boolean b = new Boolean(true);
if(b){
System.out.println("true!");
} else {
System.out.println("false!");
}


This would print true, but how is
Boolean
recognized?

Answer

It is called autoboxing and works for primitive types in Java, look here for a brief SO explanation or here for the official documentation. Java automatically converts the object representation Boolean into the corresponding primitive type boolean and back. The first is called unboxing and the latter boxing.

Comments