-4

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?

Garhoogin
  • 91
  • 2
  • 9

1 Answers1

4

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.

Community
  • 1
  • 1
thatguy
  • 21,059
  • 6
  • 30
  • 40