2

So I made an array:

public static Boolean[][] squaresWithPieces = new Boolean[][]{
    {Boolean.TRUE,Boolean.TRUE,null,null,null,null,Boolean.FALSE,Boolean.FALSE},
    {Boolean.TRUE,Boolean.TRUE,null,null,null,null,Boolean.FALSE,Boolean.FALSE},
    {Boolean.TRUE,Boolean.TRUE,null,null,null,null,Boolean.FALSE,Boolean.FALSE},
    {Boolean.TRUE,Boolean.TRUE,null,null,null,null,Boolean.FALSE,Boolean.FALSE},
    {Boolean.TRUE,Boolean.TRUE,null,null,null,null,Boolean.FALSE,Boolean.FALSE},
    {Boolean.TRUE,Boolean.TRUE,null,null,null,null,Boolean.FALSE,Boolean.FALSE},
    {Boolean.TRUE,Boolean.TRUE,null,null,null,null,Boolean.FALSE,Boolean.FALSE},
    {Boolean.TRUE,Boolean.TRUE,null,null,null,null,Boolean.FALSE,Boolean.FALSE}
};

and when I run my main() method, which executes the following:

for (int row = 0; row < squaresWithPieces.length; row++)  // Cycles through rows.
{
    for (int col = 0; col < squaresWithPieces[row].length; col++)  // Cycles through columns.
    {
        System.out.printf("%5b ", squaresWithPieces[row][col]);  // Change the %5d to however much space you want.
    }
    System.out.println();  // Makes a new row
}

I get the following output:

 true  true false false false false false false 
 true  true false false false false false false 
 true  true false false false false false false 
 true  true false false false false false false 
 true  true false false false false false false 
 true  true false false false false false false 
 true  true false false false false false false 
 true  true false false false false false false

Why is that the case? I thought using Boolean class allows Booleans to be "null"?

This question also shows that.

2
  • 2
    Rather than asking "why does Java", it's useful to ask which part of "Java" is doing this to you. It's not that you said "put a null in the array", but it puts a false value there instead; you can easily determine that (e.g., look at it in the debugger.) This sort of divide-and-conquer will quickly lead you to "printf" being the actor here. Once you have that, its pretty easy to see why; it would rather produce some output (since printf is usually for debugging output) than throw an NPE and leave you wondering "where did the null come from." Commented Mar 14, 2022 at 22:23
  • From the Javadoc: "'b', 'B' - If the argument arg is null, then the result is "false". If arg is a boolean or Boolean, then the result is the string returned by String.valueOf(arg). Otherwise, the result is "true". " Commented Mar 14, 2022 at 23:18

3 Answers 3

4

Why is that the case? I thought using Boolean class allows Booleans to be "null" ?

Your example highlights the difference between a value and a representation of a value when you print it to the screen. The characters that print out don't necessarily reflect the true value. In this case, you are using the %b format specifier which can only print true or false, even if the actual value is something else.

You can experiment with this by changing %b to %s. This might get you a more accurate representation of the underlying values.

Sign up to request clarification or add additional context in comments.

Comments

2

It's because you used printf with %b format. Change it to for example System.out.print and you will see your nulls:

for (int row = 0; row < squaresWithPieces.length; row++) //Cycles through rows
{
  for (int col = 0; col < squaresWithPieces[row].length; col++) //Cycles through columns
  {
     System.out.print(squaresWithPieces[row][col]); // Here! 
  }
  System.out.println(); //Makes a new row
}
 

Comments

1

Your assumptions are correct but you are printing with the format %b which always output true or false ("true" if non-null, "false" if null).

1 Comment

thanks! %s did the job as it accepts any type including null.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.