Comparing a Boolean object to a boolean primitive

Go To StackoverFlow.com

-4

What will be the output of this code? Please explain how the Autoboxing or unboxing id done here.

class MyBoolean
{
    Boolean[] bool = new Boolean[5];

    public static void main(String[] args)
    {
      new MyBoolean().myMethod();
    }

    public void myMethod()
    {
      if(bool[1]==true)
      {
        System.out.println("It's true");
      }
      else
      {
        System.out.println("It's false");
      }
    }
}
2012-04-05 21:50
by Vibhor
Why don't you run it and find out - Oliver Charlesworth 2012-04-05 21:52
I need to know what is happening internally in this comparison - Vibhor 2012-04-05 21:53
I'm serious; run it, and you will get your answer - Oliver Charlesworth 2012-04-05 21:54
Smells like homework - T.J. Crowder 2012-04-05 22:02


5

The code fails with a NullPointerException because bool[1] contains null. According to the Java Language Specification, Section 5.1.8, the unboxing of a Boolean is done by calling booleanValue() on the Boolean reference. Since in this case, the reference is null, you get an NPE.

In a comment on another answer, you wrote:

The reason to ask this question is to understand if we get the NPE via AutoUnBoxing or via AutoBoxing. In my understanding its due to AutoBoxing.

It's due to unboxing (extracting a primitive from a reference type), not boxing (wrapping a primative in a reference type). Specifically, from JLS Section 15.21.2 (Boolean Equality Operators == and !=):

If one of the operands is of type Boolean, it is subjected to unboxing conversion (§5.1.8).

2012-04-05 22:07
by T.J. Crowder


4

Run fails: bool[1] is null and comparison throw NullPointerException.

2012-04-05 21:55
by dash1e
-1: the '==' operator cannot cause the NullPointerException - Alex Stybaev 2012-04-05 21:56
But Java try to convert bool[1] to a Boolean object to eval comparison. You got a NullPointerException - dash1e 2012-04-05 21:57
@dash1e: "But Java try to convert bool[1] to a Boolean object" No, the runtime tries to convert the Boolean in bool[1] to a boolean primitive (lower case). It's that conversion that causes the NPE, because there's no Boolean in bool[1] at all - T.J. Crowder 2012-04-05 22:01
Downvoters: He's right about it causing an NPE - T.J. Crowder 2012-04-05 22:03
You are right, it is already a Boolean, Java try to convert in boolean, I wrote quickly when I see downvotes - dash1e 2012-04-05 22:04
It is a tricky question and its not on my homework :). The reason to ask this question is to understand if we get the NPE via AutoUnBoxing or via AutoBoxing. In my understanding its due to AutoBoxing.

Explanation. bool is an array of Boolean objects, but since it is not initialized all the elements in the array are "null". Remember "null" is not an object. Now when we try to access the elements of the bool array using bool[1] at that point the compiler try to Autobox nulls to Boolean objects and then we get NPE - Vibhor 2012-04-05 22:15

@Vibhor: Not autoboxing, autoUNboxing. See my answer for references - T.J. Crowder 2012-04-05 22:29
Ads